当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 并查集模板

并查集模板

2019年05月22日  | 移动技术网IT编程  | 我要评论

吴亚军 邓楠,微视角奎爷,吾组词

推荐题目:p3367 【模板】并查集

该题的缺点:数据不够水
本蒟蒻实在是太弱了,不会!
推荐一篇文章:。
code:

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n,m,bz;
int father[200001];
int find(int x) {
    if(x==father[x]) return x;
    return father[x]=find(father[x]);
}
void unnio(int x,int y) {
    father[y]=x;
}
inline int read() {
    bool f=0;
    int x=0;
    char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-') f=!f;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        x=x*10+c-'0';
        c=getchar();
    }
    return f?-x:x;
}
inline void write(int x) {
    if(x<0) {
        putchar('-');
        write(-x);
    } else {
        if(x/10) write(x/10);
        putchar(x%10+'0');
    }
}

int main() {
    n=read();
    m=read();
    for(int i=1; i<=n; ++i)
        father[i]=i;
    int x,y;
    for(int i=1; i<=m; ++i) {
        bz=read();
        x=read();
        y=read();
        if(bz==1)
        {
            int bz1=find(x);
            int bz2=find(y);
            if(bz1!=bz2) unnio(bz1,bz2);
        }
        else
        {
            if(find(x)==find(y)) printf("y\n");
            else printf("n\n");
        }
    }
    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网