-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10305.cpp
216 lines (192 loc) · 4.2 KB
/
10305.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//10305
// 10305 Ordering Tasks
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <iostream>
using namespace std;
template <class WEIGHT, class LABEL=int>
class Graph {
private:
int V, E;
map<LABEL, int> L;
map<int, vector< pair<WEIGHT, int> > >adj;
public:
Graph(int v=0) {
this->V = v;
this->E = 0;
};
int addNode(LABEL u) {
if(L.count(u)) return L[u];
else return L[u] = this->V++;
}
bool addEdge(LABEL labelU, LABEL labelV, WEIGHT w){
int u = addNode(labelU);
int v = addNode(labelV);
if(adj.count(u)==0)
adj[u] = vector< pair<WEIGHT, int> >();
adj[u].push_back( pair<WEIGHT,int>(w, v) );
this->E++;
return true;
}
//retorna a distancia de todos para a origem s
vector<int> bfs(LABEL origem) {
int s;
//inicializando visited com false
vector<bool> visited(V, false);
//retorno com a distancia de todos para s
vector<int> ret(V, INT_MAX);
//criando fila com s
queue<int> q;
if(L.count(origem)==0)
return ret;
s = L[origem];
q.push(s);
visited[s] = true;
ret[s] = 0;
//laco principal
while(!q.empty()) {
int u = q.front();
q.pop();
//iterar os vizinhos de u
vector< pair<WEIGHT,int> > viz = adj[u];
for(int i=0; i<viz.size(); i++) {
int v = viz[i].second;
if(!visited[v]) {
visited[v] = true;
ret[v] = ret[u]+1;
q.push(v);
}
}
} //laco principal
return ret;
}
//returns the number of visited nodes
int dfs(LABEL origem) {
int ret = 0;
//inicializa visited com false
vector<bool> visited(V, false);
//inicializa uma pilha st com s
stack<int> st;
if(L.count(origem)==0)
return 0;
int s = L[origem];
st.push(s);
visited[s] = true;
//laco principal
while(!st.empty()) {
int u = st.top();
st.pop();
ret++;
vector< pair<WEIGHT,int> > viz = adj[u];
for(int i=0; i<viz.size(); i++) {
int v = viz[i];
if(!visited[v]) {
visited[v] = true;
st.push(v);
}
}
}
return ret;
} //dfs
// g eh um grafo dirigido obrigatoriamente sem ciclos
vector<int> topSort() {
vector<int> ret;//retorno da funcao
vector<int> inDegree(V, 0); //inDegree
//inicializo inDegree
for(int u=0; u<V; u++) {
vector< pair<WEIGHT,int> > viz = adj[u];
for(int i=0; i<viz.size(); i++) {
int v = viz[i].second;
inDegree[v]++;
}
}
//inicializo fila q com todos inDegree 0
queue<int> q;
for(int u=0; u<V; u++) {
if(inDegree[u]==0)
q.push(u);
}
//laco principal sobre a fila q
while(!q.empty()) {
int u = q.front();
ret.push_back(u);
q.pop();
vector< pair<WEIGHT,int> > viz = adj[u];
for(int i=0; i<viz.size(); i++) {
int v = viz[i].second;
inDegree[v]--;
if(inDegree[v]==0) {
q.push(v);
}
}
}
//retorna o vetor criado
return ret;
}
// Complexidade: O(E + VlogV)
vector<int> Dijkstra(LABEL origemL) {
vector<int> cost(V, INT_MAX);
if(L.count(origemL)==0)
return cost;
int origem = L[origemL];
cost[origem] = 0;
vector<bool> visited(V, false);
priority_queue< pair<int,int>, vector<pair<int,int> >,
greater< pair<int,int> > > q;
q.push( pair<int,int>(0, origem) );
while(!q.empty()) {
//escolher o working node
pair<int,int> p = q.top(); //log(V)
q.pop();
int wNode = p.second;
if(visited[wNode]) continue;
/*
int wNode = -1;
for(int i=0; i<g.v; i++) {
if(visited[i]) continue;
if(wNode==-1||cost[i]<cost[wNode]) wNode=i;
}
if(wNode==-1)
return cost;
*/
//iterar sobre os vizinhos
vector< pair<WEIGHT,int> > viz = adj[wNode];
for(int i=0; i<viz.size(); i++) {
int v = viz[i].second;
WEIGHT w = viz[i].first;
if(visited[v]) continue;
if(cost[wNode] + w < cost[v]) {
cost[v] = cost[wNode]+w;
q.push( pair<int,int>(cost[v], v) );
}
}
//visitar wNode
visited[wNode] = true;
}
return cost;
} //Dijkstra
};
int main() {
int n, m;
while(1) {
cin >> n >> m;
if(!n&&!m) break;
Graph<int, int> g;
for(int i=1; i<=n; i++)
g.addNode(i);
for(int i=0; i<m; i++) {
int u, v;
cin >> u >> v;
g.addEdge(u, v, 1);
}
vector<int> v = g.topSort();
cout << v[0]+1;
for(int i=1; i<v.size(); i++) {
cout << " " << v[i]+1;
}
cout << endl;
}
}