-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcu3212.cpp
58 lines (51 loc) · 1.24 KB
/
cu3212.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
#include <bits/stdc++.h>
using namespace std;
vector<int> vec[201];
priority_queue<int> qu;
int v, n, a, b, connect[201], wh, visited[201], ans, printarr[201], fl;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> v >> n;
for(int i = 0; i < n; i++){
cin >> a >> b;
vec[a].push_back(b);
connect[b]++;
}
for(int i = 1; i <= v; i++){
sort(vec[a].begin(), vec[a].end());
}
for(int i = 1; i <= v; i++){
if(!connect[i]){
qu.push(-i);
visited[i] = 1;
}
}
while(!qu.empty()){
wh = -1 * qu.top();
printarr[++ans] = wh;
qu.pop();
for(int i = 0; i < vec[wh].size(); i++){
if(!visited[vec[wh][i]]){
connect[vec[wh][i]]--;
if(connect[vec[wh][i]] == 0){
qu.push(-1 * vec[wh][i]);
visited[vec[wh][i]] = visited[wh] + 1;
}
}
}
}
for(int i = 1; i <= v; i++){
if(connect[i]){
fl = 1;
break;
}
}
if(fl) cout << "-1\n";
else{
for(int i = 1; i <= v; i++){
cout << printarr[i] << '\n';
}
}
}