求助dalao::为什么错了

P3367 【模板】并查集

cormac @ 2017-03-18 19:27:43

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=10010;
int n,m;
struct Node{
    int rank;
    int parent;
//    int data;
}node[maxn];
int find(int x)
{    
    if(x==node[x].parent)  return x;
    else return node[x].parent=find(node[x].parent);
}
void join(int x,int y)
{
    int i=find(x);
    int j=find(j);
    if(i==j) return;
    if(node[i].rank>=node[j].rank)
     {
         node[j].parent=i;
         node[i].rank+=node[j].rank;
    }
    else
    {
        node[i].parent=j;
        node[j].rank+=node[i].rank;
    }
}
int main(){
    //freopen("ss.in","r",stdin);
    //freopen("ss.out","w",stdout);
    int i,j;
    cin>>n>>m;
    for(i=1;i<=n;i++){ 
      node[i].rank=1;
      node[i].parent=i;
    }
    for(i=1;i<=m;i++)
    {
        int z,x,y;
        scanf("%d%d%d",&z,&x,&y); 
        if(z==1) if(x!=y) join(x,y);
        if(z==2)
        {
            int fx=find(x),fy=find(y);
            if(fx==fy) printf("Y\n");
            else printf("N%d\n",i);
        }
    }
 //   fclose(stdin);
   // fclose(stdout);
    return 0;
}

|