Cod sursa(job #1379544)

Utilizator taigi100Cazacu Robert taigi100 Data 6 martie 2015 18:20:46
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
/*
    Keep It Simple!
*/

#include <fstream>
#include <vector>
#include <list>
#include <stack>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

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

#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb push_back

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int kMaxN = 100005;

int n,m,ans,x,y;
bool viz[kMaxN];
vector<int> G[kMaxN];
list<int> topo;
void DFS(int node)
{
    viz[node] = true;
    for(auto it:G[node])
        if(!viz[it])
            DFS(it);
    topo.push_front(node);
}

void Solve()
{
    f >> n >> m;
    for(int i=1;i<=m;++i)
    {
        f >> x >> y;
        G[x].pb(y);
        G[y].pb(x);
    }

    for(int i=1;i<=n;++i)
        if(!viz[i])
            DFS(i);
    for(auto it:topo)
        g << it << ' ';
}

int main()
{
    Solve();
    return 0;
}