結果
問題 |
No.1769 Don't Stop the Game
|
ユーザー |
|
提出日時 | 2021-11-26 13:31:11 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 264 ms / 3,000 ms |
コード長 | 2,303 bytes |
コンパイル時間 | 2,357 ms |
コンパイル使用メモリ | 207,984 KB |
最終ジャッジ日時 | 2025-01-26 01:18:49 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; template<class T> using V = vector<T>; using VI = V<int>; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define all(a) a.begin(),a.end() inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template<class T> inline istream& operator>>(istream& is, V<T>& v) { for (auto& a : v)is >> a; return is; } template<class W> class weighted_graph { bool directed, zero_indexed_input; int n, m; V<V<pair<int, W>>> g; public: weighted_graph(int n, int m, bool directed = false, bool zero_indexed_input = false) : n(n), m(m), directed(directed), zero_indexed_input(zero_indexed_input), g(n) {}; friend istream& operator>>(istream& is, weighted_graph<W>& me) { FOR(i, 0, me.m) { int a, b, c; is >> a >> b >> c; if (!me.zero_indexed_input)--a, --b; me.g[a].emplace_back(b, c); if (!me.directed)me.g[b].emplace_back(a, c); } return is; } const V<pair<int, W>>& operator[](int i) { return g[i]; } int size() { return n; } }; void compress(VI& x) { VI y = x; sort(all(y)); y.erase(unique(all(y)), y.end()); for (int& a : x)a = lower_bound(all(y), a) - y.begin(); } int main() { init(); int n; cin >> n; weighted_graph<int> g(n, n - 1); cin >> g; VI x(n), sz(n, 1); auto dfs1 = [&](auto&& f, int cur, int par)->int { for (auto [to, weight] : g[cur]) { if (to == par)continue; x[to] = x[cur] ^ weight; sz[cur] += f(f, to, cur); } return sz[cur]; }; dfs1(dfs1, 0, -1); compress(x); ll ans = n * ll(n - 1); VI y(n), cnt(n); auto dfs2 = [&](auto&& f, int cur, int par)->void { ans -= y[x[cur]]; ans -= ll(sz[cur]) * cnt[x[cur]]; for (auto [to, weight] : g[cur]) { if (to == par)continue; int y_tmp = y[x[cur]], cnt_tmp = cnt[x[cur]]; y[x[cur]] = n - sz[to], cnt[x[cur]] = 1; f(f, to, cur); y[x[cur]] = y_tmp, cnt[x[cur]] = cnt_tmp; } y[x[cur]] += sz[cur]; ++cnt[x[cur]]; }; dfs2(dfs2, 0, -1); cout << ans << "\n"; return 0; }