結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー outlineoutline
提出日時 2021-03-05 21:55:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 1,819 ms
コンパイル使用メモリ 140,784 KB
実行使用メモリ 14,080 KB
最終ジャッジ日時 2024-04-16 09:22:03
合計ジャッジ時間 3,692 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 65 ms
9,088 KB
testcase_04 AC 65 ms
9,088 KB
testcase_05 AC 59 ms
9,088 KB
testcase_06 AC 63 ms
9,088 KB
testcase_07 AC 59 ms
9,216 KB
testcase_08 AC 37 ms
7,168 KB
testcase_09 AC 16 ms
5,376 KB
testcase_10 AC 17 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 27 ms
6,400 KB
testcase_13 AC 33 ms
6,912 KB
testcase_14 AC 35 ms
7,168 KB
testcase_15 AC 24 ms
6,144 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 52 ms
8,576 KB
testcase_19 AC 9 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 24 ms
5,888 KB
testcase_22 AC 20 ms
5,632 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 11 ms
5,504 KB
testcase_34 AC 63 ms
14,080 KB
testcase_35 AC 21 ms
7,808 KB
testcase_36 AC 5 ms
5,376 KB
testcase_37 AC 36 ms
8,952 KB
testcase_38 AC 32 ms
8,532 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 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    vector<vector<int>> g(N);
    for(int i = 1; i < N; ++i){
        int A, B;
        cin >> A >> B;
        --A, --B;
        g[A].emplace_back(B);
        g[B].emplace_back(A);
    }

    ll ans = (ll)N * N;
    vector<int> subtree_size(N, 1);
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> int {
        for(int to : g[from]){
            if(to != par){
                subtree_size[from] += self(self, to, from);
            }
        }
        ans += (ll)subtree_size[from] * (N - subtree_size[from]);
        for(int to : g[from]){
            if(to != par){
                ans += (ll)(N - subtree_size[to]) * subtree_size[to];
            }
        }
        return subtree_size[from];
    };
    dfs(dfs);
    cout << ans << endl;

    return 0;
}
0