1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #define N 30 6 using namespace std; 7 8 int sum; 9 int ufs[N]; 10 11 void makeset(int x) 12 { 13 for(int i=0;i<x;i++) ufs[i]=i; 14 } 15 16 int Find(int x) 17 { 18 if(x==ufs[x]) return x; 19 ufs[x]=Find(ufs[x]); 20 return ufs[x]; 21 } 22 23 void Union(int a,int b) 24 { 25 int x=Find(a); 26 int y=Find(b); 27 if(x==y) return; 28 sum=sum-1; 29 ufs[y]=x; 30 } 31 32 int main() 33 { 34 /* 35 freopen("ACM.txt","r",stdin); 36 int n,m; 37 int t; 38 cin>>t; 39 while(t--) 40 { 41 cin>>n>>m; 42 sum=n; 43 makeset(n); 44 int a,b; 45 for(int i=0;i<m;i++) 46 { 47 cin>>a>>b; 48 Union(a,b); 49 } 50 cout<<sum<<endl; 51 } 52 */ 53 }