1. 程式人生 > >藍橋杯 分考場 (暴搜)

藍橋杯 分考場 (暴搜)

span print 表示 void urn room nbsp -a break

分考場

問題描述   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 }

藍橋杯 分考場 (暴搜)