結果

問題 No.1637 Easy Tree Query
ユーザー 👑 ygussanyygussany
提出日時 2021-08-06 21:23:52
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 926 bytes
コンパイル時間 1,538 ms
コンパイル使用メモリ 29,996 KB
実行使用メモリ 7,012 KB
最終ジャッジ日時 2023-10-17 04:40:05
合計ジャッジ時間 4,769 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,588 KB
testcase_01 AC 2 ms
4,588 KB
testcase_02 AC 54 ms
7,012 KB
testcase_03 AC 2 ms
4,588 KB
testcase_04 AC 18 ms
5,308 KB
testcase_05 AC 42 ms
4,804 KB
testcase_06 AC 28 ms
4,696 KB
testcase_07 AC 15 ms
4,596 KB
testcase_08 AC 15 ms
4,924 KB
testcase_09 AC 38 ms
6,152 KB
testcase_10 AC 20 ms
4,672 KB
testcase_11 AC 49 ms
6,436 KB
testcase_12 AC 47 ms
5,768 KB
testcase_13 AC 10 ms
4,680 KB
testcase_14 AC 28 ms
6,212 KB
testcase_15 AC 46 ms
6,564 KB
testcase_16 AC 39 ms
6,892 KB
testcase_17 AC 15 ms
4,680 KB
testcase_18 AC 38 ms
4,800 KB
testcase_19 AC 38 ms
5,068 KB
testcase_20 AC 48 ms
5,700 KB
testcase_21 AC 27 ms
5,360 KB
testcase_22 AC 54 ms
6,960 KB
testcase_23 AC 15 ms
4,700 KB
testcase_24 AC 23 ms
5,368 KB
testcase_25 AC 42 ms
4,720 KB
testcase_26 AC 49 ms
5,888 KB
testcase_27 AC 57 ms
6,812 KB
testcase_28 AC 30 ms
4,712 KB
testcase_29 AC 39 ms
5,592 KB
testcase_30 AC 17 ms
5,576 KB
testcase_31 AC 30 ms
4,592 KB
testcase_32 AC 21 ms
4,744 KB
testcase_33 AC 38 ms
4,676 KB
testcase_34 AC 23 ms
7,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v;
} list;

int main()
{
	int i, N, Q, u, w;
	list *adj[100001] = {}, e[200001], *p;
	scanf("%d %d", &N, &Q);
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d", &u, &w);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int par[100001] = {}, q[100001], head, tail;
	par[1] = 1;
	q[0] = 1;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] == 0) {
				par[w] = u;
				q[tail++] = w;
			}
		}
	}
	
	int size[100001] = {};
	for (head--; head > 0; head--) {
		u = q[head];
		size[u]++;
		size[par[u]] += size[u];
	}
	size[1]++;
	
	long long ans = 0;
	for (i = 1; i <= Q; i++) {
		scanf("%d %d", &u, &w);
		ans += (long long)size[u] * w;
		printf("%lld\n", ans);
	}
	fflush(stdout);
	return 0;
}
0