結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-10-21 20:30:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 684 ms / 3,000 ms
コード長 1,454 bytes
コンパイル時間 2,421 ms
コンパイル使用メモリ 215,508 KB
実行使用メモリ 86,580 KB
最終ジャッジ日時 2023-09-12 04:44:30
合計ジャッジ時間 13,916 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 340 ms
14,020 KB
testcase_09 AC 316 ms
13,856 KB
testcase_10 AC 480 ms
20,696 KB
testcase_11 AC 252 ms
11,216 KB
testcase_12 AC 273 ms
16,132 KB
testcase_13 AC 272 ms
16,092 KB
testcase_14 AC 293 ms
15,856 KB
testcase_15 AC 333 ms
15,944 KB
testcase_16 AC 457 ms
16,736 KB
testcase_17 AC 607 ms
20,352 KB
testcase_18 AC 635 ms
29,368 KB
testcase_19 AC 669 ms
28,964 KB
testcase_20 AC 684 ms
33,128 KB
testcase_21 AC 674 ms
29,956 KB
testcase_22 AC 680 ms
31,452 KB
testcase_23 AC 259 ms
15,916 KB
testcase_24 AC 253 ms
15,912 KB
testcase_25 AC 150 ms
16,668 KB
testcase_26 AC 376 ms
28,856 KB
testcase_27 AC 226 ms
74,480 KB
testcase_28 AC 432 ms
86,580 KB
testcase_29 AC 413 ms
58,448 KB
testcase_30 AC 419 ms
58,332 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)->map<int, pair<int, int>> {
        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
            }
            if (L.size() > R.size())swap(L, R);
            for (auto [l_XOR, val] : L) {
                auto [y, cnt] = val;
                if (l_XOR != XOR && R.count(l_XOR)) {
                    ans -= ll(cnt) * R[l_XOR].first;//case-3-1
                    ans -= ll(R[l_XOR].second) * y;//case-3-2
                }
                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 };
        return move(L);
    };
    dfs(dfs, 0, -1, 0);
    cout << ans << "\n";
    
    return 0;
}
0