Cod sursa(job #1214591)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 30 iulie 2014 21:29:16
Problema Componente biconexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.92 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 <stack>
#include <deque>

using namespace std;

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

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

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

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; }

/// BICONE XXX

class Graph {
public:
    Graph() {

    }
    Graph(int n) {
        g.resize(n);
        level.resize(n);
        lowlink.resize(n);
        this->n = n;
    }
    void addEdge(int x, int y) {
        g[x].push_back(y);
        g[y].push_back(x);
    }
    set<int> extractBCC(int x, int y) {
        int Tx, Ty;
        set <int> ret;
        do {
            Tx = st.top().first;
            Ty = st.top().second;
            st.pop();
            ret.insert(Tx);
            ret.insert(Ty);
        } while(Tx != x || Ty != y);
        return ret;
    }
    void dfs(int node, int actlevel, int father) {
        level[node] = lowlink[node] = actlevel;
        for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it) {
            if(*it == father)
                continue;
            if(!level[*it]) {
                st.push(make_pair(node, *it));
                dfs(*it, actlevel + 1, node);
                lowlink[node] = min(lowlink[node], lowlink[*it]);
                if(level[node] <= lowlink[*it])
                    bcc.push_back(extractBCC(node, *it));
            } else
                lowlink[node] = min(lowlink[node], level[*it]);
        }
    }
    void buildBCC() {
        dfs(0, 1, -1);
    }
    vector <set <int> > getBCC() {
        buildBCC();
        return bcc;
    }
private:
    vector <vector <int> > g;
    vector <int> level;
    vector <int> lowlink;
    vector <set <int> > bcc;
    stack <pair<int, int> > st;
    int n;
} G;

int main() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    int n, m;
    fin >> n >> m;
    G = Graph(n);
    while(m --) {
        int x, y;
        fin >> x >> y;
        -- x; -- y;
        G.addEdge(x, y);
    }
    vector <set <int> > bcc = G.getBCC();
    fout << bcc.size() << '\n';
    for(auto comp: bcc) {
        for(auto it : comp)
            fout << it + 1 << ' ';
        fout << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}