How Many Tables HDU - 1213
#include<iostream> using namespace std; const int N=1010; int p[N]; int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } int main() { int t; int n,m; cin>>t; while(t--) { cin>>n>>m; for(int i=1;i<=n;i++) p[i]=i; while(m--) { int a,b; cin>>a>>b; int pa=find(a); int pb=find(b); if(pa!=pb) p[pa]=pb; } int res=0; for(int i=1;i<=n;i++) if(p[i]==i) res++; cout<<res<<endl; } return 0; }