Cod sursa(job #1094799)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 29 ianuarie 2014 21:09:52
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.47 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 <deque>
#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; }

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

inline void extractComponents(const int &Node, const int &Ancestor) {
    int Tx;
    int Ty;
    set <int> actComp;
    do {
        Tx = St.top().first;
        Ty = St.top().second;
        St.pop();
        actComp.insert(Tx);
        actComp.insert(Ty);
    } while(Tx != Node || Ty != Ancestor);
    biconnectedComponents.push_back(actComp);
}

void DFs(const int &Node, const int &Father, const 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);
            Get_min(lowLink[Node], lowLink[*it]);
            if(lowLink[*it] >= dfLevel[Node])
                extractComponents(Node, *it);
        }
        else Get_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, 0, 1);
    fout << biconnectedComponents.size() << '\n';
    for(vector <set <int> > :: iterator it = biconnectedComponents.begin(); it != biconnectedComponents.end();
    ++ it, fout << '\n')
        for(set<int> :: iterator i = it -> begin(); i != it->end(); ++ i)
            fout << *i << ' ';
    fin.close();
    fout.close();
    return 0;
}