Cod sursa(job #1290550)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 decembrie 2014 14:38:54
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.43 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "biconex";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = 666013;

const int NMAX = 100000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;

int N, M, nrCB;
vector<int> V[NMAX];
vector<int> CB[NMAX];
vector<int> S;
int depth[NMAX];
int low[NMAX];
int dad[NMAX];

void get_biconex(int x, int y) {
    nrCB++;

    while(S.back() != y) {
        CB[nrCB].push_back(S.back());
        S.pop_back();
    }

    CB[nrCB].push_back(x);
    CB[nrCB].push_back(y);

    S.pop_back();
}

void dfs(int x, int d, int f) {
    depth[x] = d;
    low[x] = d;
    dad[x] = f;

    for(auto y : V[x]) {
        if(y == dad[x])
            continue;

        if(!depth[y]) {
            S.push_back(y);
            dfs(y, d + 1, x);
            if(low[y] >= depth[x])
                get_biconex(x, y);
            low[x] = min(low[x], low[y]);
        }

        low[x] = min(low[x], depth[y]);
    }
}

int main() {
    int i, x, y;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d%d", &N, &M);

    while(M--) {
        scanf("%d%d", &x, &y);
        V[x].push_back(y);
        V[y].push_back(x);
    }

    dfs(1, 1, 0);

    printf("%d\n", nrCB);

    for(i = 1; i <= nrCB; i++) {
        for(auto x : CB[i])
            printf("%d ", x);
        printf("\n");
    }

    return 0;
}