Cod sursa(job #1213145)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 27 iulie 2014 12:31:19
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

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

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
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; }

Graph g;
int n, m, in[MAXN];
priority_queue<int, vector <int>, greater<int> > heap;

int main() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    fin >> n >> m;
    while(m -- ) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        ++ in[y];
    }
    /// Minimum lexicographic topological sort
    for(int i = 1 ; i <= n ; ++ i)
        if(in[i] == 0)
            heap.push(i);
    while(!heap.empty()) {
        int node = heap.top();
        heap.pop();
        fout << node << ' ';
        for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it)
            if(-- in[*it] == 0)
                heap.push(*it);
    }
    fin.close();
    fout.close();
    return 0;
}