結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             forest3
                         | 
                    
| 提出日時 | 2021-08-09 11:22:35 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 285 ms / 2,000 ms | 
| コード長 | 620 bytes | 
| コンパイル時間 | 1,849 ms | 
| コンパイル使用メモリ | 171,832 KB | 
| 実行使用メモリ | 15,360 KB | 
| 最終ジャッジ日時 | 2024-09-21 10:12:20 | 
| 合計ジャッジ時間 | 11,007 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<int> d;
int dfs( int v, int p )
{
	int res = 0;
	for( int x : g[v] ) {
		if( x == p ) continue;
		res += dfs( x, v ) + 1;
	}
	d[v] = res + 1;
	return res;
}
int main()
{
	int N, Q;
	cin >> N >> Q;
	g = vector<vector<int>>( N );
	d = vector<int>( N );
	for( int i = 0; i < N - 1; i++ ) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		g[a].push_back( b );
		g[b].push_back( a );
	}
	dfs( 0, -1 );
	long long ans = 0;
	for( int i = 0; i < Q; i++ ) {
		int p;
		long long x;
		cin >> p >> x;
		p--;
		ans += d[p] * x;
		cout << ans << endl;
	}
}
            
            
            
        
            
forest3