Cod sursa(job #1111085)

Utilizator vasandANDREI POP vasand Data 18 februarie 2014 17:03:29
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# define nmax 100001
using namespace std;

ifstream f("ctc.in");
fstream g("ctc.out", ios::out);

int n,m,vis[nmax], post[nmax], nr, nrc;
vector <int> a[nmax];
vector <int> at[nmax];

void dfs(int u)
{
    vis[u]=1;
    int k,v;
    for(k=0; k<a[u].size(); k++)
    {
        v=a[u][k];
        if(vis[v]==0)
            dfs(v);
    }
    post[++nr]=u;
}

void dfst(int u)
{
    int k,v;
    vis[u]=0;
    g<<u<<" ";
    for(k=0; k<at[u].size(); k++)
    {
        v=at[u][k];
        if(vis[v])
            dfst(v);
    }
}


int main()
{
    a[nmax].reserve(nmax);
    int i,j,x,y;

    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        a[x].push_back(y);
        at[y].push_back(x);
    }

    for(i=1; i<=n; i++)
        if(vis[i]==0)
            dfs(i);

    g<<'\n';
    for(i=n; i>=1; i--)
        if(vis[post[i]])
        {
            dfst(post[i]);
            g<<'\n';
            nrc+=1;
        }
    g.close();
    g.open("ctc.out", ios::in | ios::out);
    g<<nrc;

    return 0;
}