-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockTree.java
More file actions
85 lines (84 loc) · 1.95 KB
/
ClockTree.java
File metadata and controls
85 lines (84 loc) · 1.95 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class ClockTree {
static int[] times = new int[100000];
static ArrayList<ArrayList<Integer>> AdjList = new ArrayList<ArrayList<Integer>>();
static int s0 = 0, s1 = 0, n0 = 0, n1 = 0;
static boolean[] visited;
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("clocktree.in"));
int a, b;
int N = Integer.parseInt(br.readLine());
String[] temp = br.readLine().split(" ");
for (int i = 0; i < N; i++)
{
times[i] = Integer.parseInt(temp[i]);
AdjList.add(new ArrayList<Integer>());
}
visited = new boolean[N];
for (int i = 0; i < N-1; i++)
{
temp = br.readLine().split(" ");
a = Integer.parseInt(temp[0])-1;
b = Integer.parseInt(temp[1])-1;
AdjList.get(a).add(b);
AdjList.get(b).add(a);
}
int total = 0;
dfs0(0, 0);
s0 %= 12; s0 += 12; s0 %= 12;
if (s0 == 0 || s0 == 1)
total += n0; //all the "even" nodes
Arrays.fill(visited, false);
dfs1(1, 0);
s1 %= 12; s1 += 12; s1 %= 12;
if (s1 == 0 || s1 == 1)
total += n1; //all the "odd" nodes
//System.out.println(total);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("clocktree.out")));
pw.println(total);
pw.close();
br.close();
}
public static void dfs0(int node, int parity)
{
if (visited[node])
return;
visited[node] = true;
if (parity == 0)
{
s0 += times[node];
n0++;
parity = 1;
}
else
{
s0 -= times[node];
parity = 0;
}
for (int neighbor: AdjList.get(node))
dfs0(neighbor, parity);
}
public static void dfs1(int node, int parity)
{
if (visited[node])
return;
visited[node] = true;
if (parity == 0)
{
s1 += times[node];
n1++;
parity = 1;
}
else
{
s1 -= times[node];
parity = 0;
}
for (int neighbor: AdjList.get(node))
dfs1(neighbor, parity);
}
}