救救我,并查集模板都不会打了QAQ

P3367 【模板】并查集

雨辰yoha @ 2019-10-01 11:46:48

看看我哪里错了呗QAQ

#include<bits/stdc++.h>
using namespace std;
int n,m;
const int MAXX=200005;
int z[MAXX] ,x[MAXX],y[MAXX],fa[MAXX];
int getf(int need){
    if(fa[need]=need){
        return need;
    }
    fa[need]==getf(fa[need]);
    return fa[need];
}

bool find(int a,int b){
    int fa=getf(a);
    int fb=getf(b);
    if(fa==fb){
        return true;
    }
    return false;
}
void merge(int x,int y){
    if(!find(x,y)){
        fa[y]=fa[x];
    } 
    return ;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>z[i]>>x[i]>>y[i];
         if(z[i]==1){
            merge(x[i],y[i]);
         }else{
            if(find(x[i],y[i])){
                cout<<"Y"<<endl;
             }else{
                cout<<"N"<<endl;
             }
         }
    }

 } 

输入

 4 7
2 1 2
1 1 2
2 1 2
1 3 4
2 1 4
1 2 3
2 1 4

输出

N
N
N
N

by Azazеl @ 2019-10-01 12:38:33

看来我的主页是适用于所有OIER


by 雨辰yoha @ 2019-10-01 12:41:54

@C20212104陈泺嘉 233


by Walker_V @ 2019-10-01 12:49:01

听说还有问题?


by 雨辰yoha @ 2019-10-01 12:49:24

@YUSS洛水天依 是的QAQ


by Magallan_forever @ 2019-10-01 12:50:06

@雨辰yoha

void merge(int x,int y){
    return f[getf(y)]=f[getf(x)];
}

by Walker_V @ 2019-10-01 12:50:06

@雨辰yoha 能给个现在的代码吗?


by Walker_V @ 2019-10-01 12:50:42

@AT是女孩子 对就是这个


by 雨辰yoha @ 2019-10-01 12:51:02

@YUSS洛水天依

#include<bits/stdc++.h>
using namespace std;
int n,m;
const int MAXX=200005;
int z[MAXX] ,x[MAXX],y[MAXX],fa[MAXX];
int getf(int need){
    if(fa[need]=need){
        return need;
    }
    fa[need]=getf(fa[need]);
    return fa[need];
}
void Init() {
    for(int i=1;i<=n+10;i++) {
    fa[i]=i;
   }
}
bool find(int a,int b){
    int fa=getf(a);
    int fb=getf(b);
    if(fa==fb){
        return true;
    }
    return false;
}
void merge(int x,int y){
    if(!find(x,y)){
        fa[y]=fa[x];
    } 
    return ;
}
int main()
{
    Init();
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>z[i]>>x[i]>>y[i];
         if(z[i]==1){
            merge(x[i],y[i]);
         }else{
            if(find(x[i],y[i])){
                cout<<"Y"<<endl;
             }else{
                cout<<"N"<<endl;
             }
         }
    }

 } 

QAQ


by 雨辰yoha @ 2019-10-01 12:51:11

QAQ?


by 雨辰yoha @ 2019-10-01 12:51:40

啊 原来是这样

我真zz


上一页 | 下一页