Cod sursa(job #2192943)

Utilizator PushkinPetolea Cosmin Pushkin Data 7 aprilie 2018 20:25:14
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
vector<vector<int>> G;
unsigned int n, m, a;
vector<bool> viz, b;
void rd()
{
    int x, y;
    f>>n>>m;
    viz.resize(n+1);
    b.resize(n+1);
    G.resize(n+1);
    while(f>>x>>y)
    {
        G[x].push_back(y);
        b[y]=1;
    }
    while(b[++a]);
}
void DFS(int x)
{
    viz[x]=1;
    g<<x<<' ';
    ///g<<"Am vizitat nodul "<<x<<'\n';
    for(unsigned int i=0;i<G[x].size();i++)
        if(!viz[G[x][i]])
            DFS(G[x][i]);
}
int main()
{
    rd();
    DFS(a);
    f.close();
    g.close();
    return 0;
}