Nu exista pagina, dar poti sa o creezi ...

Cod sursa(job #1707079)

Utilizator andru47Stefanescu Andru andru47 Data 24 mai 2016 09:57:09
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int NMAX = 100000 + 5;
int n,m,x,y,st[NMAX],N;
vector <int> g[NMAX];
bool sel[NMAX];
inline void dfs(int x)
{
    sel[x] = true;
    for (int i = 1; i<g[x].size(); ++i)
        if (!sel[g[x][i]])
            dfs[g[x][i]];
    st[++N] = x;
}
int main()
{
    freopen("sortaret.in","r",stdin);
    freopen("sortaret.out","w",stdout);
    scanf("%d %d", &n ,&m);
    for (int i = 1; i<=n ; ++i)
    {
        scanf("%d %d\n", &x, &y);
        g[x].pb(y);
    }
    for (int i = 1; i<=n; ++i)
        if (!sel[i])
            dfs(i);
    for (int i = N; i>=1; --i)
        printf("%d ", st[i]);
    return 0;
}