From 1ebbc5c129ed431cbba62a9990795e428387f15f Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Fri, 3 Oct 2025 20:40:31 +0900 Subject: [PATCH] =?UTF-8?q?[20251003]=20BOJ=20/=20G5=20/=20MooTube=20(Silv?= =?UTF-8?q?er)=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JHLEE325/202510/03 BOJ G5 MooTube (Silver).md | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 JHLEE325/202510/03 BOJ G5 MooTube (Silver).md diff --git a/JHLEE325/202510/03 BOJ G5 MooTube (Silver).md b/JHLEE325/202510/03 BOJ G5 MooTube (Silver).md new file mode 100644 index 00000000..bcbff6aa --- /dev/null +++ b/JHLEE325/202510/03 BOJ G5 MooTube (Silver).md @@ -0,0 +1,61 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + static class Node { + int to, cost; + Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int Q = Integer.parseInt(st.nextToken()); + + List[] graph = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>(); + + for (int i = 0; i < N - 1; i++) { + st = new StringTokenizer(br.readLine()); + int p = Integer.parseInt(st.nextToken()); + int q = Integer.parseInt(st.nextToken()); + int r = Integer.parseInt(st.nextToken()); + graph[p].add(new Node(q, r)); + graph[q].add(new Node(p, r)); + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < Q; i++) { + st = new StringTokenizer(br.readLine()); + int k = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + + boolean[] visited = new boolean[N + 1]; + Queue q = new LinkedList<>(); + visited[v] = true; + q.add(v); + int count = 0; + + while (!q.isEmpty()) { + int cur = q.poll(); + for (Node nxt : graph[cur]) { + if (!visited[nxt.to] && nxt.cost >= k) { + visited[nxt.to] = true; + q.add(nxt.to); + count++; + } + } + } + sb.append(count).append("\n"); + } + + System.out.print(sb); + } +} +```