UOJ266. 銆愭竻鍗庨泦璁?016銆慉lice鍜孊ob鍙堝湪鐜╂父鎴?鍗氬紙璁?01-trie)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UOJ266. 銆愭竻鍗庨泦璁?016銆慉lice鍜孊ob鍙堝湪鐜╂父鎴?鍗氬紙璁?01-trie)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/mat' title='mat'>mat lan cst 鍒犻櫎 oid 闂 pre
UOJ266. 銆愭竻鍗庨泦璁?016銆慉lice鍜孊ob鍙堝湪鐜╂父鎴?鍗氬紙璁?01-trie)
棰樼洰澶ф剰
鏈?(n) 涓妭鐐癸紝(m) 鏉¤竟锛?span class="math inline">(0 le m le n-1)锛夛紝鏋勬垚鑻ュ共妫?strong>鏈夋牴鏍?/strong>锛屾瘡妫垫爲鐨勬牴鑺傜偣鏄杩為€氬潡鍐呯紪鍙锋渶灏忕殑鐐广€?br> Alice 鍜?Bob 杞祦鎿嶄綔锛圓lice 鍏堟墜锛夛紝姣忓洖鍚堥€夋嫨涓€涓病鏈夎鍒犻櫎鐨勮妭鐐?(x)锛屽皢 (x) 鍙婂叾鎵€鏈夌鍏堝叏閮ㄥ垹闄わ紝涓嶈兘鎿嶄綔鐨勪汉杈撱€?br> 闇€瑕佹敞鎰忕殑鏄紝鏍戠殑褰㈡€佹槸鍦ㄤ竴寮€濮嬪氨纭畾濂界殑锛屽垹闄よ妭鐐逛笉浼氬奖鍝嶅墿浣欒妭鐐圭埗浜插拰鍎垮瓙鐨勫叧绯汇€?br> 姣斿锛?-3-2 杩欐牱涓€鏉¢摼锛? 鍙风偣鏄牴鑺傜偣锛屽垹闄?1 鍙风偣涔嬪悗锛? 鍙风偣杩樻槸 2 鍙风偣鐨勭埗鑺傜偣銆?br> 鍋囪 Alice 鍜?Bob 閮借冻澶熻仾鏄庯紝闂?Alice 鏈夋病鏈夊繀鑳滅瓥鐣ャ€?/p>
鏁版嵁鑼冨洿
瀵逛簬(10 \%)鐨勬暟鎹紝(m=0);
瀵逛簬(20 \%)鐨勬暟鎹紝(1 le n le 20);
瀵逛簬(40 \%)鐨勬暟鎹紝(1 le n le 10^2)锛?br> 瀵逛簬(60 \%)鐨勬暟鎹紝(1 le n le 10^3)锛?br> 瀵逛簬(100 \%)鐨勬暟鎹紝(1 le T le 10, 1 le n le 10^5, sum{n} le 2 imes 10^5, 0 le m le n-1)锛岃緭鍏ユ暟鎹繚璇佷笉浼氬舰鎴愮幆锛屼笖姣忔5鏍戠殑澶у皬(le 5 imes 10^4)銆?/p>
瑙i鎬濊矾
棣栧厛鍙互鐪嬪嚭杩欐槸涓€涓崥寮堣闂锛岃€冭檻鐢?SG 鍑芥暟瑙e喅
濡備綍绠楀嚭涓€涓瓙鏍戠殑 SG 鍊硷紵
鏋氫妇鍒犻櫎鐨勭涓€涓偣 x锛屽皢鏍硅妭鐐瑰埌 x 鐨勮矾寰勫垹鎺夛紝鍙戠幇鏍戝彉鎴愪簡妫灄锛屼篃灏辨槸褰撳墠灞€闈㈢殑涓€涓悗缁х姸鎬侊紝閭d箞妫灄涓墍鏈夌殑鏍?SG 鍊煎紓鎴栬捣鏉ユ彃鍏ラ泦鍚堜腑鍗冲彲锛屾渶鍚庡闆嗗悎涓殑鏁板彇 (mex) 鍗冲彲锛屾椂闂村鏉傚害 (Theta(N^2))
鑰冭檻鏁版嵁缁撴瀯浼樺寲
鑰冭檻涓€涓瓙鏍戝悜涓婂悎骞剁殑杩囩▼锛屽彂鐜板彧闇€灏嗗瓙鏍戝紓鎴栦笂鍏朵粬瀛愭爲鐨?SG 鍊煎苟鍔犲叆鍒伴泦鍚堬紝骞跺姞鍏ュ垹闄ゆ牴鑺傜偣鐨?SG 鍊硷紝涔熷氨鏄淮鎶ゆ暣浣撳紓鎴栵紝鍚堝苟锛屾彃鍏ワ紝涓嶉毦鍙戠幇鍙互鐢?01-trie 瑙e喅
浠g爜
#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;
template <typename T>
void read(T &x) {
x = 0; bool f = 0;
char c = getchar();
for (;!isdigit(c);c=getchar()) if (c==鈥?鈥? f=1;
for (;isdigit(c);c=getchar()) x=x*10+(c^48);
if (f) x=-x;
}
template<typename F>
inline void write(F x)
{
static short st[30];short tp=0;
if(x<0) putchar(鈥?鈥?,x=-x;
do st[++tp]=x%10,x/=10; while(x);
while(tp) putchar(鈥?鈥榺st[tp--]);
putchar(鈥榎n鈥?;
}
template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }
template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }
const int N = 400500; int vis[N];
int tag[N*8], ch[N*10][2], cnt, n, m, T;
int siz[N*8], h[N], ne[N<<1], to[N<<1], tot;
inline void add(int x, int y) {
ne[++tot] = h[x], to[h[x] = tot] = y;
}
#define ls ch[x][0]
#define rs ch[x][1]
inline void spread(int x, int dep) {
if ((tag[x] >> dep) & 1) swap(ls, rs);
tag[ls] ^= tag[x], tag[rs] ^= tag[x];
tag[x] = 0;
}
void insert(int x, int p) {
int pp = p;
for (int i = 16;i >= 0; i--) {
int di = (x >> i) & 1; spread(p, i);
if (!ch[p][di]) ch[p][di] = ++cnt;
p = ch[p][di];
}
if (siz[p]) return; p = pp;
for (int i = 16;i >= 0; i--) {
int di = (x >> i) & 1;
p = ch[p][di]; siz[p]++;
}
}
int merge(int x, int y, int dep) {
if (!x || !y) return x | y;
if (dep == -1) return siz[x] = 1, x;
spread(x, dep), spread(y, dep);
ls = merge(ls, ch[y][0], dep - 1);
rs = merge(rs, ch[y][1], dep - 1);
siz[x] = siz[ls] + siz[rs];
return x;
}
int query(int x, int dep) {
if (!x) return 0; spread(x, dep);
if (siz[ls] >= (1 << dep)) return query(rs, dep - 1) + (1 << dep);
return query(ls, dep - 1);
}
int res[N], sg[N], Rt[N];
void dfs2(int x, int fa) {
vis[x] = 1, tag[x] = res[x] = 0, Rt[x] = x;
for (int i = h[x]; i; i = ne[i]) {
int y = to[i]; if (y == fa) continue;
dfs2(y, x); res[x] ^= sg[y];
}
insert(res[x], Rt[x]);
for (int i = h[x]; i; i = ne[i]) {
int y = to[i]; if (y == fa) continue;
tag[Rt[y]] ^= res[x] ^ sg[y];
Rt[x] = merge(Rt[x], Rt[y], 16);
}
sg[x] = query(Rt[x], 16);
}
void work(void) {
read(n), read(m); cnt = n, tot = 0;
memset(vis, 0, sizeof(vis));
memset(h, 0, sizeof(h));
memset(ch, 0, sizeof(ch));
memset(siz, 0, sizeof(siz));
for (int i = 1, x, y;i <= m; i++)
read(x), read(y), add(x, y), add(y, x);
int ans = 0;
for (int i = 1;i <= n; i++)
if (!vis[i]) dfs2(i, 0), ans ^= sg[i];
puts(ans ? "Alice" : "Bob");
}
int main() {
for (read(T); T--; ) work();
return 0;
}
以上是关于UOJ266. 銆愭竻鍗庨泦璁?016銆慉lice鍜孊ob鍙堝湪鐜╂父鎴?鍗氬紙璁?01-trie)的主要内容,如果未能解决你的问题,请参考以下文章
UOJ 266 清华集训2016Alice和Bob又在玩游戏
uoj#266. 清华集训2016Alice和Bob又在玩游戏(博弈论)
[BZOJ4730][清华集训2016][UOJ266] Alice和Bob又在玩游戏