結果

問題 No.1637 Easy Tree Query
ユーザー korogisu
提出日時 2021-08-06 22:06:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,101 bytes
コンパイル時間 4,440 ms
コンパイル使用メモリ 255,824 KB
最終ジャッジ日時 2025-01-23 15:31:06
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 WA * 10 TLE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
using ll = long long;
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; }

const int INF = 1 << 30;

void dfs(int v, vector<bool> &seen,vector<vector<int>>& G) {
    if(seen[v]) return;
    seen[v] = true;
    for(int& nv: G[v]) dfs(nv, seen, G);
}


int main() {
    int N, Q;
    cin >> N >> Q;
    vector<vector<int>> T(N);
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        T[a].emplace_back(b);
    }

    ll ans = 0;
    vector<bool> seen;
    for (int i = 0; i < Q; i++) {
        int p, x;
        cin >> p >> x;
        p--;
        seen.assign(N,false);
        dfs(p,seen,T);
        int cnt = count(ALL(seen), true);
        ans += (ll)cnt * x;
        cout << ans << endl;
    }
    
    return 0;
}
0