Cod sursa(job #375210)

Utilizator alexandru92alexandru alexandru92 Data 19 decembrie 2009 20:25:53
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on December 19, 2009, 7:28 PM
 */
#include <stack>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>
#define pb push_back

/*
 * 
 */
using namespace std;
int counting;
vector<int> df, uz;
vector< vector<int> > v, vt, tc;
void DFS( int x )
{vector<int>::const_iterator it=v[x].begin(), iend=v[x].end();
    uz[x]=1;
    for( ; it < iend; ++it )
        if( !uz[*it] )
            DFS(*it);
   df.pb(x);
}
void DFST( int x )
{vector<int>::const_iterator it=vt[x].begin(), iend=vt[x].end();
    uz[x]=0;
    for( ; it < iend; ++it )
        if( uz[*it] )
            DFST(*it);
    tc[counting-1].pb(x+1);
}
int main()
{int n, m, x, y, i;
    ifstream in("ctc.in");
    in>>n>>m;
    v.resize(n);
    vt.resize(n);
    uz.resize(n);
    while( m-- )
    {
        in>>x>>y;
        v[x-1].pb(y-1);
        vt[y-1].pb(x-1);
    }
    for( i=0; i < n; ++i )
        if( !uz[i] )
            DFS(i);
    for( i=n-1; i >= 0; --i )
        if( uz[df[i]] )
        {++counting;
            tc.resize(counting);
            DFST(df[i]);
        }
    ofstream out("ctc.out");
    out<<counting<<'\n';
    for( i=0; i < counting; ++i )
    {
        copy( tc[i].begin(), tc[i].end(), ostream_iterator<int>(out, " ") );
        out<<'\n';
    }
    return 0;
}