Cod sursa(job #2973948)

Utilizator lukaszisgabiManoliu Gabriel lukaszisgabi Data 2 februarie 2023 20:38:49
Problema Sortare topologica Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include<fstream>
#include<iostream>
#include<vector>
using namespace std;

#define fileIn "sortaret.in"
#define fileOut "sortaret.out"
#define nul nullptr

#define MAX_N 100000
#define MAX_M 400000

int n, m, fin[MAX_N + 1], k;
bool viz[MAX_N + 1];

struct arce {
	int x, y;
	bool valid = true;
}v[MAX_M + 1];

void citire() {
	ifstream fin(fileIn);
	fin >> n >> m;
	for (int i = 1; i <= m; i++)
		fin >> v[i].x >> v[i].y;
	fin.close();
}

void vizitat(int x) {
	viz[x] = 1;
	for (int i = 1; i <= m; i++)
		if(v[i].valid)
			if (v[i].x == x && !viz[v[i].y]) {
				v[i].valid = false;
				vizitat(v[i].y);
			}
	fin[++k] = x;
}

void afisare() {
	ofstream fout(fileOut);
	for (int i = n; i > 0; i--) fout << fin[i] << ' ';
	fout.close();
}

int main() {
	citire();
	for (int i = 1; i <= n; i++)
		if(!viz[i]) vizitat(i);
	afisare();
	return 0;
}