求dalao查错QAQ

P3367 【模板】并查集

DarkMomo @ 2016-11-18 20:59:00

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int book[100005];
int n;
int inti()
{
    int i;
    for(i=1;i<=n;i++)
    {
        book[i]=i;
    }
}
int find(int f)
{
    if(book[f]=f) return f;
    else{
        book[f]=find(book[f]);
        return book[f];
    }
}
void mix(int v,int w)
{
    int a,b;
    a=find(v);
    b=find(w);
    if(a!=b)
    {
        book[b]=a;
    }
    return;
}
int main()
{
    int m,z,x,y;
    cin>>n>>m;
    for(int i=1;i<n;i++)
    {
        book[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
        cin>>z>>x>>y;
        if(z==1) mix(x,y);
        else if(z==2)
        {
            if(book[x]==book[y])
            {
                cout<<"Y"<<endl;
            }
            if(book[x]!=book[y]) cout<<"N"<<endl;
        }
    }
    for(int i=1;i<=n;i++){
        cout<<book[i]<<endl;
    }
    while(1);
}

by Shan_Xian @ 2016-12-21 13:07:05

while(1)


by 6QWQ6 @ 2017-03-08 14:17:17

呵呵


by Lyrics @ 2017-07-23 15:13:24

int find(int i){//找父节点操作
    if(i==father[i])return i;
    while(i!=father[i])i=father[i];
    return father[i];
}
void unionn(int i,int j){//合并操作
    int r1=find(i),r2=find(j);
    if(r1==r2)return;
    father[r2]=r1;
    father[j]=r1;
    return;
}
希望对您有帮助!

|