Cod sursa(job #1116523)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 22 februarie 2014 17:27:12
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.38 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const char infile[] = "biconex.in";
const char outfile[] = "biconex.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

Graph G;
int N, M, dfLevel[MAXN], lowLink[MAXN];
stack < pair<int, int> > St;
vector < set<int> > bxComp;

inline set<int> extractBiconnectedComp( int X, int Y) {
    int Tx, Ty;
    set <int> actComp;
    do {
        Tx = St.top().first;
        Ty = St.top().second;
        St.pop();
        actComp.insert(Tx);
        actComp.insert(Ty);
    } while(Tx != X || Ty != Y);
    return actComp;
}

void DFs(int Node, int Father, int actLevel) {
    dfLevel[Node] = lowLink[Node] = actLevel;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
        if(*it == Father)
            continue;
        if(!dfLevel[*it]) {
            St.push(make_pair(Node, *it));
            DFs(*it, Node, actLevel + 1);
            lowLink[Node] = min(lowLink[Node], lowLink[*it]);
            if(lowLink[*it] >= dfLevel[Node])
                bxComp.push_back(extractBiconnectedComp(Node, *it));
        }
        else lowLink[Node] = min(lowLink[Node], dfLevel[*it]);
    }
}

int main() {
    fin >> N >> M;
    for(int i = 1 ; i <= M ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFs(1, 1, 1);
    fout << bxComp.size() << '\n';
    for(vector <set<int> > :: iterator it = bxComp.begin(), fin = bxComp.end(); it != fin ; ++ it, fout << '\n')
        for(set<int> :: iterator i = it->begin(), j = it->end(); i != j ; ++ i)
            fout << *i << ' ';
    fin.close();
    fout.close();
    return 0;
}