-
Notifications
You must be signed in to change notification settings - Fork 0
/
mootube.java
77 lines (65 loc) · 1.84 KB
/
mootube.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
/*
ID: helena.6
LANG: JAVA
TASK: mootube
*/
import java.io.*;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class mootube {
static int n, q;
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("mootube.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("mootube.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
n = Integer.parseInt(st.nextToken());
q = Integer.parseInt(st.nextToken());
LinkedList<Edge>[] edges = new LinkedList[n];
for(int i = 0; i < n; i++)
edges[i] = new LinkedList<Edge>();
for (int i = 1; i < n; i++) {
st = new StringTokenizer(f.readLine());
int p = Integer.parseInt(st.nextToken())-1;
int q = Integer.parseInt(st.nextToken())-1;
int r = Integer.parseInt(st.nextToken());
edges[p].add(new Edge(q, r));
edges[q].add(new Edge(p, r));
}
for(int query = 0; query < q; query++) {
st = new StringTokenizer(f.readLine());
int threshold = Integer.parseInt(st.nextToken());
int start = Integer.parseInt(st.nextToken())-1;
int ret = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.add(start);
boolean[] seen = new boolean[n];
seen[start] = true;
while(!queue.isEmpty()) {
int curr = queue.removeFirst();
for(Edge e: edges[curr]) {
if(!seen[e.getDest()] && e.getWeight() >= threshold) {
seen[e.getDest()] = true;
queue.add(e.getDest());
ret++;
}
}
}
out.println(ret);
}
out.close();
}
}
class Edge {
private int dest;
private int weight;
public Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}
public int getDest() {
return dest;
}
public int getWeight() {
return weight;
}
}