#include <stdio.h> #include<string.h> #define max(a,b) (a>b)?a:b int set[30005]; int count[30005]; int getParent(int i,int *set) { if(i==set[i]) return i; else return (set[i]=getParent(set[i],set)); } int isUnion(int a,int b,int* set) { return getParent(a,set)==getParent(b,set); } void makeUnion(int a,int b,int* set) { set[getParent(a,set)] = getParent(b,set); } int main() { // freopen("input.txt","r",stdin); int c,y=1; scanf("%d\n\n",&c); while(c--) { if(y>1)printf("\n"); y=2; char s[20]; gets(s); int i,j,k,l,n,m; n=s[0]-64; for(i=1;i<=n;i++) count[set[i]=i]=0; while(gets(s)&&strlen(s)!=0) { i=s[0]-64; k=s[1]-64; makeUnion(i,k,set); } for(k=1;k<=n;k++) count[getParent(k,set)]++; m=0; for(i=1;i<=n;i++) { if(count[i]>0)m++; } printf("%d\n",m); } return 0; }
Sunday, August 17, 2014
uva : 459 - Graph Connectivity
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment