Cod sursa(job #3293731)

Utilizator mihai21681Maricutu Mihai Alexandru mihai21681 Data 12 aprilie 2025 13:52:51
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
// topsort2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <fstream>
#include <vector>
using namespace std;
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
int n, m;
vector<int> G[50001], rez;
bool viz[50001];
void dfs(int i) {
    if (!viz[i]) {
        viz[i] = 1;
        for (int j : G[i])
            dfs(j);
        rez.push_back(i);
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
    }
    for (int i = 1; i <= n; i++)
        if (!viz[i])
            dfs(i);
    for (auto i = rez.rbegin(); i != rez.rend(); i++)
        cout << *i << ' ';
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file