結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-10-22 16:23:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 753 ms / 3,000 ms
コード長 1,955 bytes
コンパイル時間 2,531 ms
コンパイル使用メモリ 220,508 KB
実行使用メモリ 75,624 KB
最終ジャッジ日時 2023-09-12 04:46:37
合計ジャッジ時間 15,250 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 362 ms
15,040 KB
testcase_09 AC 326 ms
14,900 KB
testcase_10 AC 510 ms
21,764 KB
testcase_11 AC 268 ms
11,956 KB
testcase_12 AC 317 ms
17,156 KB
testcase_13 AC 325 ms
17,488 KB
testcase_14 AC 352 ms
17,612 KB
testcase_15 AC 381 ms
17,548 KB
testcase_16 AC 485 ms
18,200 KB
testcase_17 AC 621 ms
21,904 KB
testcase_18 AC 690 ms
30,956 KB
testcase_19 AC 716 ms
30,776 KB
testcase_20 AC 753 ms
34,584 KB
testcase_21 AC 748 ms
31,536 KB
testcase_22 AC 752 ms
33,004 KB
testcase_23 AC 304 ms
17,612 KB
testcase_24 AC 320 ms
17,620 KB
testcase_25 AC 162 ms
17,900 KB
testcase_26 AC 484 ms
30,476 KB
testcase_27 AC 239 ms
63,088 KB
testcase_28 AC 513 ms
75,624 KB
testcase_29 AC 493 ms
53,972 KB
testcase_30 AC 494 ms
53,956 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), x(n);
    auto dfs0 = [&](auto&& f, int cur, int par)->void {
        for (auto [to, weight] : g[cur]) {
            if (to == par)continue;
            x[to] = x[cur] ^ weight;
            f(f, to, cur);
            siz[cur] += siz[to];
        }
    };
    dfs0(dfs0, 0, -1);
    auto sorted_x = x;
    sort(sorted_x.begin(), sorted_x.end());
    rep(i, n)x[i] = lower_bound(sorted_x.begin(), sorted_x.end(), x[i]) - sorted_x.begin();//compress

    ll ans = ll(n) * (n - 1);
    auto dfs1 = [&](auto&& f, int cur, int par)->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);
            if (R.count(x[cur])) {
                ans -= ll(R[x[cur]].second) * (n - siz[to]);//case-2
                ans -= ll(R[x[cur]].first) * R[x[cur]].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);
        }
        if (L.count(x[cur])) {
            ans -= L[x[cur]].first;//case-1
        }
        L[x[cur]] = { siz[cur],1 };
        if (cur == 0) {
            for (auto [l_XOR, val] : L) {
                auto [y, cnt] = val;
                if (l_XOR != x[cur])ans -= ll(y) * cnt;//case-3
            }
        }
        else ans += siz[cur];//case-3
        return L;
    };
    dfs1(dfs1, 0, -1);
    cout << ans << "\n";

    return 0;
}
0