-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordering_tasks.cpp
More file actions
77 lines (70 loc) · 2.21 KB
/
ordering_tasks.cpp
File metadata and controls
77 lines (70 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <queue>
using namespace std;
struct Node
{
vector<int> saidas;
};
int main(){
#ifndef ONLINE_JUDGE
ifstream cin("entrada.txt");
ofstream cout("saida.txt");
#endif
string line;
stringstream line_stream;
while( getline(cin,line) ){
if( line == "0 0" ) break;
line_stream = stringstream(line);
int N,M;
line_stream >> N >> M;
vector<Node> nodes(N);
vector<int> ordem_topologica;
vector<int> graus_de_entrada(N);
for( int _m = 0 ; _m < M ; _m++ ){
getline(cin,line);
line_stream = stringstream(line);
int i,j;
line_stream >> i >> j;
nodes[i-1].saidas.push_back(j-1);
}
for( auto node : nodes ){
for( auto it : node.saidas ){
graus_de_entrada[it]++;
}
}
int indice_inseridos = 0;
vector<int> faltantes;
for( int i = 0 ; i < N ; i++ )
faltantes.push_back(i);
while( ordem_topologica.size() < N ){
// Procura onde tem 0
// Se nao tem 0, erro
// Insere na pilha
// Tira um de todos grau de entrada q apontava
auto it = faltantes.begin();
//for( auto v : faltantes ) cout << "falta " << v+1 << endl;
while( it < faltantes.end() ){
if( graus_de_entrada[*it] <= 0 ){
//cout << "achei o " << *it+1 << endl;
ordem_topologica.push_back(*it);
it = faltantes.erase(it);
}
it++;
}
int proximo_executar = ordem_topologica[indice_inseridos];
Node proximo_node = nodes[proximo_executar];
for( auto it : proximo_node.saidas ){
graus_de_entrada[it] = graus_de_entrada[it]-1;
}
indice_inseridos++;
}
for( auto it = ordem_topologica.begin() ; it != ordem_topologica.end() ; it++ )
cout << (*it + 1) << ((it==ordem_topologica.end()-1)?"":" ") ;
cout << endl;
}
}