Cod sursa(job #3299173)

Utilizator CosminaneBoac Mihai Cosmin Cosminane Data 5 iunie 2025 00:08:45
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
vector <int> v[50005];
int f[50005];
int main(){
	int n, m, i, x, y;
	ifstream fin( "sortaret.in" );
	ofstream fout( "sortaret.out" );
	fin >> n >> m;
	for( i = 0; i < m; i++ ){
		fin >> x >> y;
		v[x].push_back( y );
		f[y]++;
	}
	queue <int> q;
	for( i = 1; i <= n; i++ ){
		if( f[i] == 0 ){
			q.push( i );
			fout << i << ' ';
		}
	}
	while( q.empty() == false ){
		x = q.front();
		q.pop();
		for( i = 0; i < v[x].size(); i++ ){
			y = v[x][i];
			f[y]--;
			if( f[y] == 0 ){
				q.push( y );
				fout << y << ' ';
			}
		}
	}
	return 0;
}