为何错了

P3367 【模板】并查集

PTC06 @ 2016-10-20 21:45:22

#include<bits/stdc++.h>
using namespace std;
long long i,m,n,pre[10000],t,t1,t2,r,temp;
int sch(int x)
{
    int i,temporary;
    r=x;
    while (pre[r]!=r) r=pre[r];
    while (pre[i]!=r)
    {
        temp=pre[i];
        pre[i]=r;
        r=temp;
    }
    return r;
}
void join(int x, int y)
{
    int kk=x;
    int dr=y;
    if (sch(kk)!=sch(dr))
    {
        pre[dr]=kk;
    }
}
int main()
{
    cin>>m>>n;
    for (i=1;i<=m;i++)
    {
        pre[i]=i;
    }
    for (i=1;i<=n;i++)
    {
        cin>>t>>t1>>t2;
        if (t==1)
        {
            join(t1,t2);
        }
        if (t==2)
        {
            if (sch(t1)!=sch(t2)) printf("Y\n"); else printf("N\n"); 
        }
    }
}

by Ghost_lzy @ 2016-10-21 08:12:29

pre[sch(kk)]=sch(dr)@PTC06 怎么新手都爱犯这种错误


by PTC06 @ 2016-10-21 11:14:16

@Ghost_lzy 感谢


|