蓝桥杯 分考场 (暴搜)

摘要:
整数n(1<5staticint[][]no=newint[110][110];6staticint[][]mp=newint[110][110],7staticint[]room=newint[100];ans){15return;i++){18intflag=0;20//intcnt=0;j<=room[i];35-room[res+1];

分考场  

问题描述
  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 }

免责声明:文章转载自《蓝桥杯 分考场 (暴搜)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇深入探究VC —— 资源编译器rc.exe(3)wav文件头详解,看懂wav文件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

小程序下载canvas生成图片

save_share_img:function(img){ var that = this; let { result } = that.data; getData.getData( "save_share_img", { id...

java常用加解密工具类

packagecom.sh.springboottdemo2.util; importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64; importjavax.crypto.Cipher; importjavax.crypto.KeyGenerator; importjavax.crypt...

uni-app 选择图片(chooseImage)

uni-app 选择图片(chooseImage) 1 <template> 2 <view> 3 <button type="primary"@click="img">button</button> 4 <button type="primary"@tap="i...

serviceWorker

推荐阅读:Service Worker 简介 在 Service Worker 之前,我们一般用 AppCache 来实现离线体验(就是配置 Manifest 文件的方式),这个会有很多问题(博主曾尝试过,体验非常差,非常难用,而且不灵活)。 而 Service Worker 可以写脚本去灵活自由地控制缓存。 基本使用: 1. 注册 <!-- /re...

vue--axios发送请求

首先安装:axios $ npm install axios $ cnpm install axios //taobao源 $ bower install axios 或者使用cdn: <script src="https://unpkg.com/axios/dist/axios.min.js"></script> main...

Python 基础之推导式

一.列表推导式 通过一行循环判断,遍历出一系列数据的方式就是推导式 特点:方便,简洁,可以实现一些简单的功能推导式当中只能跟循环和判断(单项分支)种类分为三种: 列表推导式  集合推导式  字典推导式 1.推导式内容讲解 #(1) 基本语法#例: #[1,2,3,4] => [2,4,6,8]lst = [1,2,3,4]lst2 = []for i...