Cod sursa(job #1317344)
| Utilizator | Data | 14 ianuarie 2015 20:26:08 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 50005
using namespace std;
ofstream out("sortaret.out");
int N, M;
vector<int> v[nmax];
int viz[nmax];
void df(int nod) {
viz[nod] = 1;
out << nod << " ";
for (int i=0; i<v[nod].size(); ++i)
if ( !viz[ v[nod][i] ] )
df(v[nod][i]);
}
int main() {
ifstream in("sortaret.in");
in >> N >> M;
for (int i=1; i<=M; ++i) {
int x, y;
in >> x >> y;
v[x].push_back(y);
}
for (int i=1; i<=N; ++i) {
if (!viz[i]) {
df(i);
}
}
}
