Cod sursa(job #2140388)

Utilizator andrei1299Ghiorghe Andrei Alexandru andrei1299 Data 23 februarie 2018 13:40:51
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
int N,M,t[50005],k;
bool viz[50005];
vector <int> a[50005];

void dfs(int x)
{
    viz[x]=true;
    int i;
    for(i=0;i<a[x].size();i++)
    {
        if(viz[a[x][i]]==false) dfs(a[x][i]);
    }
    t[++k]=x;
}

inline void DFS(int x)
{
    int y;
    unsigned int i;
    viz[x]=true;
    for(i=0;i<a[x].size();i++)
    {
        y=a[x][i];
        if(!viz[y]) DFS(y);
    }
    t[++k]=x;
}

int main()
{
    int i,x,y;
    ifstream fin("sortaret.in");
    fin>>N>>M;
    for(i=1;i<=M;i++)
    {
        fin>>x>>y;
        a[x].push_back(y);
    }
    fin.close();
    for(i=1;i<=N;i++)
    {
        if(viz[i]==0) dfs(i);
    }
    ofstream fout("sortaret.out");
    for(i=k;i>0;i--)
        fout<<t[i]<<" ";
    fout.close();
    return 0;
}