-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10305.cpp
More file actions
54 lines (50 loc) · 795 Bytes
/
10305.cpp
File metadata and controls
54 lines (50 loc) · 795 Bytes
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
#include<bits/stdc++.h>
using namespace std;
map<int,bool>visited;
map<int,vector<int> > adj;
stack<int>topoVertices;
int cnt = 0;
void clear(){
cnt = 0;
adj.clear();
}
void dfs(int x){
visited[x] = true;
for(auto i:adj[x]){
if(visited[i] == false){
dfs(i);
}
}
visited[x] = true;
topoVertices.push(x);
}
int main(){
int nodes,edges;
while(cin>>nodes>>edges){
if(nodes == 0){
break;
}
for(int i = 1;i<=nodes;i++){
visited[i] = false;
}
while(edges--){
int x,y;
cin>>x>>y;
adj[x].push_back(y);
}
for(int i=1;i<=nodes;i++){
if(visited[i] == false){
dfs(i);
}
}
while(!topoVertices.empty()){
int i = topoVertices.top();
topoVertices.pop();
cout<<i;
if(!topoVertices.empty())
cout<<' ';
}
cout<<endl;
clear();
}
}