Cod sursa(job #1216248)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 3 august 2014 21:51:35
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.9 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> 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; }

class DirectedGraph {
public:
    DirectedGraph() {

    }
    DirectedGraph(int N) {
        g.resize(N);
        gt.resize(N);
        depth.resize(N);
        lowlink.resize(N);
        inst.resize(N);
        used.resize(N);
        n = N;
    }
    void addEdge(int x, int y) {
        g[x].push_back(y);
        gt[y].push_back(x);
    }
    void dfs(int node) {
        depth[node] = lowlink[node] = ++ index;
        st.push(node);
        inst[node] = 1;
        for(It it = g[node].begin(), fin = g[node].end() ; it != fin ; ++ it) {
            if(!depth[*it]) {
                dfs(*it);
                lowlink[node] = min(lowlink[node], lowlink[*it]);
            } else if(inst[*it])
                    lowlink[node] = min(lowlink[node], lowlink[*it]);
        }
        if(depth[node] == lowlink[node]) {
            int act;
            vector <int> comp;
            do {
                act = st.top();
                inst[act] = 0;
                st.pop();
                comp.push_back(act);
            } while(act != node);
            ctc.push_back(comp);
        }
    }
    vector <vector <int> >  Tarjan() {
        for(int i = 0 ; i < n ; ++ i)
            if(!depth[i])
                dfs(i);
        return ctc;
    }
    void tsort(int node) {
        used[node] = 1;
        for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it)
            if(!used[*it])
                tsort(*it);
        st.push(node);
    }
    void dfsrev(int node, vector <int> &act) {
        used[node] = 0;
        for(It it = gt[node].begin(), fin = gt[node].end(); it != fin ; ++ it)
            if(used[*it])
                dfsrev(*it, act);
        act.push_back(node);
    }
    vector <vector <int> > Kosaraju() {
        for(int i = 0 ; i < n ; ++ i)
            if(!used[i])
                tsort(i);
        vector <int> act;
        while(!st.empty()) {
            if(used[st.top()]) {
                dfsrev(st.top(), act);
                ctc.push_back(act);
                vector <int>().swap(act);
            }
            st.pop();
        }
        return ctc;
    }
private:
    vector <vector <int> > g;
    vector <vector <int> > ctc;
    /// Tarjan
    vector <int> depth, lowlink;
    stack<int> st; /// this is for both :)
    vector <bool> inst;
    int index;
    /// Kosaraju
    vector <bool > used;
    vector <vector <int> > gt;
    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 = DirectedGraph(n);
    for( ; m -- ; ) {
        int x, y;
        fin >> x >> y;
        -- x;
        -- y;
        G.addEdge(x, y);
    }
    vector <vector <int> > ctc = G.Kosaraju();
    fout << ctc.size() << '\n';
    for(auto comp: ctc) {
        for(auto it : comp)
            fout << it + 1 << ' ';
        fout << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}