Submission #503876

# Submission time Handle Problem Language Result Execution time Memory
503876 2022-01-09T06:14:46 Z CraniXort Pipes (CEOI15_pipes) C++17
30 / 100
2231 ms 65540 KB
#include <bits/stdc++.h>
#define maxn 200005

#define fin std::cin
#define fout std::cout

// std::ifstream fin("pipes.in");
// std::ofstream fout("pipes.out");

std::vector <int> edge[maxn];
int disc[maxn], low[maxn], timer;
bool visited[maxn];
std::vector <std::pair <int, int>> bridges;

void dfs(int node, int parent) {
    visited[node] = true;
    disc[node] = low[node] = ++timer;

    bool ok = false;

    for(auto next: edge[node]) {
        if(next == parent and ok == false) {
            ok = true;
            continue;
        }
        if(visited[next] == false) {
            dfs(next, node);
            low[node] = std::min(low[node], low[next]);

            if(low[next] > disc[node]) {
                bridges.push_back({node, next});
            }
        }
        else{
            low[node] = std::min(low[node], disc[next]);
        }
    }
}

class DSU {
    int size;
    std::vector <int> rank, parent;

    public:
    DSU(int size): size(size) {
        rank.resize(size+1);
        parent.resize(size+1);

        for(int i = 1; i <= size; i ++)
            parent[i] = i;
    }

    int findRoot(int node) {
        if(parent[node] == node)
            return node;
        return parent[node] = findRoot(parent[node]);
    }

    void combine(int node1, int node2) {
        node1 == findRoot(node1);
        node2 == findRoot(node2);
        if(node1 == node2)
            return;

        if(rank[node1] < rank[node2])
            std::swap(node1, node2);

        parent[node2] = node1;

        if(rank[node1] == rank[node2])
            rank[node1] ++;
    }

    bool sameComponent(int node1, int node2) {
        node1 = findRoot(node1);
        node2 = findRoot(node2);
        return node1 == node2;
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int V, E;
    fin >> V >> E;

    DSU first(V), second(V);

    for(int i = 0, src, dest; i < E; i ++) {
        fin >> src >> dest;

        if(first.sameComponent(src, dest) == false) {
            edge[src].push_back(dest);
            edge[dest].push_back(src);
            first.combine(src, dest);
        }
        else if(second.sameComponent(src, dest) == false) {
            edge[src].push_back(dest);
            edge[dest].push_back(src);
            second.combine(src, dest);
        }
    }

    for(int i = 1; i <= V; i ++)
        if(visited[i] == false)
            dfs(i, -1);

    for(auto i: bridges)
        fout << i.first << ' ' << i.second << '\n';

    return 0;
}

Compilation message

pipes.cpp: In member function 'void DSU::combine(int, int)':
pipes.cpp:60:15: warning: value computed is not used [-Wunused-value]
   60 |         node1 == findRoot(node1);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~
pipes.cpp:61:15: warning: value computed is not used [-Wunused-value]
   61 |         node2 == findRoot(node2);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB Output is correct
2 Correct 4 ms 5012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5708 KB Output is correct
2 Correct 5 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 7804 KB Output is correct
2 Correct 109 ms 6716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 12196 KB Output is correct
2 Runtime error 213 ms 20940 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Runtime error 392 ms 23124 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 697 ms 35860 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1159 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1640 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2087 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2231 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -