Cod sursa(job #1984967)

Utilizator miruna999Morarasu Miruna miruna999 Data 26 mai 2017 17:08:51
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n,m,x,y;
bool viz[50001]; stack<int> s;
struct nod{int nr;nod *urm;}*a[50001];

void adaug(int x,int y)
{
    nod *p = new nod;
    p->nr=y;
    p->urm=a[x];
    a[x]=p;
}

void df(int k)
{
    viz[k]=1;
    for(nod *p=a[k];p;p=p->urm)
        if(!viz[p->nr])
            df(p->nr);
    s.push(k);
}

int main()
{
    f>>n>>m;
    for(int i=1;i<=m;++i)
        f>>x>>y,adaug(x,y),adaug(y,x);
    for(int i=1;i<=n;i++)
        if(!viz[i])
            df(i);
    while(!s.empty())
        g<<s.top()<<" ",s.pop();
    return 0;
}