結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー yuji9511yuji9511
提出日時 2021-03-06 01:03:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 2,152 bytes
コンパイル時間 2,295 ms
コンパイル使用メモリ 205,092 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-04-16 16:48:19
合計ジャッジ時間 4,621 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 67 ms
10,624 KB
testcase_04 AC 62 ms
10,752 KB
testcase_05 AC 68 ms
10,880 KB
testcase_06 AC 71 ms
10,880 KB
testcase_07 AC 72 ms
10,624 KB
testcase_08 AC 41 ms
8,320 KB
testcase_09 AC 19 ms
5,632 KB
testcase_10 AC 21 ms
5,888 KB
testcase_11 AC 12 ms
5,376 KB
testcase_12 AC 32 ms
7,296 KB
testcase_13 AC 37 ms
7,936 KB
testcase_14 AC 39 ms
8,192 KB
testcase_15 AC 29 ms
7,040 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 64 ms
9,984 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 28 ms
7,040 KB
testcase_22 AC 24 ms
6,528 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 13 ms
6,656 KB
testcase_34 AC 70 ms
18,304 KB
testcase_35 AC 24 ms
9,600 KB
testcase_36 AC 5 ms
5,376 KB
testcase_37 AC 39 ms
10,624 KB
testcase_38 AC 33 ms
10,064 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;}
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve(){
    ll N;
    cin >> N;
    vector<vll> tree(N);
    rep(i,0,N-1){
        ll a,b;
        cin >> a >> b;
        a--; b--;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    vll v(N, 0), sum(N,0);
    auto dfs = [&](auto dfs, ll cur, ll par) -> void {
        for(auto &e: tree[cur]){
            if(e == par) continue;
            dfs(dfs, e, cur);
            v[cur] += v[e];
        }
        v[cur]++;
    };

    auto dfs2 = [&](auto dfs2, ll cur, ll par) -> void {
        for(auto &e: tree[cur]){
            if(e == par) continue;
            dfs2(dfs2, e, cur);
            sum[cur] += sum[e];
            sum[cur] += v[e];
        }
        sum[cur]++;
    };


    dfs(dfs, 0, -1);
    dfs2(dfs2, 0, -1);

    ll ans = 0;

    auto calc = [&](auto calc, ll cur, ll par) -> void {
        ans += sum[cur];
        for(auto &e: tree[cur]){
            if(e == par) continue;
            ll t1 = sum[cur], t2 = sum[e];
            ll val = sum[cur] - N - sum[e];
            sum[e] += N - v[e];
            sum[cur] = val + N - v[e];
            sum[e] += sum[cur]; 
            calc(calc, e, cur);
            sum[cur] = t1;
            sum[e] = t2;

        }
    };
    calc(calc, 0, -1);
    print(ans);
    

}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
0