Submission #910049

# Submission time Handle Problem Language Result Execution time Memory
910049 2024-01-17T19:49:15 Z asdasdqwer Pipes (CEOI15_pipes) C++14
40 / 100
850 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;

struct DSU {
    vector<int> p,r;
    DSU(int n) :p(n),r(n) {
        for (int i=0;i<n;i++)p[i]=i;
    }

    int get(int i) {
        if (p[i]!=i)p[i]=get(p[i]);
        return p[i];
    }

    bool unite(int a,int b) {
        a=get(a),b=get(b);
        if (a==b)return false;
        if (r[a]<r[b])swap(a,b);
        p[b]=a;
        if (r[a]==r[b])r[a]++;
        return true;
    }
};

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n,m;cin>>n>>m;
    DSU d1(n), d2(n);
    vector<array<int,2>> edg;

    for (int i=0;i<m;i++) {
        int a,b;cin>>a>>b;a--;b--;
        if (d1.unite(a,b)) {
            edg.push_back({a,b});
        }

        else {
            d2.unite(a,b);
        }
    }

    vector<vector<int>> g(n);
    for (auto x:edg) {
        g[x[0]].push_back(x[1]);
        g[x[1]].push_back(x[0]);
    }

    vector<vector<int>> lft(n, vector<int>(20, -1));
    bitset<100001> vis;
    vector<int> d(n,0);

    function<void(int,int)> dfs=[&](int node, int pv) {
        vis[node]=true;
        for (int i=1;i<20;i++) {
            if (lft[node][i-1] == -1) break;
            lft[node][i]=lft[lft[node][i-1]][i-1];
        }

        for (int x:g[node]) {
            if (x==pv)continue;
            lft[x][0]=node;
            d[x]=d[node]+1;
            dfs(x, node);
        }
    };

    for (int i=0;i<n;i++) {
        if (!vis[i])dfs(i,-1);
    }

    vector<vector<int>> bkt(n);

    for (int i=0;i<n;i++) {
        bkt[d2.get(i)].push_back(i);
    }

    function<int(int,int)> jmp=[&](int a, int steps) -> int {
        int cnt=0;
        while (steps) {
            if ((steps&1)==1) {
                a=lft[a][cnt];
                if (a==-1)return -1;
            }
            steps>>=1;cnt++;
        }
        return a;
    };

    function<int(int,int)> lca=[&](int a,int b) -> int {
        if (d[a]<d[b])swap(a,b);
        a=jmp(a,d[a]-d[b]);
        if (a==b)return a;

        for (int i=19;i>=0;i--) {
            if (lft[a][i] != lft[b][i]) {
                a=lft[a][i];
                b=lft[b][i];
            }
        }
        return lft[a][0];
    };

    vector<int> dp(n, 0);

    for (int i=0;i<n;i++) {
        if (bkt[i].size() < 2) continue;
        
        int st = bkt[i][0];

        for (int x:bkt[i]) {
            if (x==st)continue;
            int l=lca(st, x);
            dp[st]++;
            dp[x]++;
            dp[l] -= 2;
        }
    }

    for (int i=0;i<n;i++) {
        vis[i]=0;
    }
    
    function<void(int)> dfs2=[&](int node) {
        vis[node]=true;
        for (int x:g[node]) {
            if (vis[x])continue;
            dfs2(x);
            dp[node]+=dp[x];
        }

        if (dp[node] == 0 && lft[node][0] != -1) {
            cout<<node+1<<" "<<lft[node][0]+1<<"\n";
        }
    };

    for (int i=0;i<n;i++) {
        if (!vis[i])dfs2(i);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1628 KB Output is correct
2 Correct 3 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 6728 KB Output is correct
2 Correct 74 ms 6360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 12112 KB Output is correct
2 Correct 140 ms 13984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 202 ms 21968 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 295 ms 37444 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 453 ms 52324 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 590 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 708 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 850 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -