·【蓝桥】·分考场

感谢优秀博客链接:[蓝桥杯2017决赛]分考场、OpenJudge:分成互质数

乍一看题目以为是并查集,还是我太年轻了~
解题思想

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

int a[105][105];
int rm[105], rp[105][105];
int n, m, ans = 105;
bool Judge(int pos, int room){
for(int i = 1; i <= rm[room]; ++i){
if(a[pos][rp[room][i]])
return false;
}
return true;
}
void Dfs(int pos, int num){
if(num >= ans)
return;
if(pos == n+1){
ans = min(ans, num);
return;
}
for(int i = 1; i <= num; ++i){
if(Judge(pos, i)){
rp[i][++rm[i]] = pos;
Dfs(pos+1, num);
rm[i]--;
}
}
rp[num+1][++rm[num+1]] = pos;
Dfs(pos+1, num+1);
rm[num+1]--;
}
int main(){
cin >> n >> m;
while(m--){
int x, y;
cin >> x >> y;
a[x][y] = 1; a[y][x] = 1;
}
Dfs(1, 0);
cout << ans << endl;
return 0;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!