Cod sursa(job #1379281)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 6 martie 2015 17:16:34
Problema Componente biconexe Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.12 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define MAXN 100010

#ifndef ONLINE_JUDGE
FILE *in = fopen("biconex.in", "r");
FILE *out = fopen("biconex.out", "w");
#endif

int n, m;
vector<int> vec[MAXN];
vector<int> rnk, low;
stack<pair<int, int> > stk;
vector<vector<int> > C;

void newComp(int x, int y) {
    int tx, ty;
    vector<int> com;
    do {
        tx = stk.top().fs; ty = stk.top().sc;
        if(find(com.begin(), com.end(), tx) == com.end())
            com.pub(tx);
        if(find(com.begin(), com.end(), ty) == com.end())
            com.pub(ty);
        stk.pop();
    } while (tx != x || ty != y);
    C.pub(com);
}

void DFS(int nod, int last, int val) {
    rnk[nod] = low[nod] = val;
    for(auto it: vec[nod])
        if(it != last) {
            if(rnk[it] == -1) {
                stk.push(mp(nod, it));
                DFS(it, nod, val + 1);
                low[nod] = min(low[it], low[nod]);
                if(low[it] >= rnk[nod])
                    newComp(nod, it);
            } else
                low[nod] = min(low[nod], rnk[it]);
        }
}

int main() {
    int x, y;

    fscanf(in, "%d%d", &n, &m);
    for(int i = 0; i < m; ++i) {
        fscanf(in, "%d%d", &x, &y);
        vec[x].pub(y);
        vec[y].pub(x);
    }

    rnk.resize(n + 1); rnk.assign(n + 1, -1);
    low.resize(n + 1);

    DFS(1, 0, 0);

    fprintf(out, "%d\n", C.size());
    for(auto it: C) {
        for(auto jt: it) {
            fprintf(out, "%d ", jt);
        }
        fprintf(out, "\n");
    }

    return 0;
}