Cod sursa(job #3295210)

Utilizator BucsMateMate Bucs BucsMate Data 3 mai 2025 16:20:51
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.16 kb
/*#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

ifstream fin("biconex.in");
ofstream fout("biconex.out");

stack<pair<int, int>> st;
int t = 1;
vector<vector<int>> megoldas;

const int INF = 1000*1000*1000;


void DFS(vector<vector<int>> &adj, int node, vector<int> &low, vector<int> &disc, vector<int> &parent, vector<vector<int>> &adj_new)
{
    disc[node] = t;
    low[node] = t;
    t++;
    int children = 0;


    for(int i = 0; i < adj[node].size(); i++){
        int newNode = adj[node][i];

        if(newNode == parent[node]){
            continue;
        }
        if(disc[newNode] == INF){
            st.push({node, newNode});
            DFS(adj, newNode, low, disc, parent, adj_new);

            low[node] = min(low[node], low[newNode]);
            if(low[newNode] >= disc[node]){
                pair<int, int> curr = st.top();
                st.pop();
                vector<int> temp;
                temp.push_back(curr.first);
                temp.push_back(curr.second);
                while(curr.first != node || curr.second != newNode){
                    curr = st.top();
                    st.pop();
                    temp.push_back(curr.first);
                }
                megoldas.push_back(temp);

            }
        }
        else{
            low[node] = min(low[node], disc[newNode]);
        }
    }
}

int main()
{
    int N, M;
    fin >> N >> M;
    vector<vector<int>> adj(N+1), adj_new(N+1);

    for(int i = 0; i < M; i++){
        int a, b;
        fin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    vector<int> disc(N+1, INF);
    vector<int> low(N+1, INF);
    vector<int> parent(N+1, -1);
    for(int i = 1; i <= N; i++){
        t = 1;
        if(disc[i] == INF){
            DFS(adj, i, low, disc, parent, adj_new);
        }
    }


    fout << megoldas.size() << endl;
    for(int i = 0; i < megoldas.size(); i++){
        sort(megoldas[i].begin(), megoldas[i].end());
        for(int j = 0; j < megoldas[i].size(); j++){
            fout << megoldas[i][j] << " ";
        }
        fout << endl;
    }
    return 0;
}
*/



#include <fstream>
#include <iostream>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>

using namespace std;

const char iname[] = "biconex.in";
const char oname[] = "biconex.out";

#define MAXN  100005
#define Min(a, b)  ((a) < (b) ? (a) : (b))

vector <int> adj[MAXN], dfn, low;

vector <vector <int> > C;

stack <pair <int, int> > stk;

void read_in(vector <int>* adj, int &n)
{
    ifstream in(iname);
    int cnt_edges, x, y;

    in >> n >> cnt_edges;
    for (; cnt_edges > 0; -- cnt_edges) {
        in >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    in.close();
}

void cache_bc(const int x, const int y)
{
    vector <int> con; int tx, ty;
    do {
        tx = stk.top().first, ty = stk.top().second;
        stk.pop();
        con.push_back(tx), con.push_back(ty);
    }
    while (tx != x || ty != y);
    C.push_back(con);
}

void DF(const int n, const int fn, int number)
{
    vector <int>::iterator it;

    dfn[n] = low[n] = number;
    for (it = adj[n].begin(); it != adj[n].end(); ++ it) {
        if (*it == fn)   continue ;
        if (dfn[*it] == -1) {
            stk.push( make_pair(n, *it) );
            DF(*it, n, number + 1);
            low[n] = Min(low[n], low[*it]);
            if (low[*it] >= dfn[n])
                cache_bc(n, *it);
        }
        else
            low[n] = Min(low[n], dfn[*it]);
    }
}

int main(void)
{
    int n;
    read_in(adj, n);
    dfn.resize(n + 1), dfn.assign(n + 1, -1);
    low.resize(n + 1);
    DF(1, 0, 0);

    ofstream out(oname);
    out << C.size() << "\n";
    for (size_t i = 0; i < C.size(); ++ i) {
        sort(C[i].begin(), C[i].end());
        C[i].erase(unique(C[i].begin(), C[i].end()), C[i].end());
        for (size_t j = 0; j < C[i].size(); ++ j)
            out << C[i][j] << " ";
        out << "\n";
    }
    out.close();

    return 0;
}