Pagini recente » Cod sursa (job #696620) | Cod sursa (job #2989451) | Cod sursa (job #2851824) | Cod sursa (job #1972555) | Cod sursa (job #1348786)
#include<stdio.h>
#include<queue>
#include<vector>
#include<set>
using namespace std;
struct node {
int life;
vector<int> nexts;
};
node what[50010];
set<int> values;
queue<int> q;
int main()
{
freopen("sortaret.in", "r", stdin);
freopen("sortaret.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0 ; i < 50010 ; ++i) {
what[i].life = 0;
}
for(int i = 0 ; i < m ; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if(what[x].life == 0) {
values.insert(x);
}
values.erase(y);
++what[y].life;
what[x].nexts.push_back(y);
}
for(int i = 1 ; i <= n ; ++i) {
if(what[i].life == 0 && values.find(i) == values.end()) {
printf("%d ", i);
}
}
for(int i : values) {
q.push(i);
}
while(!q.empty()) {
int x = q.front();
q.pop();
printf("%d ", x);
for(int i : what[x].nexts) {
--what[i].life;
if(what[i].life == 0) {
q.push(i);
}
}
--what[x].life;
}
return 0;
}