結果

問題 No.1637 Easy Tree Query
ユーザー yuri
提出日時 2021-08-12 17:13:50
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 880 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 79,968 KB
実行使用メモリ 41,400 KB
最終ジャッジ日時 2024-10-01 21:54:44
合計ジャッジ時間 8,785 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other TLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

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