結果

問題 No.1637 Easy Tree Query
コンテスト
ユーザー yuri
提出日時 2021-08-12 17:21:10
言語 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_
結果
WA  
実行時間 -
コード長 760 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,466 ms
コンパイル使用メモリ 86,148 KB
実行使用メモリ 65,372 KB
最終ジャッジ日時 2026-09-08 07:35:54
合計ジャッジ時間 33,680 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 1 WA * 31 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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;
}
0