记录编号 600085 评测结果 AAAAAAAAAAAA
题目名称 亲戚 最终得分 100
用户昵称 Gravatardream 是否通过 通过
代码语言 C++ 运行时间 0.761 s
提交时间 2025-04-15 20:04:25 内存使用 3.39 MiB
显示代码纯文本
#include<bits/stdc++.h>
#define ls p*2
#define rs p*2+1
using namespace std;
typedef long long ll;
const int N=20005;
int f[N];
int find(int x){
//    cout<<f[x]<<" ";
    if(f[x]==x){
        return f[x];
    }
    return find(f[x]);
}
int n,m;
int main(){
    freopen("relations.in","r",stdin);
    freopen("relations.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        int fx=find(x);
        int fy=find(y);
        f[fy]=fx;
    }
    int q;
    cin>>q;
    while(q--){
        int x,y;
        cin>>x>>y;
        int fx=find(x);
        int fy=find(y);
        if(fx==fy){
            cout<<"Yes\n";
        }
        else{
            cout<<"No\n";
        }
    }
    return 0;
}