Cod sursa(job #2783575)

Utilizator Irina.comanIrina Coman Irina.coman Data 14 octombrie 2021 19:00:28
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>

using namespace std;
vector<int> graph[50005];
int vis[50005], v[50005], cnt;
void dfs(int node)
{
    vis[node] = true;
    for (int i = 0; i < graph[node].size(); i++)
    {
        int next = graph[node][i];
        if (!vis[next])
            dfs(next);
    }
    v[++cnt] = node;
}

int main()
{
    ifstream fin("sortaret.in");
    ofstream fout("sortaret.out");
    int N, M, x, y;
    fin >> N >> M;
    for (int i = 1; i <= M; i++)
    {
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for (int i = 1; i <= N; i++)
        if (!vis[i])
            dfs(i);
    for (int i = 1; i <= cnt; i++)
        fout << v[i] << " ";
}