Pagini recente » Cod sursa (job #3160269) | Cod sursa (job #3235004) | Cod sursa (job #2374040) | Cod sursa (job #2171819) | Cod sursa (job #1365281)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 50005;
int n, m, in[maxn];
vector <int> g[maxn], tsort;
queue <int> q;
int main() {
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
fin >> n >> m;
for(int i = 1 ; i <= m ; ++ i) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
++ in[y];
}
for(int i = 1 ; i <= n ; ++ i)
if(!in[i])
q.push(i);
while(!q.empty()) {
int node = q.front();
tsort.push_back(node);
q.pop();
for(vector <int> :: iterator it = g[node].begin() ; it != g[node].end() ; ++ it)
if(-- in[*it] == 0)
q.push(*it);
}
for(vector <int> :: iterator it = tsort.begin() ; it != tsort.end() ; ++ it)
fout << *it << ' ';
}