Pagini recente » Borderou de evaluare (job #1596204) | Cod sursa (job #2982015) | Cod sursa (job #3336671) | Cod sursa (job #956505) | Cod sursa (job #3352153)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int Nmax = 1e5+5,Mmax = 5e5+5;
int deg[Nmax], n, m, x, y;
bool Use[Nmax];
queue<int> Q;
vector <int> G[Nmax];
void read()
{
fin>>n>>m;
for(int i = 1; i <= m; ++i)
{
fin >> x >> y;
G[x].push_back(y);
deg[y]++;
}
}
void sort_top()
{
for (int i = 1; i <= n; i++)
if (deg[i] == 0) ///grad interior
{
Q.push(i);
Use[i] = 1;
}
while (!Q.empty())
{
x = Q.front();
fout << x<<" ";
for(auto it : G[x]) {
if (!Use[it] && deg[it] == 1)
{
deg[it]--;
Q.push(it);
Use[it] = true;
}
}
Q.pop();
}
}
int main()
{
read();
sort_top();
return 0;
}