結果

問題 No.1637 Easy Tree Query
ユーザー milkcoffeemilkcoffee
提出日時 2021-06-25 11:41:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,897 bytes
コンパイル時間 3,846 ms
コンパイル使用メモリ 234,884 KB
実行使用メモリ 14,092 KB
最終ジャッジ日時 2023-10-17 04:34:12
合計ジャッジ時間 8,833 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,732 KB
testcase_01 AC 3 ms
4,732 KB
testcase_02 AC 159 ms
11,188 KB
testcase_03 AC 3 ms
4,732 KB
testcase_04 AC 32 ms
8,020 KB
testcase_05 AC 141 ms
7,228 KB
testcase_06 AC 90 ms
6,436 KB
testcase_07 AC 52 ms
5,116 KB
testcase_08 AC 26 ms
7,228 KB
testcase_09 AC 95 ms
9,340 KB
testcase_10 AC 65 ms
6,172 KB
testcase_11 AC 134 ms
9,868 KB
testcase_12 AC 138 ms
8,812 KB
testcase_13 AC 21 ms
6,172 KB
testcase_14 AC 53 ms
9,604 KB
testcase_15 AC 123 ms
10,132 KB
testcase_16 AC 84 ms
10,660 KB
testcase_17 AC 40 ms
6,172 KB
testcase_18 AC 128 ms
7,228 KB
testcase_19 AC 123 ms
7,492 KB
testcase_20 AC 148 ms
8,548 KB
testcase_21 AC 70 ms
8,020 KB
testcase_22 AC 143 ms
10,924 KB
testcase_23 AC 37 ms
6,700 KB
testcase_24 AC 53 ms
8,020 KB
testcase_25 AC 141 ms
6,964 KB
testcase_26 AC 152 ms
9,076 KB
testcase_27 AC 163 ms
10,660 KB
testcase_28 AC 99 ms
6,700 KB
testcase_29 AC 112 ms
8,548 KB
testcase_30 AC 27 ms
8,548 KB
testcase_31 AC 122 ms
4,732 KB
testcase_32 AC 60 ms
6,964 KB
testcase_33 AC 135 ms
6,172 KB
testcase_34 AC 31 ms
14,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>

#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
//constexpr ll mod = 1000000007;
constexpr ll mod = 998244353;

vll seen(100001);
vll cnt(100001);

void dfs(vvll &G, ll v){
    ll res=1;
    seen[v]=1;
    for(auto e:G[v]){
        if(seen[e]==1) continue;
        dfs(G,e);
        res+=cnt[e];
    }
    cnt[v]=res;
}

void solve(){
    ll n,q,sum=0,ans=0;
    cin>>n>>q;
    if(n<2||n>100000||q<1||q>100000){
        cout<<"Error"<<endl;
        return;
    }
    vvll G(n);
    rep(i,n-1){
        ll a,b;
        cin>>a>>b;
        if(a>n||a<1||b>n||b<1){
            cout<<"Error"<<endl;
            return;
        }
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(G,0);
    rep(i,q){
        ll p,x;
        cin>>p>>x;
        if(p<1||p>n||x<1||x>100000000){
            cout<<"Error"<<endl;
            return;
        }
        p--;
        sum+=cnt[p]*x;
        cout<<sum<<endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // cout << fixed << setprecision(16);
    //ll T;
    //cin>>T;
    //rep(ca,T) 
    solve();
    
}
0