Pagini recente » Rating Ioana Muntean (IoanaMuntean) | Cod sursa (job #2144887)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
const int Nmax = 50005;
vector < int > L[Nmax];
int n , m , gr[Nmax];
struct Graf
{
int nod;
bool operator < (const Graf &e) const
{
return nod > e . nod;
}
};
priority_queue < Graf > Q;
int main()
{
Graf w , w1;
int x , y;
fin >> n >> m;
for(int i = 1 ; i <= n ; i++)
{
fin >> x >> y;
L[x] . push_back(y);
++gr[y];
}
for(int i = 1 ; i <= n ; i++)
if(!gr[i])
{
w . nod = i;
Q . push(w);
}
while(! Q . empty())
{
w = Q . top();
fout << w . nod << " ";
Q . pop();
for(auto i : L[w . nod])
{
--gr[i];
if(!gr[i])
{
w1 . nod = i;
Q . push(w1);
}
}
}
fout << "\n";
fin.close();
fout.close();
return 0;
}