Submission #873835

# Submission time Handle Problem Language Result Execution time Memory
873835 2023-11-15T21:48:55 Z tvladm2009 Pipes (CEOI15_pipes) C++17
80 / 100
879 ms 65536 KB
#include <bits/stdc++.h>

using i64 = long long;
struct DSU {
    std::vector<int> f;

    DSU() {}
    DSU(int n) {
        init(n);
    }

    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
    }

    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        f[y] = x;
        return true;
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<int>> adj(n + 1);
    DSU t1(n), t2(n);
    int cnt = 0;
    while (m--) {
        int u, v;
        std::cin >> u >> v;
        if (t1.merge(u - 1, v - 1) == true) {
            adj[u].push_back(v);
            adj[v].push_back(u);
            cnt++;
        } else if (t2.merge(u - 1, v - 1) == true) {
            adj[u].push_back(v);
            adj[v].push_back(u);
            cnt++;
        }
    }
    assert(cnt <= 2 * n);

    std::vector<int> level(n + 1, -1);
    auto dfs = [&](auto self, int u, int par) -> int {
        int low = level[u];
        bool p = false;
        for (auto v : adj[u]) {
            if (level[v] == -1) {
                level[v] = level[u] + 1;
                low = std::min(low, self(self, v, u));
            } else if (v == par && p == false) {
                p = true;
            } else {
                low = std::min(low, level[v]);
            }
        }
        if (par != -1 && low == level[u]) {
            std::cout << par << " " << u << "\n";
        }
        return low;
    };
    for (int i = 1; i <= n; i++) {
        if (level[i] == -1) {
            level[i] = 0;
            dfs(dfs, i, -1);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 860 KB Output is correct
2 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 708 KB Output is correct
2 Correct 64 ms 692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 1620 KB Output is correct
2 Correct 129 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 199 ms 3428 KB Output is correct
2 Correct 172 ms 3152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 9548 KB Output is correct
2 Correct 234 ms 6128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 417 ms 11080 KB Output is correct
2 Correct 411 ms 8096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 540 ms 13684 KB Output is correct
2 Correct 511 ms 8912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 636 ms 13684 KB Output is correct
2 Runtime error 679 ms 61440 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 804 ms 12808 KB Output is correct
2 Runtime error 879 ms 65536 KB Memory limit exceeded
3 Halted 0 ms 0 KB -