-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbureaucracy.cpp
More file actions
68 lines (59 loc) · 1.14 KB
/
bureaucracy.cpp
File metadata and controls
68 lines (59 loc) · 1.14 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
#include<cstdio>
#include<iostream>
#include<fstream>
#include<vector>
#include<stack>
#define SIZE 100001
using namespace std;
signed n, m;
vector<signed> vertex[SIZE];
vector<signed> vertex2[SIZE];
bool visited[SIZE];
bool vertexDepend[SIZE];
vector<signed> result;
void DFS(signed v){
visited[v] = true;
result.push_back(v);
signed size1 = vertex[v].size();
for(signed i = 0; i < size1; i++)
if(!visited[vertex[v][i]]){
bool next = true;
signed size2 = vertex2[vertex[v][i]].size();
for(signed j = 0; j < size2; j++)
if(!visited[vertex2[vertex[v][i]][j]]){
next = false;
break;
}
if(next)
DFS(vertex[v][i]);
}
}
int main(){
ifstream In("input.txt");
ofstream Out("output.txt");
In >> n;
In >> m;
for(signed i = 0; i < m; i++){
signed v1, v2;
In >> v1;
In >> v2;
vertex[v1].push_back(v2);
vertex2[v2].push_back(v1);
vertexDepend[v2] = true;
}
In.close();
for(signed i = 1; i <= n; i++)
if(!vertexDepend[i]){
DFS(i);
}
if(result.size() != n){
Out << -1;
Out.close();
return 0;
}
for(signed i = 0; i < result.size(); i++){
Out << result[i];
Out << " ";
}
Out.close();
}