分考场[暴力dfs]
time limit per test | memory limit per test | input | output |
---|---|---|---|
1 seconds | 256 megabytes | standard input | standard output |
Description:
n个人参加某项特殊考试。
为了公平,要求任何两个认识的人不能分在同一个考场。
求是少需要分几个考场才能满足条件。
Input
第一行,一个整数n(1<n<100),表示参加考试的人数。
第二行,一个整数m,表示接下来有m行数据
以下m行每行的格式为:两个整数a,b,用空格分开 (1<=a,b<=n) 表示第a个人与第b个人认识。
Output
一行一个整数,表示最少分几个考场
One Example input
5
8
1 2
1 3
1 4
2 3
2 4
2 5
3 4
4 5
One Example output
4
Two Example input
5
10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
Two Example output
5
分析:
题意:
暴力
d
f
s
dfs
dfs,不行再回溯
有一个我不理解的是。。数组开
100
+
100+
100+就
r
e
re
re,开
200
+
200+
200+就过了,但是他不是只到
100
100
100吗(╯‵□′)╯︵┻━┻
Code:
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 200 + 5;
const int inf = 0x3f3f3f3f;
bool mp[maxn][maxn];
int linker[maxn][maxn]; // 第i个房间有cnt[i]个人,每个人是j
int cnt[maxn]; // 每个房间几个人
int ans, n;
void dfs(int u, int num) { // 编号 房间个数
if(num >= ans) return ;
if(u == n + 1) {
ans = min(ans, num);
return ;
}
for(int i = 0; i < num; ++i) { // 遍历房间
int tmp = 0; // 可以放一起的人
for(int j = 0; j < cnt[i]; ++j)
if(!mp[u][linker[i][j]])
tmp++;
if(tmp == cnt[i]) {
linker[i][cnt[i]++] = u;
dfs(u + 1, num);
linker[i][--cnt[i]] = 0;
}
}
linker[num][0] = u;
cnt[num]++;
dfs(u + 1, num + 1);
}
int main() {
int m, u, v;
scanf("%d%d", &n, &m);
ans = n;
memset(cnt, 0, sizeof(cnt));
memset(mp, false, sizeof(mp));
while(m--) {
scanf("%d%d", &u, &v);
mp[u][v] = mp[v][u] = true;
}
dfs(1, 0);
printf("%d\n", ans);
return 0;
}