Submission #54862

# Submission time Handle Problem Language Result Execution time Memory
54862 2018-07-05T08:22:43 Z 노영훈(#1508) Pipes (CEOI15_pipes) C++11
70 / 100
5000 ms 9612 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=100010, inf=2e9;

int n, m;
vector<pii> G[MX];

int U[MX];

pii par[MX];

int dep[MX];

int sz[MX];

int root[MX];


int find(int x){
    return x==U[x] ? x : U[x]=find(U[x]);
}
void unite(int x, int y){
    if(find(x)==find(y)) return;
    U[U[x]]=U[y];
}


void dfs(int v, int p, int d){
    dep[v]=d, root[v]=root[p];
    for(pii &e:G[v]){
        int x=find(e.first);
        if(x==p) continue;
        par[x]={e.second, e.first};
        dfs(x,v,d+1);
    }
}

void merge(int u, int v){
    if(sz[root[find(u)]]<sz[root[find(v)]]) swap(u,v);
    // v(b)를 u(a)밑에 붙인다.
    int a=find(u), b=find(v);
    G[a].push_back({v,u});
    G[b].push_back({u,v});
    sz[root[a]]+=sz[root[b]];
    dfs(b, a, dep[a]+1);
    par[b]={u,v};
}


void lca(int u, int v){
    u=find(u), v=find(v);
    vector<int> A;
    while(u!=v){
        if(dep[u]>dep[v]){
            A.push_back(u);
            u=find(par[u].first);
        }
        else{
            A.push_back(v);
            v=find(par[v].first);
        }
    }
    A.push_back(u);
    int p=find(u), pdep=dep[u]; pii ppar=par[u];
    vector<pii> adj;
    for(int v:A) unite(v,p);
    for(int v:A){
        for(pii &e:G[v]){
            int x=find(e.first);
            if(x==p) continue;
            adj.push_back(e);
        }
        G[v].clear();
    }
    // dep ???
    dep[p]=pdep;
    par[p]=ppar;
    G[p]=adj;
}

void init(){
    for(int i=1; i<=n; i++){
        U[i]=i, par[i]={0,0}, dep[i]=0, sz[i]=1, root[i]=i;
    }
}

void solve(){
    for(int v=1; v<=n; v++){
        if(find(v)!=v) continue;
        if(par[v]==pii(0,0)) continue;
        cout<<par[v].first<<' '<<par[v].second<<'\n';
    }
}

void debug(){
    for(int v=1; v<=n; v++){
        if(find(v)==v){
            cout<<v<<": ";
            for(pii &e:G[v]) cout<<find(e.first)<<' ';
            cout<<'\n';
        }
    }
    cout<<'\n';
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>n>>m;
    init();
    for(int i=1; i<=m; i++){
        int u, v; cin>>u>>v;
        if(find(u)==find(v)) continue;
        if(root[find(u)]==root[find(v)]){
            lca(u, v);
        }
        else{
            merge(u, v);
        }
        // debug();
    }
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 3072 KB Output is correct
2 Correct 9 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 2944 KB Output is correct
2 Correct 94 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 3328 KB Output is correct
2 Correct 192 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 440 ms 4240 KB Output is correct
2 Correct 323 ms 4536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2266 ms 7272 KB Output is correct
2 Correct 620 ms 6936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2984 ms 7836 KB Output is correct
2 Correct 1079 ms 7304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5095 ms 9308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5058 ms 9292 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5060 ms 9612 KB Time limit exceeded
2 Halted 0 ms 0 KB -