这题的并查集是看题解的,想不出怎么算转移次数,题解好像用的是并入的次序来计算的,这真没想到。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#define ll long long
#define MP make_pair
#define PB push_back
#define SZ(x) ((int)(x).size())
#define REP(i,n) for (int i=0; (i)<(int)n; ++(i))
#define FOR(it,c) for ( __typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it )
using namespace std;
const int N=10010;
int p[N],cnt[N],a[N];
int find(int x){
if(p[x]==x) return x;
int t=p[x];
p[x]=find(p[x]);
a[x]+=a[t];
return p[x];
}
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
int t,ca=0;
cin>>t;
while(t--){
memset(a,0,sizeof(a));
int n,m;
printf("Case %d:\n",++ca);
scanf("%d%d",&n,&m);
fill(cnt,cnt+n+1,1);
for(int i=1;i<=n;i++) p[i]=i;
while(m--){
char op[10];
scanf("%s",op);
if(op[0]=='T'){
int x,y;
scanf("%d%d",&x,&y);
int tx=find(x),ty=find(y);
if(tx!=ty){
p[tx]=ty;
a[tx]=1;
cnt[ty]+=cnt[tx];
}
}
else if(op[0]=='Q'){
int x;
scanf("%d",&x);
int tx=find(x);
printf("%d %d %d\n",tx,cnt[tx],a[x]);
}
}
}
return 0;
}

11万+

被折叠的 条评论
为什么被折叠?



