数据太水了,不用压缩
2024-08-24 19:01:59
发布于:云南
11阅读
0回复
0点赞
改前:
#include<bits/stdc++.h>
using namespace std;
int fa[5005];
int get(int n){
    if(fa[n] == n) return n;
    return get(fa[n]);
}
void merge(int x,int y){
    fa[get(x)] = get(y);
}
int main(){
    int n,m,p; cin >> n >> m >> p;
    for(int i = 1;i <= n;i++) fa[i] = i;
    for(int i = 1;i <= m;i++){
        int u,v; cin >> u >> v;
        if(get(u) != get(v)) merge(get(u),get(v));
    }
    for(int i = 1;i <= p;i++){
        int x,y; cin >> x >> y;
        if(get(x) != get(y)) cout << "No\n";
        else cout << "Yes\n";
    }
    return 0;
}
改后:
#include<bits/stdc++.h>
using namespace std;
int fa[5005];
int get(int n){
    if(fa[n] == n) return n;
    return fa[n] = get(fa[n]);
}
void merge(int x,int y){
    fa[get(x)] = get(y);
}
int main(){
    int n,m,p; cin >> n >> m >> p;
    for(int i = 1;i <= n;i++) fa[i] = i;
    for(int i = 1;i <= m;i++){
        int u,v; cin >> u >> v;
        if(get(u) != get(v)) merge(get(u),get(v));
    }
    for(int i = 1;i <= p;i++){
        int x,y; cin >> x >> y;
        if(get(x) != get(y)) cout << "No\n";
        else cout << "Yes\n";
    }
    return 0;
}
这里空空如也







有帮助,赞一个