Submission #658134

# Submission time Handle Problem Language Result Execution time Memory
658134 2022-11-12T09:11:11 Z FatihSolak Pipes (CEOI15_pipes) C++17
70 / 100
1010 ms 28900 KB
#include <bits/stdc++.h>
#define N 200005
using namespace std;
struct DSU{
    vector<int> par;
    void init(int n){
        par.assign(n+1,0);
        for(int i = 1;i<=n;i++){
            par[i] = i;
        }
    }
    int find(int a){
        if(a == par[a])return a;
        return par[a] = find(par[a]);
    }
    bool merge(int a,int b){
        a = find(a);
        b = find(b);
        if(a == b)
            return 0;
        par[a] = b;
        return 1;
    }
};
int dep[N];
int mini[N];
bool vis[N];
vector<int> adj[N];
void dfs(int v,int par){
    vis[v] = 1;
    mini[v] = dep[v];
    bool ok = 0;
    for(auto u:adj[v]){
        if(u == par && !ok){
            ok = 1;
            continue;
        }
        if(!vis[u]){
            dep[u] = dep[v] + 1;
            dfs(u,v);
            mini[v] = min(mini[v],mini[u]);
        }
        else{
            mini[v] = min(mini[v],dep[u]);
        }
    }
    if(par != 0 && dep[v] == mini[v]){
        cout << par << ' ' << v << '\n';
    }
}
void solve(){
    int n,m;
    cin >> n >> m;
    DSU d1,d2;
    d1.init(n);
    d2.init(n);
    for(int i = 1;i<=m;i++){
        int a,b;
        cin >> a >> b;
        if(d1.merge(a,b)){
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        else if(d2.merge(a,b)){
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
    }
    for(int i = 1;i<=n;i++){
        if(!vis[i])
            dfs(i,0);
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef Local
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    #ifdef Local
    cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5460 KB Output is correct
2 Correct 5 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 5316 KB Output is correct
2 Correct 77 ms 5180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 6120 KB Output is correct
2 Runtime error 178 ms 16972 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 232 ms 7692 KB Output is correct
2 Runtime error 209 ms 21144 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 321 ms 13004 KB Output is correct
2 Correct 300 ms 10784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 516 ms 14204 KB Output is correct
2 Correct 468 ms 11376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 638 ms 16332 KB Output is correct
2 Correct 635 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 822 ms 16336 KB Output is correct
2 Correct 783 ms 15412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1008 ms 15836 KB Output is correct
2 Runtime error 1010 ms 28900 KB Memory limit exceeded
3 Halted 0 ms 0 KB -