int main() { for(int i=1;i<=MAXN;i++) father[i]=i,size[i]=1; freopen("galaxy.in","r",stdin); freopen("galaxy.out","w",stdout); int T; scanf("%d",&T); while(T--) { char ch; int i,j; do {scanf("%c",&ch);}while(ch!='M'&&ch!='C'); scanf("%d%d",&i,&j); if (ch=='M') judge(i,j); else { if (getfather(i)==getfather(j)) printf("%d\n",abs(before[i]-before[j])-1); else printf("-1\n"); } } return 0; }
void work() { memset(mint,0,sizeof(mint)); int minspan=0,tminspan=0; for(int i=1;i<=m;++i) if(getfather(edge[i].x)!=getfather(edge[i].y)) { mint[++mint[0]]=i; minspan+=edge[i].z; Union(edge[i].x,edge[i].y); } memset(canuse,true,sizeof(canuse)); for(int i=1;i<=mint[0];++i) { tminspan=0; canuse[i]=false; for(int j=1;j<=n;++j) father[j]=j; for(int j=1;j<=m;++j) if(canuse[j]) if(getfather(edge[j].x)!=getfather(edge[j].y)) { tminspan+=edge[j].z; // printf("%d ",j); Union(edge[j].x,edge[j].y); } // printf("\n%d\n",tminspan); if(tminspan==minspan) { printf("Not Unique!\n"); return; } canuse[i]=true; } printf("%d\n",minspan); }
inline void judge(int i,int j) { int fi=getfather(i),fj=getfather(j); father[fi]=fj; before[fi]=size[fj]; size[fj]+=size[fi]; }
void merge(int v, int u) { if(v > u) return merge(u, v); int t1, t2; t1 = getfather(v); t2 = getfather(u); if(t1 != t2) { find[t2] = t1; // 靠左原则,左边的变成右边的boss } }
void check(int a, int b, int type) { int x=getfather(a), y=getfather(b); if(x==y) { if((key[a]-key[b]+3)%3!=type) ++ans; return; } father[x]=b; delt[x]=(type-key[a]+3)%3; }
int getfather(int x){ if(x==f[x])return x; else{ int tmp=getfather(f[x]); f[x]=tmp;return tmp; } }
inline int getfather(int i) { if (father[i]==i) return i; int fi=getfather(father[i]); before[i]=before[i]+before[father[i]]; return father[i]=fi; }
int main() { freopen("monkeyk.in","r",stdin); freopen("monkeyk.out","w",stdout); scanf("%d",&N); for(int i=1;i<=N;i++) { father[i]=i; scanf("%d",&mem[i].key); Heap[i]=i; } scanf("%d",&M); while(M--) { int x,y; scanf("%d%d",&x,&y); if (same(x,y)) printf("-1\n"); else { int fx=getfather(x),fy=getfather(y); judge(fx,fy); int t; int p=Maximum(Heap[fx]); t=del(Heap[fx],p); Heap[fx]=Union(t,Heap[fx]); mem[p].key/=2; Heap[fx]=insert(Heap[fx],p); p=Maximum(Heap[fy]); t=del(Heap[fy],p); Heap[fy]=Union(t,Heap[fy]); mem[p].key/=2; Heap[fy]=insert(Heap[fy],p); Heap[fx]=Union(Heap[fx],Heap[fy]); Heap[fy]=NULL; printf("%d\n",mem[Maximum(Heap[fx])].key); } } return 0; }
int main(){ num=0; while(scanf("%d%d",&n,&m)!=EOF){ num++; if(!n&&!m) return 0; for(i=1;i<=n;i++)f[i]=i; for(i=1;i<=m;i++){ scanf("%d%*c%d",&x,&y); add(x,y); } for(i=1;i<=n;i++)flag[i]=false; ans=0; for(i=1;i<=n;i++){ if(!flag[getfather(i)]){ ans++;flag[getfather(i)]=true; } } printf("Case %d: %d\n",num,ans); } }
int getfather(int x) { int t=father[x]; if(father[x]==x) return x; father[x]=getfather(father[x]); key[x]=(delt[x]+key[t])%3; // printf("now:%d father:%d relat:%d get:%d\n",x,father[x],delt[x],key[x]); delt[x]=key[x]; // printf("now:%d father:%d relat:%d \n",x,father[x],key[x]); return father[x]; }
void judge() { if(!tot) { printf("Possible\n"); return; } else{ for(int i=2;i<=tot;++i) if(getfather(i)!=getfather(i-1)) { printf("Impossible\n"); return; } int cnt=0; for(int i=1;i<=tot;++i) cnt+=deg[i]&1; if(cnt==0||cnt==2) printf("Possible\n"); else printf("Impossible\n"); } }
int main() { while(scanf("%d%d",&n,&m)) { int k,x,y; if(!n&&!m) break; for(int i=0;i<n;++i) father[i]=i; for(int i=1;i<=m;++i){ scanf("%d",&k); scanf("%d",&x); for(int j=2;j<=k;++j){ scanf("%d",&y); together(x,y); } } ans=1; int f=getfather(0); for(int i=1;i<n;++i) if(getfather(i)==f) ++ans; printf("%d\n",ans); } }
int getfather(int v) { // int father = v; // while(find[father] != father) // { // father = find[father]; // } // return father; // 采用递归方式实现 // 每次在函数返回的时候,将 if(find[v] == v) { return v; } else { find[v] = getfather(find[v]); return find[v]; } }
// 初始化并查集 #define N 100 int father[N]; void init() { for(int i=0; i<N; i++) father[i] = i; } // 合并两个元素所在的集合 void union(int x,int y) { x = getfather(x); y = getfather(y); if(x!= y) father[x]=y; } // 判断两个元素是否属于同一个集合 bool same(int x,int y) { return getfather(x)==getfather(y); } // 获取根结点 int getfather(int x) { while(x != father[x]) x = father[x]; return x; }
inline bool same(int x,int y) { return getfather(x)==getfather(y); }
inline void judge(int x,int y) { int fx=getfather(x),fy=getfather(y); father[fy]=fx; }
inline int getfather(int u) { if (father[u]==u) return u; return father[u]=getfather(father[u]); }
void together(int a, int b) { int x=getfather(a),y=getfather(b); father[x]=y; }
int add(int x,int y){ if(getfather(x)!=getfather(y))f[getfather(x)]=getfather(y); }
void Union(int a, int b) { int x=getfather(a),y=getfather(b); father[x]=y; }
int getfather(int x) { if(father[x]==x) return x; return father[x]=getfather(father[x]); }
void together(int a, int b) { father[getfather(a)]=getfather(b); }
int getfather(int x) { return father[x]==x?x:father[x]=getfather(father[x]); }