結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー Manuel1024Manuel1024
提出日時 2021-04-28 22:57:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,361 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 74,992 KB
実行使用メモリ 16,232 KB
最終ジャッジ日時 2023-09-22 05:29:58
合計ジャッジ時間 6,129 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 109 ms
9,120 KB
testcase_04 AC 110 ms
9,216 KB
testcase_05 AC 104 ms
9,052 KB
testcase_06 AC 104 ms
9,060 KB
testcase_07 AC 104 ms
9,140 KB
testcase_08 AC 68 ms
6,988 KB
testcase_09 AC 29 ms
4,876 KB
testcase_10 AC 33 ms
5,200 KB
testcase_11 AC 16 ms
4,380 KB
testcase_12 AC 52 ms
6,268 KB
testcase_13 AC 62 ms
6,696 KB
testcase_14 AC 62 ms
7,012 KB
testcase_15 AC 48 ms
5,892 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 92 ms
8,640 KB
testcase_19 AC 14 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 48 ms
5,996 KB
testcase_22 AC 39 ms
5,472 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 20 ms
5,936 KB
testcase_34 AC 104 ms
16,232 KB
testcase_35 AC 41 ms
8,596 KB
testcase_36 AC 8 ms
4,376 KB
testcase_37 AC 63 ms
8,884 KB
testcase_38 AC 59 ms
8,640 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
using graph = vector<vector<P>>;

int dfs(graph &G, vector<bool> &isvisit, int cur, int parent){
    if(isvisit[cur]) return 0;
    isvisit[cur] = true;

    int ans = 1;
    for(auto &p: G[cur]){
        if(p.first == parent){
            p.second = -1;
            continue;
        }else{
            p.second = dfs(G, isvisit, p.first, cur);
            ans += p.second;
        }
    }
    return ans;
}

int main(){
    int n;
    cin >> n;
    graph G(n);
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(make_pair(b, -1));
        G[b].push_back(make_pair(a, -1));
    }
    //cerr << "input end" << endl;

    vector<bool> isvisit(n, false);
    dfs(G, isvisit, 0, -1);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        //for(auto &p: G[i]) cerr << "(" <<  p.first << ", " << p.second << ") ";
        //cerr << endl;
        ans += n;
        int childrenSum = 0;
        for(auto &p: G[i]){
            if(p.second != -1) childrenSum += p.second;
        }
        for(auto &p: G[i]){
            int xnum = p.second;
            if(xnum == -1) xnum = n-childrenSum-1;
            ans += (ll)(n-xnum) * xnum;
        }
    }

    cout << ans << endl;
    return 0;
}
0