Mai intai trebuie sa te autentifici.
Cod sursa(job #603148)
Utilizator | Data | 14 iulie 2011 19:54:36 | |
---|---|---|---|
Problema | Sortare topologica | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.87 kb |
#include <iostream>
#include <vector>
#include <stack>
#define NMax 50005
using namespace std;
vector <int> G[NMax];
int N;
bool Viz[NMax];
stack <int> ST;
void DFS (int X)
{
Viz[X]=true;
for (vector <int> :: iterator V=G[X].begin (); V!=G[X].end (); ++V)
{
if (!Viz[*V])
{
DFS (*V);
}
}
ST.push (X);
}
int main()
{
freopen ("sortaret.in", "r", stdin);
freopen ("sortaret.out", "w", stdout);
int M;
scanf ("%d %d", &N, &M);
for (; M>0; --M)
{
int X, Y;
scanf ("%d %d", &X, &Y);
G[X].push_back (Y);
}
for (int i=1; i<=N; ++i)
{
if (!Viz[i])
{
DFS (i);
}
}
while (!ST.empty ())
{
printf ("%d ", ST.top ());
ST.pop ();
}
printf ("\n");
return 0;
}