-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUVa 10305.cpp
75 lines (66 loc) · 1.33 KB
/
UVa 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
#include <bits/stdc++.h>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define WHITE 0
#define GREY 1
#define BLACK 2
#define INF -1
vector<int> adj_l[105];
int _time, d[105], f[105], col[105];
stack <int> topo;
int V, E;
void init(){
for(int i = 0; i < 105; i++)
adj_l[i].clear();
mem(d, 0);
mem(f, 0);
mem(col, WHITE);
_time = 0;
}
void input(int u, int v){
adj_l[u].push_back(v);
}
void dfs(int src){
int u = src;
_time = _time + 1;
d[u] = _time;
col[u] = GREY;
for(int i = 0; i < adj_l[u].size(); i++){
int v = adj_l[u][i];
if(col[v] == WHITE)
dfs(v);
}
col[u] = BLACK;
_time = _time + 1;
f[u] = _time;
topo.push(u);
return;
}
int main(){
READ("UVa 10305.txt");
int u, v;
while(1){
init();
cin>>V>>E;
if(V == 0 && E == 0)
break;
for(int i = 0; i < E; i++){
cin>>u>>v;
input(u, v);
}
for(int i = 1; i <= V; i++){
if(col[i] == WHITE)
dfs(i);
}
cout<<topo.top();
topo.pop();
while(!topo.empty()){
cout<<" "<<topo.top();
topo.pop();
}
cout<<endl;
}
return 0;
}