Cod sursa(job #2050888)

Utilizator draghici_vladDraghici Vlad draghici_vlad Data 28 octombrie 2017 11:49:38
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

vector <int> l[100000];

bool viz[5000005],ok;
int k,u,p,poz,n,s,m,x,y,nr,v[100000];

void topo(int nod)
{
    int i;
    viz[nod] = 1;
    for(i = 0; i< l[nod].size(); i++)
        if(viz[l[nod][i]] == 0)
            {
                v[++u] = l[nod][i];
                topo(l[nod][i]);
            }
}

int main()
{
    f>>n>>m;
    for(int i = 1; i<= m; i++)
    {
        f>>x>>y;
        ok=1;

        for(int j = 0; j < l[x].size() && ok; j++)
            if(l[x][y] == y)
                ok = 0;
        if(ok)
            l[x].push_back(y);
    }

    v[++u] = 1;

    for(int i=1;i<=n;i++)
        if(viz[i]==0)
            topo(i);

    for(int i=1;i<=u;i++)
        g<<v[i]<<' ';
    return 0;
}