1 条题解

  • 0
    @ 2025-11-21 12:03:54
    #include<bits/stdc++.h>
    using namespace std;
    #define fre(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout);
    struct DSU{
        vector<int> f;
        DSU(int n):f(n){
            iota(f.begin(),f.end(),0);
        }
        int find(int x){
            return f[x] == x ? x : f[x] = find(f[x]);
        }
        bool merge(int a,int b){
            int fa = find(a),fb = find(b);
            if(fa == fb)return false;
            f[fb] = fa;
            return true;
        }
    };
    int main(){
        fre("rings");
        cin.tie(nullptr) -> ios::sync_with_stdio(false);
        int n,m,q;
        cin >> n >> m >> q;
        vector<vector<int>> node(n + 1);
        for(int i = 1;i <= m;i ++){
            int a,b;
            cin >> a >> b;
            node[a].push_back(b);
            node[b].push_back(a);
        }
        DSU ds(n + 1);
        int ok = true;
        for(int i = 1;i <= q;i ++){
            int op;
            cin >> op;
            if(op == 1){
                ds = DSU(n + 1);
                ok = true;
            }else if(op == 2){
                int x;cin >> x;
                for(int c:node[x]){
                    if(!ds.merge(x,c))ok = false;
                }
            }else {
                cout << (ok ?"Yes":"No") <<"\n";
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    24
    时间
    1000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    34
    已通过
    1
    上传者