-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCC'01S3.java
More file actions
68 lines (53 loc) · 1.62 KB
/
Copy pathCCC'01S3.java
File metadata and controls
68 lines (53 loc) · 1.62 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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
ArrayList<int[]> roads = new ArrayList<int[]>();
ArrayList<int[]> disconnect = new ArrayList<int[]>();
int edges = 0;
for (int x = 0; x <= 27; x++){
list.add(new ArrayList<Integer>());
}
while (!input.equals("**")){
int[] a = {(int) input.charAt(0) - 64, (int) input.charAt(1) - 64};
roads.add(a);
edges++;
input = sc.nextLine();
}
for(int x = 0; x < edges; x++){
for (int y = 0; y < roads.size(); y++){
if (y != x){
list.get(roads.get(y)[0]).add(roads.get(y)[1]);
list.get(roads.get(y)[1]).add(roads.get(y)[0]);
}
}
LinkedList<Integer> queue = new LinkedList<Integer>();
boolean[] visited = new boolean[list.size()];
queue.add(1);
visited[1] = true;
while(!queue.isEmpty()) {
int cur = queue.pop();
for (int i = 0; i < list.get(cur).size(); i++){
if (!visited[list.get(cur).get(i)]){
visited[list.get(cur).get(i)] = true;
queue.push(list.get(cur).get(i));
}
}
}
if (visited[2] == false){
disconnect.add(roads.get(x));
}
for (int y = 0; y <= 27; y++){
list.get(y).clear();
}
}
for (int x = 0; x < disconnect.size(); x++){
System.out.println((char) (disconnect.get(x)[0] + 64) + "" + (char) (disconnect.get(x)[1] + 64));
}
System.out.println("There are " + disconnect.size() +" disconnecting roads.");
}
}