-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42HowManyTables1445.cpp
63 lines (59 loc) · 1.17 KB
/
42HowManyTables1445.cpp
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
58
59
60
61
62
63
#include<iostream>
#include<vector>
using namespace std;
#define N 1001
//有时候题目要求的换行 就是你手动换行。。真是醉了!
int tree[N];
int sum[N];
int FindRoot(int x)
{
if(tree[x]==-1)
return x;
else
{
int tmp=FindRoot(tree[x]);
tree[x]=tmp;
return tmp;
}
}
int main()
{
int T;
while(cin>>T)
{
vector<int>v;
vector<int>::iterator it;
while(T--)
{
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++)
{
tree[i]=-1;
sum[i]=1;
}
for(int j=0; j<m; j++)
{
int a,b;
cin>>a>>b;
a=FindRoot(a);
b=FindRoot(b);
if(a!=b)
{
tree[a]=b;
sum[b]+=sum[a];
}
}
int ans=0;
for(int i=1; i<=n; i++)
{
if(tree[i]==-1)
ans++;
}
v.push_back(ans);
}
for(it=v.begin(); it!=v.end(); it++)
cout<<*it<<endl;
}
return 0;
}