-
Notifications
You must be signed in to change notification settings - Fork 4
/
TheNecklace.java
97 lines (87 loc) · 3.2 KB
/
TheNecklace.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TheNecklace {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private static final int MAX_COL = 51;
private final List<List<Integer>> nodes;
private final List<List<Boolean>> traversed = new ArrayList<>();
private final List<int[]> path = new ArrayList<>();
private TheNecklace(List<List<Integer>> nodes) {
this.nodes = nodes;
for (int i = 0; i < MAX_COL; ++i) {
traversed.add(new ArrayList<>());
}
boolean eulerian = true;
int src = -1;
for (int i = 0; i < nodes.size(); ++i) {
List<Integer> node = nodes.get(i);
if (node.size() % 2 != 0) {
eulerian = false;
break;
}
for (int j = 0; j < node.size(); ++j) {
traversed.get(i).add(false);
}
if (src == -1 && node.size() > 0) {
src = i;
}
}
if (eulerian) {
traverse(src, 0);
}
}
private void traverse(int src, int pos) {
for (int i = 0; i < nodes.get(src).size(); ++i) {
if (traversed.get(src).get(i)) {
continue;
}
traversed.get(src).set(i, true);
int dst = nodes.get(src).get(i);
for (int j = 0; j < nodes.get(dst).size(); ++j) {
if (nodes.get(dst).get(j) == src &&
!traversed.get(dst).get(j)) {
traversed.get(dst).set(j, true);
break;
}
}
path.add(pos, new int[] { src, dst });
traverse(dst, pos + 1);
}
}
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(reader.readLine().trim());
for (int i = 0; i < n; ++i) {
int m = Integer.parseInt(reader.readLine().trim());
List<List<Integer>> nodes = new ArrayList<>();
for (int j = 0; j < MAX_COL; ++j) {
nodes.add(new ArrayList<>());
}
for (int j = 0; j < m; ++j) {
List<Integer> node = stream(
reader.readLine().trim().split(" "))
.filter(x -> !x.equals(""))
.map(Integer::parseInt)
.collect(toList());
nodes.get(node.get(0)).add(node.get(1));
nodes.get(node.get(1)).add(node.get(0));
}
TheNecklace necklace = new TheNecklace(nodes);
System.out.println("Case #" + (i + 1));
if (necklace.path.size() != 0) {
necklace.path
.forEach(x -> System.out.println(x[0] + " " + x[1]));
} else {
System.out.println("some beads may be lost");
}
if (i < n - 1) {
System.out.println();
}
}
}
}