Mai intai trebuie sa te autentifici.
Cod sursa(job #168744)
| Utilizator | Data | 31 martie 2008 19:20:50 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 90 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <vector.h>
#define MAXN 50000
int n, in[MAXN];
vector<int> mat[MAXN];
ofstream fout("sortaret.out");
void go(int i){
in[i] = -1;
fout << i << " ";
for (vector<int>::iterator it = mat[i].begin();
it != mat[i].end(); ++it) {
int k = *it;
in[k]--;
if (!in[k])
go(k);
}
}
int main(void){
ifstream fin("sortaret.in");
int m,x,y;
fin >> n;
for (fin >> m;m;m--){
fin >> x >> y;
mat[x].push_back(y);
in[y]++;
}
for (int i=1;i<=n;i++)
if (!in[i]) go(i);
fin.close();
fout.close();
return 0;
}
