-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10227.cpp
More file actions
57 lines (52 loc) · 1.12 KB
/
10227.cpp
File metadata and controls
57 lines (52 loc) · 1.12 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <cstring>
int P, T, seen[102][102];
char buf[128];
int parent[102], size;
int find_set(int x) {
if(parent[x] == x)
return x;
return parent[x] = find_set(parent[x]);
}
bool union_set(int x, int y) {
x = find_set(x);
y = find_set(y);
if(x == y) return true;
parent[x] = y;
size--;
return false;
}
bool same_opinion(int i, int j) {
for(int k = 0; k < T; ++k)
if(seen[i][k] != seen[j][k])
return false;
return true;
}
int main(int argc, char *argv[]) {
int TC, u, v,i,j;
gets(buf);
sscanf(buf,"%d",&TC);
gets(buf);
while(TC-- > 0) {
memset(seen, 0, sizeof(seen));
gets(buf);
sscanf(buf,"%d%d",&P,&T);
while(gets(buf) && sscanf(buf,"%d%d",&u,&v) == 2) {
--u; --v;
seen[u][v] = 1;
}
size = P;
for(i= 0; i < P; ++i)
parent[i] = i;
for(i = 0; i < P - 1; ++i) {
for(j = i + 1; j < P; ++j) {
if(same_opinion(i, j)) {
union_set(i, j);
}
}
}
printf("%d\n",size);
if(TC) printf("\n");
}
return 0;
}