
题目链接
sol
跟叠积木差不多,就是询问的时候输出两个权值相减而已
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #define rep(i,l,r) for(int i=l;i<=r;++i) #define per(i,l,r) for(int i=l;i>=r;--i) #define MAXN 30010 using namespace std; inline int () { char c; int a=0; while((c=getchar())==' '||c=='n'||c=='r'); while(c>='0'&&c<='9') { a*=10;a+=(c-'0');c=getchar(); } return a; } int a[MAXN],b[MAXN],v[MAXN]; int finda(int x) { if(!a[x]) return x; finda(a[x]); v[x]+=v[a[x]]; return a[x]=finda(a[x]); } int findb(int x) { return b[x]?b[x]=findb(b[x]):x; } int main() { char c; int tx,ty; int T=read(); while(T--) { while((c=getchar())==' '||c=='n'||c=='r'); tx=read();ty=read(); if(c=='M') { tx=finda(tx);ty=findb(ty); v[tx]=1; a[tx]=ty; b[ty]=findb(tx); } else if(c=='C') { if(finda(tx)!=finda(ty)) printf("-1n"); else printf("%dn",abs(v[tx]-v[ty])-1); } } return 0; }
|
近期评论