标签:logs class algorithm iostream string expect ring data pac
2 3 3 T 1 2 T 3 2 Q 2 3 4 T 1 2 Q 1 T 1 3 Q 1
Case 1: 2 3 0 Case 2: 2 2 1 3 3 2
题解:
这题的题解和之前的那题hdu 2818 Building Blocks 很像,同个写法
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; char ch[5]; int T,n,m; int fa[10005],mov[10005],num[10005]; int findfa(int k) { if (fa[k]==k) return k; int faa=fa[k]; fa[k]=findfa(fa[k]); mov[k]+=mov[faa]; return fa[k]; } void uni(int x,int y) { int fx=findfa(x); int fy=findfa(y); if (fx==fy) return; fa[fx]=fy; num[fy]+=num[fx]; mov[fx]=1;//这里不是很理解,我觉得在底部的那个也可能一直移来移去,为什么底部的就是只移了一次。 } int main() { while(~scanf("%d",&T)) { for(int cas=1;cas<=T;cas++) { printf("Case %d:\n",cas); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { fa[i]=i; num[i]=1; mov[i]=0; } for(int i=1;i<=m;i++) { scanf("%s",&ch); if (ch[0]==‘T‘) { int x,y; scanf("%d%d",&x,&y); uni(x,y); } else { int x; scanf("%d",&x); findfa(x); printf("%d %d %d\n",fa[x],num[fa[x]],mov[x]); } } } } return 0; }
标签:logs class algorithm iostream string expect ring data pac
原文地址:http://www.cnblogs.com/stepping/p/7219714.html