結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-10-21 13:24:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 720 ms / 3,000 ms
コード長 1,448 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 214,804 KB
実行使用メモリ 77,232 KB
最終ジャッジ日時 2023-09-12 04:43:45
合計ジャッジ時間 13,908 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 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 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 349 ms
13,820 KB
testcase_09 AC 321 ms
13,788 KB
testcase_10 AC 499 ms
20,800 KB
testcase_11 AC 263 ms
11,000 KB
testcase_12 AC 276 ms
15,664 KB
testcase_13 AC 283 ms
16,064 KB
testcase_14 AC 294 ms
15,916 KB
testcase_15 AC 332 ms
15,880 KB
testcase_16 AC 468 ms
16,716 KB
testcase_17 AC 621 ms
20,656 KB
testcase_18 AC 665 ms
29,488 KB
testcase_19 AC 693 ms
29,068 KB
testcase_20 AC 720 ms
33,028 KB
testcase_21 AC 693 ms
29,940 KB
testcase_22 AC 701 ms
31,448 KB
testcase_23 AC 259 ms
15,760 KB
testcase_24 AC 267 ms
15,948 KB
testcase_25 AC 151 ms
16,352 KB
testcase_26 AC 392 ms
29,052 KB
testcase_27 AC 218 ms
64,676 KB
testcase_28 AC 450 ms
77,232 KB
testcase_29 AC 423 ms
53,880 KB
testcase_30 AC 448 ms
53,920 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 L;
    };
    dfs(dfs, 0, -1, 0);
    cout << ans << "\n";
    
    return 0;
}
0