結果
問題 | No.2800 Game on Tree Inverse |
ユーザー |
![]() |
提出日時 | 2024-06-26 01:20:11 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 661 ms / 4,000 ms |
コード長 | 3,041 bytes |
コンパイル時間 | 2,387 ms |
コンパイル使用メモリ | 207,552 KB |
最終ジャッジ日時 | 2025-02-22 00:34:39 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 64 |
ソースコード
#include<bits/stdc++.h> using namespace std; template <int MAX_LOG> struct binary_trie { struct node { node *ch[2] = {nullptr, nullptr}; int cnt = 0; node() {} }; public: node *root = nullptr; int xor_all = 0; binary_trie() : root(nullptr) {} void insert(int val){ val ^= xor_all; root = insert(root, val); } int count(int val){ node* now_node = root; val ^= xor_all; for (int b=MAX_LOG-1; b>=0; b--){ if (now_node == nullptr) break; now_node = now_node->ch[(val>>b)&1]; } return size(now_node); } int xth(int x){ node* now_node = root; assert(now_node != nullptr); int ret = 0; for (int b=MAX_LOG-1; b>=0; b--){ bool f = (xor_all>>b)&1; if (size(now_node->ch[f]) <= x){ x -= size(now_node->ch[f]); now_node = now_node->ch[f^1]; ret = (ret<<1)|(f^1); }else{ assert(1 <= size(now_node->ch[f])); now_node = now_node->ch[f]; ret = (ret<<1)|f; } } return ret ^ xor_all; } int mex(){ int ub = root->cnt; int lb = -1; while (ub - lb > 1){ int t = (ub + lb) / 2; if (xth(t) == t) lb = t; else ub = t; } return lb + 1; } private: node* insert(node* t, int val, int b = MAX_LOG - 1){ if (t == nullptr) t = new node; t->cnt++; if (b < 0) return t; bool f = (val>>b)&1; t->ch[f] = insert(t->ch[f], val, b-1); return t; } int size(node* t){ if (t == nullptr) return 0; return t->cnt; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<vector<int>> ikeru(n, vector<int>(0)); for (int i=0; i<n-1; i++){ int u, v; cin >> u >> v; u--; v--; ikeru[u].push_back(v); ikeru[v].push_back(u); } vector<int> dp(n); vector<binary_trie<20>> ep(n); auto dfs1 = [&](auto self, int i, int p) -> void { int x = 0; int mxval = 0; int mxind = i; for (int j: ikeru[i]){ if (j == p) continue; self(self, j, i); x ^= dp[j]; if (mxval < ep[j].root->cnt) { mxval = ep[j].root->cnt; mxind = j; } } ep[mxind].xor_all ^= dp[mxind]^x; for (int j: ikeru[i]){ if (j == p) continue; if (j == mxind) { continue; }else{ int mxz = ep[j].root->cnt; for (int z=0; z<mxz; z++){ int tar = ep[j].xth(z); if (ep[mxind].count(tar^x^dp[j]) == 0){ ep[mxind].insert(tar^x^dp[j]); } } } } if (ep[mxind].count(x) == 0){ ep[mxind].insert(x); } swap(ep[mxind], ep[i]); dp[i] = ep[i].mex(); }; dfs1(dfs1, 0, -1); vector<int> ans; auto dfs2 = [&](auto self, int i, int p, int g) -> void { int x = g; for (int j: ikeru[i]){ if (j == p) continue; x ^= dp[j]; } if (x == 0) ans.push_back(i); for (int j: ikeru[i]){ if (j == p) continue; self(self, j, i, x^dp[j]); } }; dfs2(dfs2, 0, -1, 0); if (ans.empty()){ cout << "Bob\n"; }else{ cout << "Alice\n"; int k = ans.size(); sort(ans.begin(), ans.end()); cout << k << '\n'; for (int x: ans){ cout << x+1 << ' '; } cout << '\n'; } }