BeileySok


私信TA

用户名:H2030819055

访问量:1464

签 名:

等  级
排  名 974
经  验 3263
参赛次数 16
文章发表 6
年  龄 20
在职情况 学生
学  校 贺州学院
专  业 软件工程

  自我简介:

TA的其他文章

二分法查找
浏览:312
大二上学期代码
浏览:427
第H道题目的
浏览:79

#include<bits/stdc++.h>

using namespace std;

int father[100005];

int n;

void init(){

    for(int i=1;i<=n;i++){

        father[i]=i;

    }

}

int getfather(int x){

    if(father[x]==x){

        return x;

    }

    father[x]=getfather(father[x]);

    return father[x];

}

int fun(int x,int y){

    father[getfather(x)]=getfather(y);

}

int main(){

    int m,a,b;

    cin>>n>>m;

    init();

    while(m--){

        cin>>a>>b;

        if(getfather(a)!=getfather(b)){

            fun(a,b);

        }

    }

    int x,y;

    cin>>x>>y;

    if(getfather(x)==getfather(y)){

        cout<<"YES";

    }else{

        cout<<"NO";

    }

    return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区