Submission #472861

# Submission time Handle Problem Language Result Execution time Memory
472861 2021-09-14T12:18:07 Z Carmel_Ab1 Pipes (CEOI15_pipes) C++17
0 / 100
5000 ms 13680 KB
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;

#define all(x) x.begin(),x.end()
#define out(x) {cout << x << "\n"; return;}
#define GLHF ios_base::sync_with_stdio(0); cin.tie(0)
#define pb push_back

void solve();
int main(){
    GLHF;
    solve();
}
vi up;
vi par,rnk,dep,par2;

int get(int x) { return par[x] = (x == par[x] ? x : get(par[x])); }
int get2(int x){return par2[x]=(x==par2[x]?x:get2(par2[x]));}
void unite2(int u,int v){
    u = get2(u), v = get2(v);
    if (u == v)return;
    if(dep[u]>dep[v])
        swap(u,v);
    par2[v] = u;
}
void unite(int u, int v) {//u in new leader
    u = get(u), v = get(v);
    if (u == v)return;
    if (rnk[u] > rnk[v])
        swap(u, v);
    par[u] = v;
    rnk[v] += rnk[u];
}
vector<set<int>> inv;//inv[i]= {j s.t par[j]=i} = {kids of i}

set<pi>ans;
void dfs(int src,int p=0){
    //dep is maintain-able

    if(src==-1  )
        return;
    dfs(up[src],src);
    up[src]=p;
    if(p)
        dep[src]=dep[p]+1;
    inv[src].erase(p);
    inv[p].insert(src);

}
void add_edge(int u,int v){
    ans.insert({min(u,v),max(u,v)});
    if(rnk[get(u)]<rnk[get(v)])
        swap(u,v);
    //deg[u]>deg[v], re-root at v
    dep[v]=dep[u]+1;
    dfs(v);
    up[v]=u;
    inv[u].insert(v);
}
void solve(){
    int n,m;
    cin >> n >> m;
    up.resize(n+1,-1);
    dep.resize(n+1);
    par.resize(n+1);
    rnk.resize(n+1,1);
    par2.resize(n+1);
    for(int i=0; i<=n; i++)
        par[i]=i,par2[i]=i;
    inv.resize(n+1);

    for(int i=0; i<m; i++){
        int u,v;
        cin >> u >> v;
        if(u==v)
            continue;
        if(get(u)!=get(v)){
            add_edge(u,v);
            unite(u,v);
            continue;
        }

        if(dep[u]>dep[v])
            swap(u,v);

        while(up[v]!=-1 && dep[u]<dep[v]){
            ans.erase({min(up[v],v),max(v,up[v])});
            unite2(up[v],v);
            v=get2(v);
        }

        while(up[u]!=-1 && up[v]!=-1 && get2(u)!=get2(v)){
            ans.erase({min(up[v],v),max(v,up[v])});
            unite2(up[v],v);
            v=get2(v);
            ans.erase({min(up[u],u),max(u,up[u])});
            unite2(up[u],u);
            u=get2(u);
        }

    }

    for(pi p:ans)
        cout << p.first << " " << p.second << "\n";


}
# Verdict Execution time Memory Grader output
1 Execution timed out 5069 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5061 ms 844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5053 ms 716 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5044 ms 1612 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5042 ms 3404 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5052 ms 9796 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5056 ms 10568 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5073 ms 13680 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5063 ms 13676 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5013 ms 12868 KB Time limit exceeded
2 Halted 0 ms 0 KB -