题目链接:
PREV-53 分考场
思路:
假设我们目前有ans个考场,我们dfs一下,检查这么多考场够不够用;
ans个考场,分为已经有人在的考场和没人在的考场,对于每一个人,可以分配到有人的和没有人的,只不过有人的考场需要逐个检查是不是朋友;
最后我们让ans从1开始逐个递增,寻找到最少考场数即可;
//刚开始博主用的二分ans,不知为何只有60…递增就是满分
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
bool G[maxn][maxn];
int n, m, ans;
vector<int> v[maxn];
#define DFS { v[i].push_back(u); if(dfs(u + 1)) return true; v[i].pop_back(); }
inline bool dfs(int u) {
if(u == n + 1) return true;
for(int i = 1; i <= ans; i++) {
if(v[i].size() == 0) { DFS; break; }
for(int j = 0; j < v[i].size(); j++) {
if(G[v[i][j]][u]) break;
if(j + 1 == v[i].size()) { DFS; }
}
}
return false;
}
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
G[x][y] = G[y][x] = 1;
}
for(ans = 1; ans <= n; ans++) if(dfs(1)) break;
printf("%d", ans);
return 0;
}