結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-11-03 14:28:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 563 ms / 3,000 ms
コード長 1,558 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 214,616 KB
実行使用メモリ 61,808 KB
最終ジャッジ日時 2023-09-12 05:00:12
合計ジャッジ時間 12,467 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 279 ms
13,684 KB
testcase_09 AC 228 ms
13,032 KB
testcase_10 AC 356 ms
19,268 KB
testcase_11 AC 200 ms
11,128 KB
testcase_12 AC 295 ms
15,760 KB
testcase_13 AC 299 ms
16,012 KB
testcase_14 AC 305 ms
16,152 KB
testcase_15 AC 319 ms
15,864 KB
testcase_16 AC 364 ms
16,324 KB
testcase_17 AC 434 ms
19,372 KB
testcase_18 AC 509 ms
25,928 KB
testcase_19 AC 538 ms
26,940 KB
testcase_20 AC 548 ms
29,916 KB
testcase_21 AC 549 ms
26,996 KB
testcase_22 AC 563 ms
28,684 KB
testcase_23 AC 298 ms
16,208 KB
testcase_24 AC 299 ms
15,996 KB
testcase_25 AC 169 ms
16,344 KB
testcase_26 AC 315 ms
26,024 KB
testcase_27 AC 218 ms
52,468 KB
testcase_28 AC 326 ms
61,808 KB
testcase_29 AC 369 ms
53,488 KB
testcase_30 AC 341 ms
44,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<n;++i)

int main() {
    int n; cin >> n;
    vector<vector<pair<int, int>>> g(n);
    rep(i, n - 1) {
        int a, b, c; cin >> a >> b >> c;
        --a, --b;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    vector<int> siz(n, 1);
    ll ans = ll(n) * (n - 1);
    auto dfs = [&](auto&& f, int cur, int par, int XOR)->unordered_map<int, pair<int, int>> {
        unordered_map<int, pair<int, int>> L;//{y,cnt}
        for (auto [to, weight] : g[cur]) {
            if (to == par)continue;
            auto R = f(f, to, cur, XOR ^ weight);
            if (R.count(XOR)) {
                ans -= ll(R[XOR].second) * (n - siz[to]);//case-2
                ans -= ll(R[XOR].first) * R[XOR].second;//case-3
            }
            if (L.size() > R.size())swap(L, R);
            for (auto [l_XOR, val] : L) {
                auto [y, cnt] = val;
                R[l_XOR].first += y;
                R[l_XOR].second += cnt;
            }
            swap(L, R);
            siz[cur] += siz[to];
        }
        if (L.count(XOR)) {
            ans -= L[XOR].first;//case-1
        }
        L[XOR] = { siz[cur],1 };
        if (cur == 0) {
            for (auto [l_XOR, val] : L) {
                auto [y, cnt] = val;
                if (l_XOR != XOR)ans -= ll(y) * cnt;//case-3
            }
        }
        else ans += siz[cur];//case-3
        return L;
    };
    dfs(dfs, 0, -1, 0);
    cout << ans << "\n";

    return 0;
}
0