Cod sursa(job #2195512)

Utilizator horiahoria1Horia Alexandru Dragomir horiahoria1 Data 16 aprilie 2018 16:48:51
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.89 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>

void get_bcc(int node, int neighbour, std::stack<std::pair<int, int>> &components, std::vector<std::vector<int>> &result, std::ofstream &out) {
    std::vector<int> new_bcc;
    while (components.size() > 1  && (components.top().first != node || components.top().second != neighbour)) {
        new_bcc.push_back(components.top().first);
        new_bcc.push_back(components.top().second);
        components.pop();
    }
    new_bcc.push_back(components.top().first);
    new_bcc.push_back(components.top().second);
    components.pop();
    std::sort(new_bcc.begin(), new_bcc.end());
    new_bcc.erase(std::unique(new_bcc.begin(), new_bcc.end()), new_bcc.end());
    result.push_back(new_bcc);
}

void dfs(int node, std::vector<int> *adj, std::vector<int> &index, std::vector<int> &topmost, std::vector<int> &parent, std::stack<std::pair<int, int>> &components, int &count, int &number, std::vector<std::vector<int>> &result, std::ofstream &out) {
    index[node] = topmost[node] = count++;
    int children = 0;
    for (auto &neighbour : adj[node]) {
        if (index[neighbour] == -1) {
            parent[neighbour] = node;
            ++children;
            components.push({node, neighbour});
            dfs(neighbour, adj, index, topmost, parent, components, count, number, result, out);
            topmost[node] = std::min(topmost[node], topmost[neighbour]);
            if ((parent[node] == -1 && children >= 2) || (parent[node] != -1 && topmost[neighbour] >= index[node])) {
                get_bcc(node, neighbour, components, result, out);
                ++number;
            }
        } else if (neighbour != parent[node] && index[neighbour] < topmost[node]) {
            topmost[node] = std::min(topmost[node], index[neighbour]);
            components.push({node, neighbour});
        }
    }
}

int main() {

    std::ifstream in("biconex.in");
    std::ofstream out("biconex.out");

    int n, m;
    in >> n >> m;

    std::vector<int> adj[n + 1];

    int i;
    int u, v;

    for (i = 0; i < m; ++i) {
        in >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    std::stack<std::pair<int, int>> components;

    std::vector<int> parent(n + 1, -1);

    std::vector<int> index(n + 1, -1);
    std::vector<int> topmost(n + 1);

    int count = 1;
    int number = 0;

    std::vector<std::vector<int>> result;

    for (i = 1; i <= n; ++i) {
        if (index[i] == -1) {
            dfs(i, adj, index, topmost, parent, components, count, number, result, out);
            if (!components.empty()) {
                get_bcc(-1, -1, components, result, out);
                ++number;
            }
        }
    }

    out << number << '\n';
    for (auto &bcc : result) {
        for (auto &node : bcc) {
            out << node << ' ';
        }
        out << '\n';
    }

    in.close();
    out.close();

    return 0;
}