家族-AC

#include<iostream>
using namespace std;

int father[5005];
int n,m,p,x,y;

int find(int x)
{
    if (father[x]!=x) father[x]=find(father[x]);
    return father[x];
}

void un(int x, int y)
{
    x=find(x); y=find(y);
    father[y]=x;
}

bool judge(int x, int y)
{
    x=find(x); y=find(y);
    if(father[x]==father[y]) return true;
    else return false;
}

int main()
{
    cin>>n>>m>>p;
    for (int i=1; i<=n; i++)
    {
        father[i]=i;
    }
    for (int i=1; i<=m; i++)
    {
        cin>>x>>y;
        un(x,y);
    }
    for (int i=1; i<=p; i++)
    {
        cin>>x>>y;
        if(judge(x,y))
        {
            cout<<"Yes"<<endl;
        } else
        {
            cout<<"No"<<endl;
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章