-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlysonTree.java
More file actions
66 lines (66 loc) · 1.9 KB
/
AlysonTree.java
File metadata and controls
66 lines (66 loc) · 1.9 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
import java.io.*;
import java.util.*;
public class AlysonTree {
static int[] distance;
static int[] values;
static ArrayList<ArrayList<Integer>> AdjList = new ArrayList<ArrayList<Integer>>();
static ArrayList<ArrayList<Integer>> weights = new ArrayList<ArrayList<Integer>>();
static boolean[] visited;
static boolean[] connected;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
values = new int[N]; distance = new int[N]; visited = new boolean[N];
connected = new boolean[N];
String[] temp = br.readLine().split(" ");
for (int i = 0; i < N; i++)
values[i] = Integer.parseInt(temp[i]);
int node, weight;
for (int i = 0; i < N; i++)
{
AdjList.add(new ArrayList<Integer>());
weights.add(new ArrayList<Integer>());
}
for (int i = 0; i < N-1; i++)
{
temp = br.readLine().split(" ");
node = Integer.parseInt(temp[0])-1;
weight = Integer.parseInt(temp[1]);
AdjList.get(i+1).add(node);
AdjList.get(node).add(i+1);
weights.get(i+1).add(weight);
weights.get(node).add(weight);
}
treeTraversal(0);
delete(0);
int count = 0;
for (boolean in: connected)
if (!in)
count++;
System.out.println(count);
}
public static void treeTraversal(int node)
{
visited[node] = true;
for (int i = 0; i < AdjList.get(node).size(); i++)
{
if (visited[AdjList.get(node).get(i)])
continue;
distance[AdjList.get(node).get(i)] = Math.max(distance[node] + weights.get(node).get(i), 0);
treeTraversal(AdjList.get(node).get(i));
}
}
public static void delete(int node)
{
connected[node] = true;
for (int i = 0; i < AdjList.get(node).size(); i++)
{
if (connected[AdjList.get(node).get(i)])
continue;
if (distance[AdjList.get(node).get(i)] > values[AdjList.get(node).get(i)])
continue;
delete(AdjList.get(node).get(i));
}
}
}