Submission #96159

# Submission time Handle Problem Language Result Execution time Memory
96159 2019-02-06T14:49:25 Z popovicirobert Pipes (CEOI15_pipes) C++14
50 / 100
2000 ms 39060 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;

map < pair <int, int>, int > mp;

inline void addEdge(int x, int y) {
    g[x].push_back(y);
    g[y].push_back(x);
    mp[{x, y}]++;
    mp[{y, 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];
    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) {
            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) {
        if(mp[it] < 2) {
            cout << it.first << " " << it.second << "\n";
        }
    }
    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2660 KB Output is correct
2 Correct 3 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 4344 KB Output is correct
2 Correct 14 ms 4096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 3960 KB Output is correct
2 Correct 101 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 6184 KB Output is correct
2 Correct 210 ms 5864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 378 ms 11384 KB Output is correct
2 Correct 323 ms 10792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 862 ms 28380 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1221 ms 32088 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1610 ms 39060 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1668 ms 38948 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2000 ms 37172 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -