-
Notifications
You must be signed in to change notification settings - Fork 0
/
clocktree.java
96 lines (80 loc) · 2.07 KB
/
clocktree.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 java.io.*;
import java.util.*;
public class clocktree {
static int N;
static Room[] rooms;
static ArrayList<Integer> sortedRooms;
public static void main(String[] args) throws IOException{
String file = "clocktree";
BufferedReader br = new BufferedReader(new FileReader(file + ".in"));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
rooms = new Room[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
int time = Integer.parseInt(st.nextToken());
rooms[i] = new Room(time % 12);
}
for(int i = 0; i < N - 1; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
rooms[a].linkedTo.add(b);
rooms[b].linkedTo.add(a);
}
int res = 0;
sortedRooms = new ArrayList<Integer>();
for(int p = 0; p < N; p++) {
reset();
sortedRooms.clear();
bfs(p);
for(int i = N - 1; i >= 0; i--) {
Room curr = rooms[sortedRooms.get(i)];
if(curr.parent != -1) {
int difference = (12 - curr.tempTime) % 12;
rooms[curr.parent].tempTime = (rooms[curr.parent].tempTime + difference) % 12;
}
}
if(rooms[p].tempTime == 0 || rooms[p].tempTime == 1) res++;
}
PrintWriter out = new PrintWriter(new File(file + ".out"));
out.println(res);
out.close();
}
public static void reset() {
for(Room r: rooms) {
r.visited = false;
r.tempTime = r.time;
r.parent = -1;
}
}
public static void bfs(int p) {
rooms[p].visited = true;
sortedRooms.add(p);
int x = 0;
while(x < sortedRooms.size()) {
int curr = sortedRooms.get(x);
for(int r: rooms[curr].linkedTo) {
if(!rooms[r].visited) {
rooms[r].parent = curr;
rooms[r].visited = true;
sortedRooms.add(r);
}
}
x++;
}
}
public static class Room {
int time;
int parent;
boolean visited;
int tempTime;
ArrayList<Integer> linkedTo;
Room(int t){
time = t;
linkedTo = new ArrayList<Integer>();
int parent = -1;
visited = false;
}
}
}