Cod sursa(job #1645117)

Utilizator serbanSlincu Serban serban Data 10 martie 2016 11:07:14
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.74 kb
// just testing
// do not judge

#include <bits/stdc++.h>

using namespace std;

const char infile[] = "sortaret.in";
const char outfile[] = "sortaret.out";

ofstream fout(outfile);

const int MAXN = 50005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

const int lim = 1 << 10;
char buffer[lim];
int pos;
int N, M;
Graph G;

inline void get(int &nr) {
    nr = 0;
    while(!('0' <= buffer[pos] && buffer[pos] <= '9')) {
        if(++ pos == lim) {
            fread(buffer, 1, lim, stdin);
            pos = 0;
        }
    }
    while(('0' <= buffer[pos] && buffer[pos] <= '9')) {
        nr = nr * 10 + buffer[pos] - '0';
        if(++ pos == lim) {
            fread(buffer, 1, lim, stdin);
            pos = 0;
        }
    }
}

vector<int> ans;
bool viz[50100];

void df(int node) {
    viz[node] = true;
    for(int i = 0; i < G[node].size(); i ++) {
        int cur = G[node][i];
        if(!viz[cur]) df(cur);
    }
    ans.push_back(node);
}

int main() {
    freopen(infile, "r", stdin);
    get(N);
    get(M);
    for(int i = 1 ; i <= M ; ++ i) {
        int x, y;
        get(x);
        get(y);
        G[x].push_back(y);
    }
    for(int i = 1; i <= N; i ++) {
        if(!viz[i]) df(i);
    }
    for(int i = ans.size() - 1; i >= 0; i --) fout << ans[i] << " ";
    fout << "\n";
    fout.close();
    return 0;
}