結果

問題 No.1637 Easy Tree Query
ユーザー OmameBeansOmameBeans
提出日時 2021-08-06 22:30:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 170,892 KB
実行使用メモリ 15,632 KB
最終ジャッジ日時 2023-10-17 05:12:05
合計ジャッジ時間 9,595 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,156 KB
testcase_01 AC 5 ms
6,156 KB
testcase_02 AC 260 ms
9,560 KB
testcase_03 AC 4 ms
6,156 KB
testcase_04 AC 64 ms
7,976 KB
testcase_05 AC 219 ms
7,448 KB
testcase_06 AC 145 ms
7,184 KB
testcase_07 AC 78 ms
6,172 KB
testcase_08 AC 51 ms
7,712 KB
testcase_09 AC 170 ms
8,768 KB
testcase_10 AC 101 ms
7,184 KB
testcase_11 AC 237 ms
9,032 KB
testcase_12 AC 235 ms
8,504 KB
testcase_13 AC 39 ms
7,184 KB
testcase_14 AC 109 ms
8,768 KB
testcase_15 AC 220 ms
9,296 KB
testcase_16 AC 160 ms
9,560 KB
testcase_17 AC 67 ms
7,184 KB
testcase_18 AC 202 ms
7,448 KB
testcase_19 AC 199 ms
7,712 KB
testcase_20 AC 249 ms
8,504 KB
testcase_21 AC 121 ms
7,976 KB
testcase_22 AC 252 ms
9,560 KB
testcase_23 AC 63 ms
7,184 KB
testcase_24 AC 100 ms
7,976 KB
testcase_25 AC 223 ms
7,448 KB
testcase_26 AC 250 ms
8,504 KB
testcase_27 AC 281 ms
9,296 KB
testcase_28 AC 160 ms
7,448 KB
testcase_29 AC 193 ms
8,240 KB
testcase_30 AC 60 ms
8,240 KB
testcase_31 AC 176 ms
6,156 KB
testcase_32 AC 101 ms
7,448 KB
testcase_33 AC 210 ms
7,184 KB
testcase_34 AC 77 ms
15,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}

vecveci G(100010);
veci dp(100010,0);
veci seen(100010);

void dfs(int i) {
    seen[i] = 1;
    for(int ni : G[i]) {
        if(seen[ni]) continue;
        dfs(ni);
        dp[i] += dp[ni];
    }
    dp[i]++;
}

int main() {
    int N,Q; cin >> N >> Q;
    ll tot = 0;
    REP(i,N-1) {
        int a,b; cin >> a >> b;
        a--,b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0);

    // REP(i,N) cout << dp[i] << " ";
    // cout << endl;

    REP(i,Q) {
        int p; cin >> p; p--;
        ll x; cin >> x;
        tot += x*dp[p];
        cout << tot << endl;
    }
    return 0;
}
0