Cod sursa(job #2150305)

Utilizator netfreeAndrei Muntean netfree Data 3 martie 2018 14:09:00
Problema Componente biconexe Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;

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

const int N_MAX = 100000 + 5;
vector<int> vec[N_MAX];
bitset<N_MAX> viz;
stack<pii> st;
int n, m;
int jump[N_MAX]; // adancimea maxima la care pot ajunge dintr-un nod din subarborele lui cu o singura muchie de intoarcere
int level[N_MAX];

vector<set<int> > ans;
set<int> s;

void popstack(int node, int son) {
  int x, y;
  do {
    x = st.top().x;
    y = st.top().y;
    st.pop();
    s.insert(x);
    s.insert(y);
  } while (not (node == x and son == y)); //pana nu am dat de muchia care imi "tine" ultimul v
  ans.push_back(s);
  s.clear();
}

void dfs(int node, int father){
    viz[node] = true;
    level[node] = level[father] + 1;
    jump[node] = level[node];
    for(auto v : vec[node])
        if(!viz[v]){
            st.push({node, v});
            dfs(v, node);
            jump[node] = min(jump[node], jump[v]);
            if(jump[v] >= level[node]){ /// node este un punct de articulatie
                popstack(node, v);
                //cout << node << " ";
            }
        } else if (viz[v] and v != father){
            /// o muchie de intoarcere din nodul curent (pentru care am calculat deja subarboarele)
            jump[node] = min(jump[node], level[v]);
        }
}

int main()
{
    fin >> n >> m;
    while(m--){
        int x, y;
        fin >> x >> y;
        vec[x].push_back(y);
        vec[y].push_back(x);
    }

    dfs(1, 0);

    fout << ans.size() << "\n";
    for (auto s : ans) {
        for (auto i : s)
            fout << i << " ";
    fout << "\n";
    }

    return 0;
}