Pagini recente » Cod sursa (job #2713704) | Cod sursa (job #2312376) | Cod sursa (job #2319358) | Cod sursa (job #1689364) | Cod sursa (job #559253)
Cod sursa(job #559253)
#include <algorithm>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const char Input[] = "sortaret.in";
const char Output[] = "sortaret.out";
const int Dim = 50001;
int N, M;
int gr[Dim];
queue <int> q;
vector <int> v[Dim];
int main() {
ifstream fin( Input );
ofstream fout( Output );
int i, x, y, nod;
vector <int> :: iterator it;
fin >> N >> M;
while( M-- ) {
fin >> x >> y;
v[x].push_back( y );
++gr[y];
}
for( i = 1; i <= N; ++i )
if( !gr[i] )
q.push( i );
while( !q.empty() ) {
nod = q.front();
q.pop();
fout << nod << " ";
for( it = v[nod].begin(); it != v[nod].end(); ++it ) {
--gr[*it];
if( !gr[*it] )
q.push( *it );
}
}
fin.close();
fout.close();
return 0;
}