結果

問題 No.1637 Easy Tree Query
ユーザー NanashimaNanashima
提出日時 2021-08-06 21:54:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 2,051 ms
コンパイル使用メモリ 178,696 KB
実行使用メモリ 10,620 KB
最終ジャッジ日時 2023-10-17 05:08:45
合計ジャッジ時間 9,848 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 263 ms
10,620 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 63 ms
7,264 KB
testcase_05 AC 218 ms
6,196 KB
testcase_06 AC 146 ms
5,484 KB
testcase_07 AC 79 ms
4,348 KB
testcase_08 AC 53 ms
6,472 KB
testcase_09 AC 170 ms
9,088 KB
testcase_10 AC 98 ms
5,060 KB
testcase_11 AC 233 ms
9,592 KB
testcase_12 AC 231 ms
8,136 KB
testcase_13 AC 37 ms
5,196 KB
testcase_14 AC 108 ms
9,208 KB
testcase_15 AC 216 ms
9,840 KB
testcase_16 AC 162 ms
10,520 KB
testcase_17 AC 64 ms
5,196 KB
testcase_18 AC 203 ms
6,192 KB
testcase_19 AC 198 ms
6,736 KB
testcase_20 AC 243 ms
7,924 KB
testcase_21 AC 121 ms
7,292 KB
testcase_22 AC 254 ms
10,600 KB
testcase_23 AC 62 ms
5,484 KB
testcase_24 AC 98 ms
7,284 KB
testcase_25 AC 222 ms
6,000 KB
testcase_26 AC 247 ms
8,696 KB
testcase_27 AC 287 ms
10,320 KB
testcase_28 AC 156 ms
5,772 KB
testcase_29 AC 191 ms
7,792 KB
testcase_30 AC 60 ms
7,792 KB
testcase_31 AC 173 ms
4,348 KB
testcase_32 AC 97 ms
6,208 KB
testcase_33 AC 210 ms
5,152 KB
testcase_34 AC 72 ms
10,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF ((1LL<<62)-(1LL<<31))
#define all(a)  (a).begin(),(a).end()
#define rall(a)  (a).rbegin(),(a).rend()
typedef long long ll;
typedef pair<ll,ll> pl;
typedef tuple<ll,ll,ll> tupl;

int main()
{
    int n,q;
    cin >> n >> q;
    vector<vector<int>> G(n,vector<int> ());
    vector<int> cnt(n,1);
    rep(i,n-1) {
        int a,b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    queue<int> qu;
    qu.push(0);
    vector<ll> dist(n,INF);
    vector<int> u;
    dist[0]=0;
    while(!qu.empty()) {
        int v=qu.front();
        qu.pop();
        u.push_back(v);
        for(auto nv:G[v]) {
            if(dist[nv]!=INF) continue;
            dist[nv]=dist[v]+1;
            qu.push(nv);
        }
    }
    reverse(all(u));
    for(auto nu:u) {
        for(auto x:G[nu]) {
            if(dist[x]==dist[nu]-1) cnt[x]+=cnt[nu];
        }
    }
    ll sum=0;
    rep(i,q) {
        ll p,x;
        cin >> p >> x;
        p--;
        sum+=cnt[p]*x;
        cout << sum << endl;
    }
    return 0;
}
0