蒟蒻求教模板能过这题就wa完

P1551 亲戚

@[Excaliburlll](/space/show?uid=41821) ```cpp #include<stdio.h> using namespace std; int f[100005],n,i,x,y,m,p; inline int read() { int X=0,w=0; char ch=0; while(!(ch>='0'&&ch<='9')) {w|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9') X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } int find(int x) { if(x!=f[x]) f[x]=find(f[x]); return f[x]; } inline void un(int r1,int r2) { f[r1]=r2; } int main () { n=read();m=read();p=read(); for(i=1;i<=n;i++) { f[i]=i; } for(i=1;i<=m;i++) { x=read();y=read(); int x1=find(x); int y1=find(y); if(x1!=y1) { un(x1,y1); } } for(i=1;i<=p;i++) { x=read();y=read(); if(find(x)==find(y)) { puts("Yes"); } else { puts("No"); } } return 0; } ```
by 雪绮晶 @ 2018-09-30 23:41:30


@[雪绮晶](/space/show?uid=110460) 谢谢dalao%%%不过我还是想找下自己code哪里有问题
by Fburan @ 2018-10-01 06:23:07


@[Excaliburlll](/space/show?uid=41821) 你的那个cnt变量不是会从0开始记录 最后循环不是 $0$到$cnt-1$?
by 顾z @ 2018-10-01 06:53:18


@[Excaliburlll](/space/show?uid=41821) 还有这个rank数组没必要吧 qwq
by 顾z @ 2018-10-01 06:54:46


@[顾z](/space/show?uid=87960) 只是没有用靠左原则,然后保证每次合并的时候都是将比较小的树合并到大的树
by Fburan @ 2018-10-01 14:19:45


@[顾z](/space/show?uid=87960) cnt是后来改的,然而正常遍历也会wa%%
by Fburan @ 2018-10-01 14:20:42


|