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;
    	}
    }
    
    
  • 相关阅读:
    hdu 1269 迷宫城堡 (并查集)
    hdu 1272 小希的迷宫 (深搜)
    hdu 1026 Ignatius and the Princess I (深搜)
    hdu 1099 Lottery
    hdu 1068 Girls and Boys (二分匹配)
    几个基础数位DP(hdu 2089,hdu 3555,uestc 1307 windy 数)
    hdu 1072 Nightmare (广搜)
    hdu 1398 Square Coins (母函数)
    hdu 1253 胜利大逃亡 (深搜)
    hdu 1115 Lifting the Stone (求重心)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078994.html
Copyright © 2011-2022 走看看