Cod sursa(job #2425143)

Utilizator cochinteleandreeaCochintele Andreea Elena cochinteleandreea Data 24 mai 2019 13:32:13
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>
#include<stack>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
vector<int> viz;
int n, m, s,d,nr=0;
bool ok;
vector<int > a[100005];
stack<int > stiva;

void citire()
{
    int x, y, i;
    f >> n >>m;
    for(i=0; i<=n; i++)
        viz.push_back(0);

    while(f>>x>>y)
    {
        a[x].push_back(y);
        a[y].push_back(x);
    }


}
void dfs(int X)
{
    viz[X] = 1;
    for(unsigned int itr =0 ; itr < a[X].size(); itr ++)
       {
           int temp=a[X][itr];
            if(  viz[temp] == 0) dfs(temp);
       }
       stiva.push(X);

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

    while(!stiva.empty())
    {
        g<<stiva.top()<< " ";
        stiva.pop();
    }

    return 0;
}