Cod sursa(job #1250938)

Utilizator vlady1997Vlad Bucur vlady1997 Data 28 octombrie 2014 19:23:25
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
        #include <cstdio>
        #include <vector>
        using namespace std;
        vector <int> g[50001];
        int q[50001], k=0;
        bool sel[50001];
        void dfs (int x)
        {
            sel[x]=true;
            while (!g[x].empty())
            {
                if (sel[g[x].back()]==false)
                {
                    dfs(g[x].back());
                }
                g[x].pop_back();
            }
            q[++k]=x;
        }
        int main()
        {
            int n, m, i, x, y;
            freopen("sortaret.in","r",stdin);
            freopen("sortaret.out","w",stdout);
            scanf("%d%d",&n,&m);
            for (i=1; i<=m; i++)
            {
                scanf("%d%d",&x,&y);
                g[x].push_back(y);
            }
            for (i=1; i<=n; i++)
            {
                if (sel[i]==false)
                {
                    dfs(i);
                }
            }
            for (i=k; i>=1; i--) printf("%d ",q[i]);
            fclose(stdin);
            fclose(stdout);
            return 0;
        }