結果
| 問題 |
No.1769 Don't Stop the Game
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-11-03 14:28:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 722 ms / 3,000 ms |
| コード長 | 1,558 bytes |
| コンパイル時間 | 2,691 ms |
| コンパイル使用メモリ | 206,804 KB |
| 最終ジャッジ日時 | 2025-01-25 10:56:08 |
|
ジャッジサーバーID (参考情報) |
judge3 / 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);
auto dfs = [&](auto&& f, int cur, int par, int XOR)->unordered_map<int, pair<int, int>> {
unordered_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
ans -= ll(R[XOR].first) * R[XOR].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);
siz[cur] += siz[to];
}
if (L.count(XOR)) {
ans -= L[XOR].first;//case-1
}
L[XOR] = { siz[cur],1 };
if (cur == 0) {
for (auto [l_XOR, val] : L) {
auto [y, cnt] = val;
if (l_XOR != XOR)ans -= ll(y) * cnt;//case-3
}
}
else ans += siz[cur];//case-3
return L;
};
dfs(dfs, 0, -1, 0);
cout << ans << "\n";
return 0;
}