結果
| 問題 | No.1769 Don't Stop the Game | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-10-21 20:21:48 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 938 ms / 3,000 ms | 
| コード長 | 1,499 bytes | 
| コンパイル時間 | 2,443 ms | 
| コンパイル使用メモリ | 209,368 KB | 
| 最終ジャッジ日時 | 2025-01-25 02:32:35 | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 28 | 
ソースコード
#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;
}
            
            
            
        