Cod sursa(job #1190878)

Utilizator gapdanPopescu George gapdan Data 25 mai 2014 20:31:09
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<cstdio>
#include<vector>
using namespace std;
vector<int>v[50001];
vector<int>sol;
int viz[50001];
int n,m,i;
void DF(int nod)
{
    vector<int>::iterator it;
    viz[nod]=1;
    for (it=v[nod].begin();it<=v[nod].end();++it)
    {
        if (viz[*it]==0)
        {
            DF(*it);
        }
    }
    sol.push_back(nod);
}
int main()
{
    int 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);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (i=1;i<=n;++i)
    {
        if (viz[i]==0)
        {
            DF(i);
        }
    }
    vector<int>::iterator it;
    for (it=sol.begin();it!=sol.end();++it)
        printf("%d ",&*it);
    return 0;
}