結果
問題 | No.1769 Don't Stop the Game |
ユーザー |
|
提出日時 | 2021-10-22 21:10:48 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 763 ms / 3,000 ms |
コード長 | 3,972 bytes |
コンパイル時間 | 3,043 ms |
コンパイル使用メモリ | 216,108 KB |
最終ジャッジ日時 | 2025-01-25 03:04:22 |
ジャッジサーバーID (参考情報) |
judge1 / 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)template<class T> inline void print(const T & a) { cout << a << "\n"; }template<class T, class... Ts> inline void print(const T & a, const Ts&... ts) { cout << a << " "; print(ts...); }template<class T> inline void print(const vector<T>&v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }pair<int, int> operator+(const pair<int, int>& a, const pair<int, int>& b) {return { a.first + b.first,a.second + b.second };}pair<int, int> operator-(const pair<int, int>& a, const pair<int, int>& b) {return { a.first - b.first,a.second - b.second };}pair<int, int> operator+=(pair<int, int>& a, const pair<int, int>& b) {a.first += b.first;a.second += b.second;return a;}template<class T> class BinaryIndexedTree {int n;vector<T> dat;public:BinaryIndexedTree(int n) : n(n), dat(n + 1) {};void add(int i, T a) {for (++i; i <= n; i += i & -i)dat[i] += a;}T sum(int r) {// sum of [0,r)T res = {};for (; r; r -= r & -r)res += dat[r];return res;}T sum(int l, int r) {// sum of [l,r)if (l < 0 || n < r || l > r)return {};return sum(r) - sum(l);}};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);}int now = 0;vector<int> siz(n, 1), x(n), in(n), out(n);auto dfs0 = [&](auto&& f, int cur, int par)->void {in[cur] = now;++now;for (auto [to, weight] : g[cur]) {if (to == par)continue;x[to] = x[cur] ^ weight;f(f, to, cur);siz[cur] += siz[to];}out[cur] = now;};dfs0(dfs0, 0, -1);auto sorted_x = x;sort(sorted_x.begin(), sorted_x.end());sorted_x.erase(unique(sorted_x.begin(), sorted_x.end()), sorted_x.end());rep(i, n)x[i] = lower_bound(sorted_x.begin(), sorted_x.end(), x[i]) - sorted_x.begin();//compressvector<vector<int>> idxs(sorted_x.size());auto dfs1 = [&](auto&& f, int cur, int par)->void {idxs[x[cur]].push_back(in[cur]);for (auto [to, weight] : g[cur]) {if (to == par)continue;f(f, to, cur);}};dfs1(dfs1, 0, -1);vector<BinaryIndexedTree<pair<int, int>>> y_cnt;y_cnt.reserve(idxs.size());rep(i, idxs.size()) {sort(idxs[i].begin(), idxs[i].end());y_cnt.emplace_back(idxs[i].size());}ll ans = ll(n) * (n - 1);auto dfs2 = [&](auto&& f, int cur, int par)->void {int l = lower_bound(idxs[x[cur]].begin(), idxs[x[cur]].end(), in[cur]) - idxs[x[cur]].begin();int r = lower_bound(idxs[x[cur]].begin(), idxs[x[cur]].end(), out[cur]) - idxs[x[cur]].begin();for (auto [to, weight] : g[cur]) {if (to == par)continue;f(f, to, cur);}{auto [y, cnt] = y_cnt[x[cur]].sum(l, r);ans -= y;//case-1y_cnt[x[cur]].add(l, pair<int, int>(siz[cur] - y, 1 - cnt));}if (cur == 0) {rep(i, idxs.size())if (i != x[cur]) {auto [y, cnt] = y_cnt[i].sum(0, idxs[i].size());ans -= ll(y) * cnt;//case-3}}else {l = lower_bound(idxs[x[par]].begin(), idxs[x[par]].end(), in[cur]) - idxs[x[par]].begin();r = lower_bound(idxs[x[par]].begin(), idxs[x[par]].end(), out[cur]) - idxs[x[par]].begin();auto [y, cnt] = y_cnt[x[par]].sum(l, r);ans -= ll(cnt) * (n - siz[cur]);//case-2ans -= ll(y) * cnt;//case-3ans += siz[cur];//case-3}};dfs2(dfs2, 0, -1);cout << ans << "\n";return 0;}