Submission #96158

# Submission time Handle Problem Language Result Execution time Memory
96158 2019-02-06T14:39:05 Z popovicirobert Pipes (CEOI15_pipes) C++14
20 / 100
1248 ms 14440 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];
    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) {
        cout << it.first << " " << it.second << "\n";
    }
    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Incorrect 4 ms 2688 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3200 KB Output is correct
2 Incorrect 7 ms 3072 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 101 ms 3068 KB Output is correct
2 Incorrect 108 ms 2936 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Incorrect 169 ms 3796 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 304 ms 5484 KB Output is correct
2 Correct 267 ms 5276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 473 ms 10872 KB Output is correct
2 Incorrect 438 ms 7928 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 702 ms 12152 KB Output is correct
2 Incorrect 683 ms 9464 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 930 ms 14236 KB Output is correct
2 Incorrect 850 ms 10164 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 1057 ms 14440 KB Output is correct
2 Incorrect 1044 ms 10232 KB Wrong number of edges
# Verdict Execution time Memory Grader output
1 Correct 1248 ms 13816 KB Output is correct
2 Correct 1212 ms 11164 KB Output is correct