結果
問題 |
No.1637 Easy Tree Query
|
ユーザー |
|
提出日時 | 2021-08-12 17:21:10 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 760 bytes |
コンパイル時間 | 2,345 ms |
コンパイル使用メモリ | 77,092 KB |
実行使用メモリ | 78,404 KB |
最終ジャッジ日時 | 2024-10-01 22:02:27 |
合計ジャッジ時間 | 47,414 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 1 WA * 31 TLE * 1 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int Q = scan.nextInt(); Node[] nodes = new Node[N]; for (int i = 0; i < N; i++) { nodes[i] = new Node(); } int A, B; int tmp; for (int i = 1; i < N; i++) { A = scan.nextInt() - 1; B = scan.nextInt() - 1; nodes[A].chiledNum++; tmp = A; while (nodes[tmp].root != -1) { tmp = nodes[tmp].root; nodes[tmp].chiledNum++; } nodes[B].root = A; } long ans = 0; for (int i = 0; i < Q; i++) { A = scan.nextInt() - 1; B = scan.nextInt(); ans += B * nodes[A].chiledNum; System.out.println(ans); } } } class Node { int root = -1; int chiledNum = 1; }