Submission #910075

# Submission time Handle Problem Language Result Execution time Memory
910075 2024-01-17T20:48:13 Z asdasdqwer Pipes (CEOI15_pipes) C++14
60 / 100
787 ms 47956 KB
#include <bits/stdc++.h>
using namespace std;

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

    int get(int i) {
        if (p[i]!=i && p[i]!=p[p[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;
        r[a]+=r[b];
        return true;
    }
};

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

    int n,m;cin>>n>>m;
    DSU d1(n), d2(n);
    vector<vector<int>> g(n);

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

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

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

    function<void(int,int)> dfs=[&](int node, int pv) {
        vis[node]=true;
        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);
    }

    for (int j=1;j<18;j++) {
        for (int i=0;i<n;i++) {
            lft[i][j]=lft[i][j-1];
            if (lft[i][j] != -1) {
                lft[i][j]=lft[lft[i][j]][j-1];
            }
        }
    }

    vector<int> dp(n, 0);

    for (int i=0;i<n;i++) {
        if (d2.p[i] == i) continue;
        int a=i, b=d2.get(i);
        dp[a]++;
        dp[b]++;

        if (d[a]<d[b])swap(a,b);
        int steps=d[a]-d[b];
        int cnt=0;
        while (steps) {
            if ((steps&1)==1) {
                a=lft[a][cnt];
            }
            steps>>=1;cnt++;
        }
        
        if (a==b) {
            dp[a]-=2;
            continue;
        }

        for (int i=17;i>-1;--i) {
            if (lft[a][i] != lft[b][i]) {
                a=lft[a][i];
                b=lft[b][i];
            }
        }
        
        dp[lft[a][0]] -= 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];
        }
    };

    for (int i=0;i<n;i++) {
        if (!vis[i])dfs2(i);
    }

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1112 KB Output is correct
2 Correct 4 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 948 KB Output is correct
2 Correct 65 ms 948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 2132 KB Output is correct
2 Correct 130 ms 2136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 198 ms 4684 KB Output is correct
2 Correct 163 ms 5696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 257 ms 12880 KB Output is correct
2 Correct 240 ms 12880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 414 ms 14840 KB Output is correct
2 Runtime error 456 ms 47956 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 542 ms 18208 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 658 ms 18212 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 787 ms 17348 KB Memory limit exceeded
2 Halted 0 ms 0 KB -