-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTeleportersPath.java
More file actions
75 lines (64 loc) · 2.27 KB
/
TeleportersPath.java
File metadata and controls
75 lines (64 loc) · 2.27 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
69
70
71
72
73
74
75
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class TeleportersPath {
static ArrayList<ArrayList<Integer>> al = new ArrayList<>();
static HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
static HashMap<Integer, HashSet<Integer>> degree = new HashMap<>();
static HashMap<Integer, Integer> in = new HashMap<>();
static HashMap<Integer, Integer> out = new HashMap<>();
static Stack<Integer> ans = new Stack<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
for(int i = 0; i < n; i++) {
al.add(new ArrayList<>());
map.put(i, new HashSet<>());
out.put(i, 0);
in.put(i, 0);
}
for(int i = 0; i < m; i++) {
String str[] = br.readLine().split(" ");
int u = Integer.parseInt(str[0]) - 1;
int v = Integer.parseInt(str[1]) - 1;
al.get(u).add(v);
in.put(v, in.get(v) + 1);
out.put(u, out.get(u) + 1);
}
boolean flag = false;
for(int i = 1; i < n-2; i++) {
if(in.get(i) != out.get(i)) {
flag = true;
break;
}
}
if(out.get(0) - in.get(0) != 1) {
System.out.println("IMPOSSIBLE");
} else if (in.get(n-1) - out.get(n-1) != 1) {
System.out.println("IMPOSSIBLE");
} else if(flag) System.out.println("IMPOSSIBLE");
else {
dfs(0);
while(!ans.isEmpty()) {
System.out.print((ans.pop()+1) +" ");
}
}
}
public static void dfs(int node) {
if(out.get(node) == 0) {
ans.push(node);
return;
}
for(int v : al.get(node)) {
if(!map.get(node).contains(v)) {
map.get(node).add(v);
out.put(node, out.get(node) -1);
dfs(v);
}
}
ans.push(node);
}
}