Cod sursa(job #764234)

Utilizator mi5humihai draghici mi5hu Data 4 iulie 2012 15:05:46
Problema Sortare topologica Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <stdio.h>
#include <set>
#include <vector>
using namespace std;   
set<int> v[50050];
vector<int> rez;
int n, m;

void citire() {     
    int a, b;
    scanf("%d%d", &n, &m);
                 
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &a, &b);
        v[b].insert(a);
    }
}

void rezolva() {
     int i, tmp, j;
     bool done;
     bool vazut[50050];
     for (i = 1; i <= n; i++) {
         vazut[i] = false;
     }
     
     done = false;
                
     i = 0;    
     while (i < n) {
         i++;          
         tmp = i;     
         if ((!vazut[i]) && v[i].empty()) {
             for (j = 1; j <= n; j++) {                      
                 v[j].erase(i);
                 if ((j < tmp + 1) && (v[j].empty()) && (!vazut[j])) {
                     tmp = j-1;
                 }
             }             
             vazut[i] = true;
             rez.push_back(i); 
             i = tmp;               
         }
     }     
}

void afisare() {
     while (!rez.empty()) {
           printf("%d ", rez.front());
           rez.erase(rez.begin());
     }     
}

int main()
{
    freopen("sortaret.in", "r", stdin);
    freopen("sortaret.out", "w", stdout);
    
    citire();
    rezolva();
    afisare();    
}