Cod sursa(job #1977028)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 4 mai 2017 21:05:38
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
#define Nmax 100001
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
ofstream fout("x.txt");
struct graph
{
    vector <int> v;
};
graph G[Nmax],GG[Nmax];
bitset <Nmax> viz;
int nr;
int pord[Nmax];
void DFSG(int x)
{
    int i;
    viz[x]=true;
    for(i=0;i<G[x].v.size();i++)
    if(!viz[G[x].v[i]]) DFSG(G[x].v[i]);
    pord[++nr]=x;
}
void DFSGG(int x)
{
    int i;
    fout<<x<<' ';
    viz[x]=false;
    for(i=0;i<GG[x].v.size();i++)
    if(viz[GG[x].v[i]]) DFSGG(GG[x].v[i]);
}
int main()
{int n,m,k,i,j;
f>>n>>m;
for(k=1;k<=m;k++)
{
    f>>i>>j;
    G[i].v.push_back(j);
    GG[j].v.push_back(i);
}
for(i=1;i<=n;i++)
if(!viz[i]) DFSG(i);
nr=0;
for(i=n;i>0;i--)
if(viz[pord[i]])
{
    nr++;
    DFSGG(pord[i]);
    fout<<'\n';
}
g<<nr<<'\n';
fout.close();
ifstream fin("x.txt");
char ch;
while(fin>>noskipws>>ch)
g<<ch;

    return 0;
}