結果

問題 No.1637 Easy Tree Query
ユーザー yuriyuri
提出日時 2021-08-12 17:13:50
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 880 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 79,468 KB
実行使用メモリ 105,332 KB
最終ジャッジ日時 2024-04-09 21:27:07
合計ジャッジ時間 7,812 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
60,060 KB
testcase_01 AC 116 ms
53,504 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.concurrent.DelayQueue;

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;
		for (int i = 1; i < N; i++) {
			A = scan.nextInt() - 1;
			B = scan.nextInt() - 1;
			nodes[A].branch.add(B);
		}

		int now;
		long ans = 0;
		Queue<Integer> nextNodes = new ArrayDeque<>();
		for (int i = 0; i < Q; i++) {
			A = scan.nextInt() - 1;
			B = scan.nextInt();
			nextNodes.add(A);
			while (!nextNodes.isEmpty()) {
				now = nextNodes.poll();
				ans += B;

				for (int key : nodes[now].branch) {
					nextNodes.add(key);
				}
			}
			System.out.println(ans);
		}

	}
}

class Node {
	Set<Integer> branch = new HashSet<>();
	int cost = 0;
}
0