Skip to content

Commit b40ad45

Browse files
authored
Merge pull request #1028 from AlgorithmWithGod/JHLEE325
[20251003] BOJ / G5 / MooTube (Silver) / 이준희
2 parents 30ac053 + 1ebbc5c commit b40ad45

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static class Node {
7+
int to, cost;
8+
Node(int to, int cost) {
9+
this.to = to;
10+
this.cost = cost;
11+
}
12+
}
13+
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
int N = Integer.parseInt(st.nextToken());
19+
int Q = Integer.parseInt(st.nextToken());
20+
21+
List<Node>[] graph = new ArrayList[N + 1];
22+
for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>();
23+
24+
for (int i = 0; i < N - 1; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
int p = Integer.parseInt(st.nextToken());
27+
int q = Integer.parseInt(st.nextToken());
28+
int r = Integer.parseInt(st.nextToken());
29+
graph[p].add(new Node(q, r));
30+
graph[q].add(new Node(p, r));
31+
}
32+
33+
StringBuilder sb = new StringBuilder();
34+
for (int i = 0; i < Q; i++) {
35+
st = new StringTokenizer(br.readLine());
36+
int k = Integer.parseInt(st.nextToken());
37+
int v = Integer.parseInt(st.nextToken());
38+
39+
boolean[] visited = new boolean[N + 1];
40+
Queue<Integer> q = new LinkedList<>();
41+
visited[v] = true;
42+
q.add(v);
43+
int count = 0;
44+
45+
while (!q.isEmpty()) {
46+
int cur = q.poll();
47+
for (Node nxt : graph[cur]) {
48+
if (!visited[nxt.to] && nxt.cost >= k) {
49+
visited[nxt.to] = true;
50+
q.add(nxt.to);
51+
count++;
52+
}
53+
}
54+
}
55+
sb.append(count).append("\n");
56+
}
57+
58+
System.out.print(sb);
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)