Cod sursa(job #1326845)

Utilizator theprdvtheprdv theprdv Data 26 ianuarie 2015 03:12:40
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
fstream fout("sortaret.out", ios::out);

int n, m, p[50100];
queue <int> q;

struct node{
    int inf;
    node *next;
};
struct node *list[50100];

void add(node *&root, int n){
    node *elem=new node;
    elem->inf=n;
    elem->next=root;
    root=elem;
}
void read(){
    fstream fin("sortaret.in", ios::in);
    fin>>n>>m;
    for(int i=1; i<=n; i++){
        int x, y;
        fin>>x>>y;
        add(list[x], y);
    }
    fin.close();
}
void DFS(int s){
    //fout<<s<<" ";
    p[s]=1;
    node *root=list[s];
    while(root){
        if(!p[root->inf]) DFS(root->inf);
        root=root->next;
    }
    fout<<s<<" ";
}
int main()
{
    read();
    DFS(1);
    fout.close();
    return 0;
}