結果

問題 No.2638 Initial fare
ユーザー nono00nono00
提出日時 2024-02-19 22:36:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,261 bytes
コンパイル時間 2,931 ms
コンパイル使用メモリ 254,536 KB
実行使用メモリ 30,916 KB
最終ジャッジ日時 2024-02-19 22:36:21
合計ジャッジ時間 5,682 ms
ジャッジサーバーID
(参考情報)
judge16 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 93 ms
19,804 KB
testcase_05 AC 96 ms
20,680 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 79 ms
20,448 KB
testcase_08 AC 59 ms
19,744 KB
testcase_09 AC 68 ms
21,044 KB
testcase_10 AC 66 ms
21,260 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 83 ms
20,280 KB
testcase_13 AC 59 ms
18,796 KB
testcase_14 AC 70 ms
20,952 KB
testcase_15 AC 64 ms
21,272 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 71 ms
20,616 KB
testcase_18 AC 66 ms
20,176 KB
testcase_19 AC 84 ms
21,836 KB
testcase_20 AC 91 ms
21,100 KB
testcase_21 AC 86 ms
22,220 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 104 ms
30,916 KB
testcase_24 AC 112 ms
29,548 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 101 ms
26,528 KB
testcase_27 AC 120 ms
29,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

void solve() {
    using Data = std::array<long long, 4>;
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> graph(n);
    for (int i = 0; i + 1 < n; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    std::vector<Data> dp(n, {0, 0, 0, 0});
    long long ans = 0;
    auto dfs = [&](auto self, int u, int p) -> void {
        dp[u][0] = 1;
        for (auto v: graph[u]) {
            if (v == p) continue;
            self(self, v, u);
            for (int i = 0; i < 3; i++) {
                dp[u][i + 1] += dp[v][i];
            }
        }
        for (auto v: graph[u]) {
            if (v == p) continue;
            ans += (dp[u][1] - dp[v][0]) * dp[v][0];
            ans += (dp[u][2] - dp[v][1]) * dp[v][0];
            ans += (dp[u][1] - dp[v][0]) * dp[v][1];
        }
    };
    dfs(dfs, 0, 0);
    ans /= 2;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < 4; j++) {
            ans += dp[i][j];
        }
    }
    std::cout << ans << '\n';
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(16);
    int t = 1;

    while (t--) solve();
}
0