Cod sursa(job #2059339)

Utilizator zanugMatyas Gergely zanug Data 6 noiembrie 2017 21:28:53
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>

#define pb push_back
#define ll long long

using namespace std;

const int N = 1e5 + 10;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

int n, m;
vector < int > v[N];
vector < int > a;
int apa[N];
bool b[N];

void dfs(int cnod)
{
    if(!b[cnod])
    {
        b[cnod] = true;
        fout << cnod << " ";
        for(int i = 0; i < v[cnod].size(); ++i)
        {
            int nnod = v[cnod][i];
                dfs(nnod);
        }
    }

}

int main()
{
    ios::sync_with_stdio(false);

    fin >> n >> m;
    int x, y;
    for(int i = 0; i < m; ++i)
    {
        fin >> x >> y;
        v[x].pb(y);
        apa[y] = x;
    }

    for(int i = 1; i <= n; ++i)
    {
        if(!apa[i])
            a.pb(i);
    }

    for(int i = 0; i < a.size(); ++i)
    {
        int s = a[i];
            dfs(s);
    }

    fout << "\n";

    return 0;
}