-
Notifications
You must be signed in to change notification settings - Fork 6
/
10305.cpp
51 lines (46 loc) · 881 Bytes
/
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
// Steven Kester Yuwono - UVa 10305
#include <cstdio>
#include <vector>
#include <cstring>
#include <stack>
#define MAX 104
using namespace std;
vector<int> adjList[MAX];
stack<int> stk;
bool visited[MAX];
void init() {
for (int i=0;i<MAX;i++){
adjList[i].clear();
}
}
void dfs(int start) {
if(visited[start]) return;
visited[start] = true;
for(int i=0;i<(int)adjList[start].size();i++) {
dfs(adjList[start][i]);
}
stk.push(start);
}
int main() {
int n,m;
while ((scanf("%d %d",&n,&m)==2) && (n||m)) {
init();
for(int i=0;i<m;i++){
int from,to;
scanf("%d %d",&from,&to);
adjList[from].push_back(to);
}
memset(visited,false,sizeof(visited));
for(int i=1;i<=n;i++){
if(!visited[i]) {
dfs(i);
}
}
while(!stk.empty()){
if (stk.size()==1) printf("%d\n",stk.top());
else printf("%d ",stk.top());
stk.pop();
}
}
return 0;
}