Cod sursa(job #1164732)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 11:52:07
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.84 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[] = "ctc.in";
const char outfile[] = "ctc.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; }

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) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    }
}

Graph G, Gt;
bitset <MAXN> Used, inStack;
vector <int> tsort, act;
vector <vector <int> > CTC;
int N, M, depth[MAXN], lowLink[MAXN], ind;
stack <int> st;

inline void DFsplus(int Node) {
    Used[Node] = 1;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
        if(!Used[*it])
            DFsplus(*it);
    tsort.push_back(Node);
}

inline void DFsminus(int Node) {
    Used[Node] = 1;
    act.push_back(Node);
    for(It it = Gt[Node].begin(), fin = Gt[Node].end(); it != fin ; ++ it)
        if(!Used[*it])
            DFsminus(*it);
}

inline void Kosaraju() {
    for(int i = 1 ; i <= N ; ++ i)
        if(!Used[i])
            DFsplus(i);
    Used.reset();
    for(vector <int> :: reverse_iterator it = tsort.rbegin(), fin = tsort.rend(); it != fin ; ++ it)
        if(!Used[*it]) {
            vector <int> ().swap(act);
            DFsminus(*it);
            CTC.push_back(act);
        }
    fout << CTC.size() << '\n';
    for(vector <vector <int> > :: iterator comp = CTC.begin(), compfin = CTC.end();
        comp != compfin ; ++ comp) {
            for(vector <int> :: iterator it = comp -> begin(), fin = comp -> end(); it != fin ; ++ it)
                fout << *it << ' ';
            fout << '\n';
        }
}

inline void Tarjan(int Node) {
    depth[Node] = lowLink[Node] = ++ ind;
    st.push(Node);
    inStack[Node] = 1;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
        if(!depth[*it]) {
            Tarjan(*it);
            lowLink[Node] = min(lowLink[Node], lowLink[*it]);
        } else if(inStack[*it]) lowLink[Node] = min(lowLink[Node], lowLink[*it]);
    if(depth[Node] == lowLink[Node]) {
        vector <int> actC;
        int nod;
        do {
            nod = st.top();
            st.pop();
            inStack[nod] = 0;
            actC.push_back(nod);
        } while(nod != Node);
        CTC.push_back(actC);
    }
}

inline void TarjanAlgorithm() {
    for(int i = 1 ; i <= N ; ++ i)
        if(!depth[i])
            Tarjan(i);
    fout << CTC.size() << '\n';
    for(vector <vector <int> > :: iterator comp = CTC.begin(), compfin = CTC.end(); comp != compfin ; ++ comp) {
        for(vector <int> :: iterator it = comp->begin(), fin = comp->end(); it != fin ; ++ it)
            fout << *it << ' ';
        fout << '\n';
    }
}

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);
        Gt[y].push_back(x);
    }
    ///Kosaraju();
    TarjanAlgorithm();
    fin.close();
    fout.close();
    return 0;
}