Cod sursa(job #2488932)

Utilizator bigmixerVictor Purice bigmixer Data 7 noiembrie 2019 20:00:05
Problema Componente biconexe Scor 46
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.06 kb
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz() size()
#define fr first
#define sc second
#define pb push_back
#define er erase
#define in insert
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
#define mp make_pair
//#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
///#define cin fin
///#define cout fout
using namespace std;

const int mod=1e9+7;

int n,m,x,y,viz[100005],level[100005],low[100005],ans=-1;
vector<int>nod[100005];
stack<int>stiva;
vector<int>rs[100005];

void DFS(int s,int par,int k){
    viz[s]=1;
    level[s]=k;
    low[s]=k;
    stiva.push(s);
    for(auto it:nod[s]){
        if(it!=par){
            if(viz[it]==1){
                low[s]=min(low[s],level[it]);
            }
            else{
                DFS(it,s,k+1);
                low[s]=min(low[s],low[it]);
                if(low[it]>=level[s]) {
                    ans++;
                    int curr=stiva.top();
                    while(true){
                        rs[ans].push_back(curr);
                        if(curr==s) break;
                        stiva.pop();
                        curr=stiva.top();
                    }
                }
            }
        }
    }
}

int32_t main(){
	//ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
    srand(chrono::steady_clock::now().time_since_epoch().count());
    ifstream fin("biconex.in");
    ofstream fout("biconex.out");
    fin >> n >> m;
    for(int i=1;i<=m;i++){
        fin >> x >> y;
        nod[x].push_back(y);
        nod[y].push_back(x);
    }
    DFS(1,0,1);
    fout << ans+1 << '\n';
    for(int i=0;i<=ans;i++){
        for(int j=0;j<rs[i].size();j++){
            fout << rs[i][j] << ' ';
        }
        fout << '\n';
    }
}


/**

8 9
1 2
2 3
3 4
4 1
1 5
5 6
6 7
7 5
7 8

*/