标签:
Description
Input
Output
Sample Input
Sample Output
#include <iostream> #include <cstring> #include <queue> #include <cmath> #include <cstdio> #include <algorithm> using namespace std; int pre[1005]; int _find(int x) { int r=pre[x]; while(r!=pre[r]) r=pre[r]; int i=x,j; while(r!=i) { j=pre[i]; pre[i]=r; i=j; } return r; } void mix(int x,int y) { int fx,fy; fx=_find(x); fy=_find(y); if(fx!=fy) pre[fx]=fy; } int main() { int c; cin>>c; while(c--) { int n,m; cin>>n>>m; for(int i=1;i<=n;i++) pre[i]=i; while(m--) { int a,b; cin>>a>>b; mix(a,b); } int ans=0; for(int i=1;i<=n;i++) if(pre[i]==i) ans++; cout<<ans<<endl; } return 0; }
HDU - 1213 How Many Tables(并查集)
标签:
原文地址:http://www.cnblogs.com/nefu929831238/p/5618074.html