結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-10-21 20:21:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 804 ms / 3,000 ms
コード長 1,499 bytes
コンパイル時間 2,646 ms
コンパイル使用メモリ 216,236 KB
実行使用メモリ 105,052 KB
最終ジャッジ日時 2023-09-12 04:44:46
合計ジャッジ時間 15,126 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 394 ms
54,668 KB
testcase_09 AC 362 ms
50,212 KB
testcase_10 AC 569 ms
75,340 KB
testcase_11 AC 303 ms
42,164 KB
testcase_12 AC 295 ms
39,500 KB
testcase_13 AC 322 ms
39,552 KB
testcase_14 AC 322 ms
43,468 KB
testcase_15 AC 365 ms
52,960 KB
testcase_16 AC 535 ms
71,904 KB
testcase_17 AC 692 ms
89,948 KB
testcase_18 AC 772 ms
100,844 KB
testcase_19 AC 773 ms
100,884 KB
testcase_20 AC 791 ms
105,052 KB
testcase_21 AC 796 ms
102,440 KB
testcase_22 AC 804 ms
103,700 KB
testcase_23 AC 279 ms
32,964 KB
testcase_24 AC 290 ms
36,332 KB
testcase_25 AC 204 ms
51,032 KB
testcase_26 AC 519 ms
64,216 KB
testcase_27 AC 222 ms
65,288 KB
testcase_28 AC 408 ms
77,796 KB
testcase_29 AC 491 ms
71,548 KB
testcase_30 AC 503 ms
71,308 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);
    vector<map<int, pair<int, int>>> ma;//{y,cnt}
    auto dfs = [&](auto&& f, int cur, int par, int XOR)->int {
        int l_idx = ma.size();
        ma.emplace_back();
        for (auto [to, weight] : g[cur]) {
            if (to == par)continue;
            int r_idx = f(f, to, cur, XOR ^ weight);
            ans -= ll(ma[r_idx][XOR].second) * (n - siz[to]);//case-2
            if (ma[l_idx].size() > ma[r_idx].size())swap(ma[l_idx], ma[r_idx]);
            for (auto [l_XOR, val] : ma[l_idx]) {
                auto [y, cnt] = val;
                if (l_XOR != XOR) {
                    ans -= ll(cnt) * ma[r_idx][l_XOR].first;//case-3-1
                    ans -= ll(ma[r_idx][l_XOR].second) * y;//case-3-2
                }
                ma[r_idx][l_XOR].first += y;
                ma[r_idx][l_XOR].second += cnt;
            }
            swap(ma[l_idx], ma[r_idx]);
            siz[cur] += siz[to];
        }
        ans -= ma[l_idx][XOR].first;//case-1
        ma[l_idx][XOR] = { siz[cur],1 };
        return l_idx;
    };
    dfs(dfs, 0, -1, 0);
    cout << ans << "\n";
    
    return 0;
}
0