Cod sursa(job #2131849)

Utilizator CronosClausCarare Claudiu CronosClaus Data 15 februarie 2018 00:19:02
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

const int mxn = 50 * 1000 + 10;

vector< int > v[ mxn ];
vector< int > s;

int n, m;

bool viz[ mxn ];

void dfs(int nod){
    viz[ nod ] = 1;
    for(auto it: v[ nod ])
        if(viz[ it ] == 0)
            dfs( it );
    s.push_back( nod );
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie();
    ifstream cin("sortaret.in");
    ofstream cout("sortaret.out");
    cin>> n >> m;
    for(int i = 0, x ,y; i < m; i++){
        cin>> x >> y;
        v[ x ].push_back( y );
        v[ y ].push_back( x );
    }
    for(int i = 1; i <= n; i++)
        if(viz[ i ] == 0)
            dfs( i );
    for(int i = 0; i < s.size(); i++)
        cout<< s[ i ] << ' ';
    return 0;
}