Cod sursa(job #2089782)

Utilizator zanugMatyas Gergely zanug Data 17 decembrie 2017 10:12:05
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 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];
bool b[N];
bool start[N];

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

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

//    cout << 1;

    int s = 1;
    while(start[s])
        ++s;

    while(s <= n)
    {
        if(!b[s])
        {
            fout << s << " ";
            dfs(s);
        }
        ++s;
    }

    fout << "\n";

    return 0;
}