-
Notifications
You must be signed in to change notification settings - Fork 0
/
2252.cpp
50 lines (46 loc) · 925 Bytes
/
2252.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
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int N, M;
int indegree[35002];
vector<int> adj[35002];
int main() {
int from, to; scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
scanf("%d %d", &from, &to);
adj[from].push_back(to);
indegree[to]++;
}
queue<int> topoloQ;
queue<int> result;
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) {
topoloQ.push(i);
}
}
while (!topoloQ.empty()) {
int loopcount = topoloQ.size();
for (int loop = 0; loop < loopcount; loop++) {
int now = topoloQ.front();
result.push(now);
topoloQ.pop();
indegree[now]--;
for (int next : adj[now]) {
indegree[next]--;
if (indegree[next] == 0) {
topoloQ.push(next);
}
}
}
}
for (int i = 1; i <= N; i++) {
cout << result.front() << " ";
result.pop();
}
return 0;
}