Submission #96160

# Submission time Handle Problem Language Result Execution time Memory
96160 2019-02-06T14:51:51 Z popovicirobert Pipes (CEOI15_pipes) C++14
100 / 100
1333 ms 15256 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

struct DSU {
    vector <int> par;
    stack <int> stk;
    int n;
    inline void init(int _n) {
        n = _n;
        par.resize(n + 1);
    }
    int root(int x) {
        if(par[x] == 0)
            return x;
        return par[x] = root(par[x]);
    }
    inline void join(int x, int y) {
        x = root(x), y = root(y);
        if(x != y) {
            stk.push(x);
            par[x] = y;
        }
        else {
            stk.push(-1);
        }
    }
    inline void undo() {
        if(stk.top() != -1) {
            par[stk.top()] = 0;
        }
        stk.pop();
    }
};

const int MAXN = (int) 1e5;

vector <int> g[MAXN + 1];
stack <int> stk;

inline void addEdge(int x, int y) {
    g[x].push_back(y);
    g[y].push_back(x);
}

int lvl[MAXN + 1], low[MAXN + 1];
vector < pair <int, int> > sol;

void dfs(int nod, int par) {
    stk.push(nod);
    lvl[nod] = lvl[par] + 1;
    low[nod] = lvl[nod];
    int cnt = 0;
    for(auto it : g[nod]) {
        if(lvl[it] == 0) {
            dfs(it, nod);
            low[nod] = min(low[nod], low[it]);
            if(low[it] == lvl[it]) {
                while(stk.top() != it) {
                    stk.pop();
                }
                stk.pop();
                sol.push_back({nod, it});
            }
        }
        else {
            if(it == par) {
                if(cnt == 1) {
                    low[nod] = min(low[nod], lvl[it]);
                }
                cnt++;
            }
            else {
                low[nod] = min(low[nod], lvl[it]);
            }
        }
    }
}

int main() {
    //ifstream cin("B.in");
    //ofstream cout("B.out");
    int i, n, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    DSU dsu1, dsu2;
    dsu1.init(n);
    dsu2.init(n);
    for(i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        if(dsu1.root(x) == dsu1.root(y)) {
            if(dsu2.root(x) != dsu2.root(y)) {
                dsu2.join(x, y);
                addEdge(x, y);
            }
        }
        else {
            dsu1.join(x, y);
            addEdge(x, y);
        }
    }
    for(i = 1; i <= n; i++) {
        if(lvl[i] == 0) {
            dfs(i, 0);
        }
    }
    for(auto it : sol) {
        cout << it.first << " " << it.second << "\n";
    }
    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3200 KB Output is correct
2 Correct 7 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 3064 KB Output is correct
2 Correct 95 ms 2932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 3920 KB Output is correct
2 Correct 187 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 5612 KB Output is correct
2 Correct 252 ms 5308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 434 ms 11644 KB Output is correct
2 Correct 422 ms 8116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 648 ms 12804 KB Output is correct
2 Correct 591 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 871 ms 15256 KB Output is correct
2 Correct 813 ms 10348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1060 ms 15232 KB Output is correct
2 Correct 1014 ms 10292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1333 ms 14600 KB Output is correct
2 Correct 1211 ms 11636 KB Output is correct