POJ2524 Ubiquitous Religions

2014-11-24 10:54:34 · 作者: · 浏览: 0
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         using namespace std; const int maxn = 50001; int f[maxn+10]; int gn, gm; int getFather(int x) { if(x == f[x]) return x; else return f[x] = getFather(f[x]); } int main() { int Case = 0; int x, y, t1, t2; while(scanf("%d%d", &gn, &gm) == 2 && (gn || gm)) { for(int i = 1; i <= maxn; i++) f[i] = i; for(int j = 0; j < gm; j++) { scanf("%d%d", &x, &y); t1 = getFather(x); t2 = getFather(y); if(t1 != t2) f[t1] = t2; } int cnt = 0; for(int i = 1; i <= gn; i++) { if(f[i] == i) cnt++; } printf("Case %d: %d\n", ++Case, cnt); } }