結果

問題 No.1637 Easy Tree Query
コンテスト
ユーザー yuri
提出日時 2021-08-12 17:13:50
言語 Java
(openjdk 26.0.2.1 + ACL)
コンパイル:
javac -J-Duser.language=en -encoding UTF8 -cp /opt/aclib/ac_library.jar _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true -cp .:/opt/aclib/ac_library.jar _class_
結果
TLE  
実行時間 -
コード長 880 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,479 ms
コンパイル使用メモリ 88,388 KB
実行使用メモリ 85,320 KB
最終ジャッジ日時 2026-09-08 07:28:21
合計ジャッジ時間 10,857 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other TLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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