-
Notifications
You must be signed in to change notification settings - Fork 69
/
RoundTrip.java
78 lines (66 loc) Β· 2.21 KB
/
RoundTrip.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
//issue 153
import java.util.*;
public class RoundTrip {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Number of cities
int m = sc.nextInt(); // Number of roads
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
// Read roads and build the adjacency list
for (int i = 0; i < m; i++) {
int a = sc.nextInt() - 1; // Adjust for 0-based indexing
int b = sc.nextInt() - 1; // Adjust for 0-based indexing
adj.get(a).add(b);
adj.get(b).add(a);
}
sc.close();
List<Integer> path = new ArrayList<>();
boolean[] visited = new boolean[n];
int startCity = -1;
// Find a valid starting city
for (int i = 0; i < n; i++) {
if (!visited[i] && adj.get(i).size() > 1) {
startCity = i;
break;
}
}
if (startCity == -1) {
System.out.println("IMPOSSIBLE");
return;
}
// Construct the round trip
path.add(startCity);
visited[startCity] = true;
int currentCity = startCity;
while (true) {
int nextCity = -1;
for (int neighbor : adj.get(currentCity)) {
if (!visited[neighbor]) {
nextCity = neighbor;
break;
}
}
if (nextCity == -1) {
// No unvisited neighbor found, check if we can return to the start
if (adj.get(currentCity).contains(startCity)) {
path.add(startCity);
break;
} else {
System.out.println("IMPOSSIBLE");
return;
}
}
path.add(nextCity);
visited[nextCity] = true;
currentCity = nextCity;
}
// Output the round trip
System.out.println(path.size());
for (int city : path) {
System.out.print((city + 1) + " "); // Adjust for 1-based indexing
}
}
}