Cod sursa(job #1164800)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 12:19:31
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.73 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; }

vector <set<int> > BCC;
Graph G;
int depth[MAXN], lowLink[MAXN], N, M;
stack <pair<int, int> > st;

inline void extractBCC(int x, int y) {
    int tx, ty;
    set <int> act;
    do {
        tx = st.top().first;
        ty = st.top().second;
        st.pop();
        act.insert(tx);
        act.insert(ty);
    } while(x != tx || y != ty);
    BCC.push_back(act);
}

inline void DFs(int Node, int Father, int actLevel) {
    depth[Node] = lowLink[Node] = actLevel;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
        if(*it == Father)
            continue;
        if(!depth[*it]) {
            st.push(make_pair(Node, *it));
            DFs(*it, Node, actLevel + 1);
            lowLink[Node] = min(lowLink[Node], lowLink[*it]);
            if(lowLink[*it] >= depth[Node])
                extractBCC(Node, *it);

        } else lowLink[Node] = min(lowLink[Node], depth[*it]);
    }
}

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void get(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    }
}

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