結果

問題 No.1637 Easy Tree Query
ユーザー snrnsidysnrnsidy
提出日時 2021-08-09 10:40:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 608 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 203,464 KB
実行使用メモリ 15,724 KB
最終ジャッジ日時 2023-10-21 08:30:20
合計ジャッジ時間 8,422 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,964 KB
testcase_01 AC 3 ms
5,968 KB
testcase_02 AC 59 ms
9,484 KB
testcase_03 AC 3 ms
5,912 KB
testcase_04 AC 29 ms
7,832 KB
testcase_05 AC 44 ms
7,308 KB
testcase_06 AC 30 ms
6,960 KB
testcase_07 AC 14 ms
6,052 KB
testcase_08 AC 21 ms
7,432 KB
testcase_09 AC 54 ms
8,700 KB
testcase_10 AC 22 ms
6,760 KB
testcase_11 AC 66 ms
8,992 KB
testcase_12 AC 57 ms
8,300 KB
testcase_13 AC 14 ms
6,808 KB
testcase_14 AC 46 ms
8,764 KB
testcase_15 AC 65 ms
9,128 KB
testcase_16 AC 66 ms
9,464 KB
testcase_17 AC 18 ms
6,804 KB
testcase_18 AC 42 ms
7,308 KB
testcase_19 AC 44 ms
7,576 KB
testcase_20 AC 57 ms
8,236 KB
testcase_21 AC 37 ms
7,880 KB
testcase_22 AC 79 ms
9,536 KB
testcase_23 AC 20 ms
6,992 KB
testcase_24 AC 35 ms
7,888 KB
testcase_25 AC 45 ms
7,200 KB
testcase_26 AC 63 ms
8,432 KB
testcase_27 AC 79 ms
9,376 KB
testcase_28 AC 34 ms
7,112 KB
testcase_29 AC 50 ms
8,124 KB
testcase_30 AC 33 ms
8,100 KB
testcase_31 AC 26 ms
5,984 KB
testcase_32 AC 27 ms
7,244 KB
testcase_33 AC 37 ms
6,780 KB
testcase_34 AC 37 ms
15,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

vector <int> adj[100005];
int dp[100005];

int dfs(int now,int prev)
{
	dp[now] = 1;
	for(auto next : adj[now])
	{
		if(prev==next)
		{
			continue;
		}
		dp[now] += dfs(next,now);
	}
	return dp[now];
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n,q,a,b;

	cin >> n >> q;

	for(int i=0;i<n-1;i++)
	{
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	dfs(1,0);

	long long int sum = 0;
	for(int i=0;i<q;i++)
	{
		cin >> a >> b;
		long long int res = dp[a];
		res*=b;
		sum += res;
		cout << sum << '\n';
	}
	return 0;
}
0