結果

問題 No.2638 Initial fare
ユーザー GOTKAKOGOTKAKO
提出日時 2024-02-19 21:37:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 814 bytes
コンパイル時間 2,122 ms
コンパイル使用メモリ 207,404 KB
実行使用メモリ 35,124 KB
最終ジャッジ日時 2024-02-19 21:37:51
合計ジャッジ時間 6,145 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 140 ms
13,780 KB
testcase_05 AC 148 ms
14,404 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 122 ms
14,312 KB
testcase_08 AC 92 ms
13,996 KB
testcase_09 AC 103 ms
14,872 KB
testcase_10 AC 102 ms
14,900 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 127 ms
14,252 KB
testcase_13 AC 88 ms
13,404 KB
testcase_14 AC 108 ms
14,796 KB
testcase_15 AC 103 ms
14,916 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 122 ms
14,684 KB
testcase_18 AC 113 ms
14,244 KB
testcase_19 AC 138 ms
17,028 KB
testcase_20 AC 156 ms
15,728 KB
testcase_21 AC 144 ms
18,152 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 169 ms
35,124 KB
testcase_24 AC 175 ms
31,684 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 167 ms
25,796 KB
testcase_27 AC 174 ms
32,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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

    int N; cin >> N;
    vector<vector<int>> Graph(N);
    for(int i=0; i<N-1; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }

auto dp = [&](auto dp,int pos,int back) -> vector<long long> {
    long long ret = 0;
    long long one = 0,two = 0;
    for(auto to : Graph.at(pos)){
        if(to == back) continue;
        auto ko = dp(dp,to,pos);
        long long x = ko.at(0),y = ko.at(1),z = ko.at(2);
        ret += x+y+z+ko.back();
        ret += one*x+two*x+one*y;
        one++; two += y;
    }
    return {1,one,two,ret};
};
    vector<long long> result = dp(dp,0,-1);
    cout << result.back() << endl;
}
0