蓝桥杯 分考场 (暴搜)

分考场  

问题描述
  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
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     static int n,m;
 5     static int[][] no = new int [110][110];
 6     static int[][] mp = new int [110][110];
 7     static int[] room = new int [110];
 8     static int ans = 1000000;
 9     static void dfs(int res,int x) {
10         if(x==n+1) {
11             ans = Math.min(ans, res);
12             return;
13         }
14         if(res>ans) {
15             return; 
16         }
17         for(int i=1;i<=res;i++) {
18             int flag = 0;
19             //int sum = room[i];
20             //int cnt = 0;
21             for(int j=1;j<=room[i];j++) {
22                 if(mp[x][no[i][j]]==1) {
23                     flag = 1;
24                     break;
25                 }
26             }
27             if(flag==0) {
28                 no[i][++room[i]] = x;
29                 dfs(res, x+1);
30                 --room[i];
31             }
32         }
33         no[res+1][++room[res+1]] = x;
34         dfs(res+1, x+1);
35         --room[res+1];
36     }
37     public static void main(String[] args) {
38         Scanner cin = new Scanner(System.in);
39         n = cin.nextInt();
40         m = cin.nextInt();
41         for(int i=0;i<m;i++) {
42             int x = cin.nextInt();
43             int y = cin.nextInt();
44             mp[x][y] = 1;
45             mp[y][x] = 1;
46         }
47         dfs(0,1);
48         System.out.println(ans);
49     }
50 }

 

转载于:https://www.cnblogs.com/1013star/p/10352701.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值