Cod sursa(job #3248588)

Utilizator IleaIlea Bogdan Ilea Data 12 octombrie 2024 10:49:55
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m;
vector<set<int> > v;
vector<int> b;
vector<int> l;
void dfs(int i)
{
    b[i]=1;
    for (set<int>::iterator it=v[i].begin(); it!=v[i].end(); it++){
        if (!b[*it]){dfs(*it);l.push_back(*it);}
    }
}
int main()
{
    fin>>n>>m;
    v.resize(n+1);
    b.resize(n+1);
    for (int i=0; i<m; i++){
        int x, y;
        fin>>x>>y;
        v[x].insert(y);
    }
    for (int i=1; i<=n; i++){
        if (!b[i]){dfs(i); l.push_back(i);};
    }
    for (int i=l.size()-1; i>=0; i--)fout<<l[i]<<" ";
    return 0;
}