Cod sursa(job #2099869)

Utilizator georgerapeanuRapeanu George georgerapeanu Data 4 ianuarie 2018 19:35:59
Problema Componente biconexe Scor 42
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
vector<int> G[100005];
vector<vector<int> > R;
int N,M;
int id[100005],lastid;
int low[100005];
int st[100005];
void dfs(int nod,int tata){
    st[++st[0]] = nod;
    low[nod] = id[nod] = ++lastid;
    for(auto it:G[nod]){
        if(it == tata){
            continue;
        }
        if(!id[it]){
            dfs(it,nod);
            if(low[it] >= id[nod]){
                vector<int> tmp;
                while(st[st[0]] != nod){
                    tmp.push_back(st[st[0]]);
                    st[0]--;
                }
                tmp.push_back(st[st[0]]);
                R.push_back(tmp);
            }
        }
        if(low[it] < low[nod]){
            low[nod] = low[it];
        }
    }
}
int main(){
    freopen("biconex.in","r",stdin);cin.sync_with_stdio(false);cin.tie(0);
    freopen("biconex.out","w",stdout);cout.sync_with_stdio(false);cout.tie(0);
    cin >> N >> M;
    while(M--){
        int x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1,0);
    cout << R.size() << "\n";
    for(auto it:R){
        sort(it.begin(),it.end());
        for(auto it2:it){
            cout << it2 << " ";
        }
        cout << "\n";
    }
    return 0;
}