Pagini recente » Cod sursa (job #1413774) | Cod sursa (job #3273641) | Cod sursa (job #2431976) | Cod sursa (job #1959310) | Cod sursa (job #1379623)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50010;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m, grad[MAXN];
vector <int> g[MAXN];
queue <int> q;
void sortareTop()
{
for (int i = 1; i <= n; ++i)
{
if (grad[i] == 0)
{
q.push(i);
fout << i << " ";
}
}
while (!q.empty())
{
int node = q.front();
q.pop();
for (vector <int> :: iterator it = g[node].begin(); it != g[node].end(); ++it)
{
int v = *it;
grad[v]--;
if (grad[v] == 0)
{
q.push(v);
fout << v << " ";
}
}
}
}
int main()
{
fin >> n >> m;
for (int a, b; m; --m)
{
fin >> a >> b;
g[a].push_back(b);
grad[b]++;
}
sortareTop();
return 0;
}