zoukankan      html  css  js  c++  java
  • Java实现蓝桥杯历届试题分考场

    历届试题 分考场
    时间限制:1.0s 内存限制:256.0MB
    提交此题
    问题描述
      n个人参加某项特殊考试。
      为了公平,要求任何两个认识的人不能分在同一个考场。
      求是少需要分几个考场才能满足条件。
    输入格式
      第一行,一个整数n(1<n<100),表示参加考试的人数。
      第二行,一个整数m,表示接下来有m行数据
      以下m行每行的格式为:两个整数a,b,用空格分开 (1<=a,b<=n) 表示第a个人与第b个人认识。
    输出格式
      一行一个整数,表示最少分几个考场。
    样例输入
    5
    8
    1 2
    1 3
    1 4
    2 3
    2 4
    2 5
    3 4
    4 5
    样例输出
    4
    样例输入
    5
    10
    1 2
    1 3
    1 4
    1 5
    2 3
    2 4
    2 5
    3 4
    3 5
    4 5
    样例输出
    5

    import java.util.Scanner;
    
    public class fenkaochang {
    	public static int n = 0, m = 0, min = 1000;
    	public static boolean[][] know;
    	public static int[][] kaochang;
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		n = sc.nextInt();
    		m = sc.nextInt();
    		know = new boolean[n + 1][n + 1];
    		kaochang = new int[n + 1][n + 1];
    		for (int i = 0; i < m; i++) {
    			int c = sc.nextInt();
    			int d = sc.nextInt();
    			know[c][d] = true;
    			know[d][c] = true;
    		}
    		sc.close();
    		f(1, 1);
    		System.out.println(min);
    	}
    
    	public static void f(int a, int b) {// a是考场b是人
    		if (b >= n + 1) {
    			min = Math.min(a, min);
    			return;
    		}
    		if (a >= min) {
    			return;
    		}
    		A: for (int i = 1; i <= a; i++) {
    			int j;
    			for (j = 1; j < b; j++) {
    				if (kaochang[i][j] != 0) {
    					if (know[b][j]) {
    						continue A;
    					}
    				}
    			}
    			if (j == b) {
    				kaochang[i][b] = b;
    				f(a, b + 1);
    				kaochang[i][b] = 0;
    			}
    		}
    		kaochang[a + 1][b] = b;
    		f(a + 1, b + 1);
    		kaochang[a + 1][b] = 0;
    	}
    }
    
    
  • 相关阅读:
    SVN服务器搭建和使用(二)
    Unity5自动命名Assetbundle并打包
    Unity学习(六)5.x依赖打包
    Dota2技能系统设计分析
    android调用其他apk的activity
    Android 跨应用调用Activity
    Unity5的AssetBundle的一点使用心得
    Myeclipse快捷键集合
    我的CSS命名规则
    常用的 Windows 键
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948795.html
Copyright © 2011-2022 走看看