Cod sursa(job #2144621)

Utilizator alexandruilieAlex Ilie alexandruilie Data 26 februarie 2018 20:38:56
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>
#include <string.h>
#define nmax 100001
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
vector <int> v[nmax];
vector <int> v1[nmax];
int viz[nmax],st[nmax],i,n,m,x,y,j,k,nr;
void df(int i)
{
    int j;
    viz[i]=1;
    for(j=0;j<v[i].size();j++)
        if(!viz[v[i][j]]) df(v[i][j]);
    st[++k]=i;
}
void df1(int i)
{
    int j;
    viz[i]=1;
    for(j=0;j<v1[i].size();j++)
    {
        if(!viz[v1[i][j]]) df1(v1[i][j]);
    }
}
void df2(int i)
{
    int j;
    viz[i]=1;g<<i<<' ';
    for(j=0;j<v1[i].size();j++)
    {
        if(!viz[v1[i][j]]) df2(v1[i][j]);
    }
}
int main()
{
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        v[x].push_back(y);
        v1[y].push_back(x);
    }
    for(i=1;i<=n;i++)
    if(!viz[i])df(i);
    memset(viz,0,sizeof(viz));
    for(j=n;j>=1;j--)
    if(!viz[st[j]]){nr++;
    df1(st[j]);}
    g<<nr<<'\n';
    memset(viz,0,sizeof(viz));
    for(j=n;j>=1;j--)
    if(!viz[st[j]])
    {df2(st[j]);
    g<<'\n';}
    return 0;
}