問題描述
n個人參加某項特殊考試。
為了公平,要求任何兩個認識的人不能分在同一個考場。
求是少需要分幾個考場才能滿足條件。
為了公平,要求任何兩個認識的人不能分在同一個考場。
求是少需要分幾個考場才能滿足條件。
輸入格式
第一行,一個整數n(1<n<100),表示參加考試的人數。
第二行,一個整數m,表示接下來有m行數據
以下m行每行的格式為:兩個整數a,b,用空格分開 (1<=a,b<=n) 表示第a個人與第b個人認識。
第二行,一個整數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
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
10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
樣例輸出
5
基本思路:
回溯啊,一開始瘋了沒剪枝,后來想剪枝的時候發現想不到什么剪枝策略,就很難受,然后就加了一個最顯而易見的剪枝就過了
代碼如下:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
int gra[maxn][maxn];
int cun[maxn][maxn];
int cnt[maxn];
int res=inf;
int n,m;
void solve(int id,int num){
if(num>=res){
return;
}
if(id>n){
res=min(res,num);
return;
}
for(int i=1;i<=num;i++){
int sz=cnt[i];
int jishu=0;
for(int j=1;j<=sz;j++){
if(gra[id][cun[i][j]]==0){
jishu++;
}
}
if(jishu==sz){
cun[i][++cnt[i]]=id;
solve(id+1,num);
cnt[i]--;
}
}
cun[num+1][++cnt[num+1]]=id;
solve(id+1,num+1);
--cnt[num+1];
}
int main(){
scanf("%d%d",&n,&m);
memset(gra,0,sizeof(gra));
memset(cnt,0,sizeof(cnt));
while(m--){
int a,b;
scanf("%d%d",&a,&b);
gra[a][b]=gra[b][a]=1;
}
solve(1,0);
printf("%d\n",res);
return 0;
}
