結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー hitonanode
提出日時 2021-02-06 14:36:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 718 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 74,664 KB
最終ジャッジ日時 2025-01-18 13:33:14
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 TLE * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

// 想定 TLE (二乗オーダー)
#include <iostream>
#include <vector>
using namespace std;

vector<int> to[100000];

int main() {

    int N;
    cin >> N;

    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }

    long long ret = 0;
    auto dfs = [&](auto self, int now, int prv) -> int {
        int subtree_size = 1;
        for (auto nxt : to[now]) {
            if (nxt != prv) subtree_size += self(self, nxt, now);
        }
        ret += subtree_size;
        return subtree_size;
    };
    for (int s = 0; s < N; s++) dfs(dfs, s, -1);
    cout << ret << '\n';
}
0