-
Notifications
You must be signed in to change notification settings - Fork 0
/
Railways.java
78 lines (78 loc) · 2.35 KB
/
Railways.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
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Railways {
public static List<Integer>[] graph;
public static int[] colors;
public static void main(String[] args){
getGraph();
boolean result = checkGraph();
if(result){
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
public static boolean DFS(int startVertex){
Stack<Integer> stack = new Stack<>();
stack.push(startVertex);
while(!stack.empty()){
int v = stack.pop();
if(colors[v] == 0){
colors[v] = 1;
stack.push(v);
List<Integer> next = graph[v];
for(int w: next){
if(colors[w] == 0){
stack.push(w);
} else if(colors[w] == 1){
return false;
}
}
}
else if (colors[v] == 1){
colors[v] = 2;
}
}
return true;
}
public static boolean checkGraph(){
for (int i = 0; i < graph.length; i++) {
if (colors[i] == 0) {
if(!DFS(i)){
return false;
}
}
}
return true;
}
private static void getGraph(){
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
int cities = Integer.parseInt(reader.readLine());
graph = new ArrayList[cities];
for (int i = 0; i < cities; i++) {
List<Integer> info = new ArrayList<>();
graph[i] = info;
}
colors = new int[cities];
int n = cities - 1;
for (int i = 0; i < n; i++) {
String line = reader.readLine();
char[] roads = line.toCharArray();
for (int j = i + 1; j < cities; j++) {
Character letter = roads[j - i - 1];
if(letter.equals('B')){
graph[j].add(i);
} else {
graph[i].add(j);
}
}
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}