Pagini recente » Cod sursa (job #522327) | Cod sursa (job #2734371) | Cod sursa (job #1318425) | Cod sursa (job #280330) | Cod sursa (job #2271935)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
int const NM = 5e4 + 7;
vector <int> G [NM];
int d [NM];
ifstream f ("sortaret.in");
ofstream g ("sortaret.out");
int n , m;
inline void bfs (){
queue <int> Q;
for(int i = 1 ; i <= n ; ++ i)
if (! d [i]){
Q . push (i);
g << i << ' ';
}
while (! Q . empty ()){
int node = Q . front ();
vector <int> :: iterator it;
for(it = G [node] . begin () ; it != G [node] . end () ; ++ it){
-- d [*it];
if (! d [*it]){
Q . push (*it);
g << *it << ' ';
}
}
Q . pop ();
}
}
int main()
{
f >> n >> m;
for(int i = 1 ; i <= m ; ++ i){
int a , b;
f >> a >> b;
G [a] . push_back (b);
++ d [b];
}
bfs ();
return 0;
}