题目:

  P3727曼哈顿计划E

分析:

  大长题面容易给人一种不可做的错觉,但是这题考的知识点都是我们熟悉的。

  稍加分析我们可以得到,我们可以把每个点当成一个单独的游戏,如果k=1,就是简单的nim游戏,这样,当多个游戏放在一起的时候,我们就可以根据一条链的权值异或和来判断必胜必败。

  这个给我们启发,根据SG定理(应该是这个定理?)当我们选一条链,根据这条链上所有点的SG函数的异或和,可以判断胜负。

  所以我们可以对于每个k想办法求点的sg函数,就可以用点分治解决这个题。

  怎么求sg函数?(打表找规律啊),或者如果你有办法推也可以。

 代码:

 #include<bits/stdc++.h>
#define ms(a,x) memset(a,x,sizeof(a))
using namespace std;
const int N=,M=1e7;
struct node{int y,nxt;}e[N*];
int t,n,k,s,c=,hs,pd,nt,h[N];
int w[N],siz[N],vis[N],tp[N],tt[N];
void add(int x,int y){
e[++c]=(node){y,h[x]};h[x]=c;
e[++c]=(node){x,h[y]};h[y]=c;
} int sg1(int x){return x;}
int sg2(int x){return (x+)%(s+)?x&:;}
int sg3(int x){return x/s;}
int sg4(int x){
switch(x%){
case : return x-;
case : return x+;
default : return x;
}
} void init(){
ms(h,);ms(vis,);c=;
} void gs(int x,int fa){
siz[x]=;
for(int i=h[x],y;i;i=e[i].nxt)
if((y=e[i].y)!=fa&&!vis[y])
gs(y,x),siz[x]+=siz[y];
} int gg(int x,int fa){
for(int i=h[x],y;i;i=e[i].nxt)
if((y=e[i].y)!=fa&&!vis[y]&&siz[y]>=hs)
return gg(y,x);return x;
} void dfs(int x,int fa,int o){
tt[nt]=x;tp[nt++]=o;
for(int i=h[x],y;i;i=e[i].nxt)
if((y=e[i].y)!=fa&&!vis[y]) dfs(y,x,o^w[y]);
} struct Hash{
static const int P1=,P2=;
int h1(int x){return x%P1;}
int h2(int x){return x%P2;}
int t1[P1],t2[P2],st[N],top;
Hash(){ms(t1,-);ms(t2,-);}
int find(int x){
return t1[h1(x)]==x||t2[h2(x)]==x;
} void insert(int x){
if(find(x)) return ;
int h=h2(x);st[top++]=x;
if(t2[h]==-) t2[h]=x;
else while(~x){
h=h1(x);swap(x,t1[h]);
if(x==-) return ;
h=h2(x);swap(x,t2[h]);
}
} void del(int x){
if(t1[h1(x)]==x) t1[h1(x)]=-;
else t2[h2(x)]=-;
} void clear(){
while(top) del(st[--top]);
}
}H;void dc(int x){
gs(x,);hs=siz[x]>>;int g;
g=gg(x,);vis[g]=;H.insert();
for(int i=h[g],y;i;i=e[i].nxt)
if(!vis[y=e[i].y]){
nt=;dfs(y,,w[y]);
for(int j=;j<nt;j++)
if(H.find(w[g]^tp[j]))
{pd=;break;}
for(int j=;j<nt;j++)
H.insert(tp[j]);
} H.clear();
for(int i=h[g],y;i&&!pd;i=e[i].nxt)
if(!vis[y=e[i].y]) dc(y);return ;
} int main(){
scanf("%d",&t);while(t--){
init();scanf("%d",&n);
for(int i=,y,x;i<n;i++)
scanf("%d%d",&x,&y),add(x,y);
for(int i=;i<=n;i++)
scanf("%d",&w[i]);scanf("%d",&k);
int (*sg)(int);
if(k==) sg=sg1;
else if(k==) scanf("%d",&s),sg=sg2;
else if(k==) scanf("%d",&s),sg=sg3;
else sg=sg4;pd=;
for(int i=;i<=n;i++)
if(!(w[i]=sg(w[i]))) pd=;
if(!pd) dc();
puts(pd?"Mutalisk ride face how to lose?":
"The commentary cannot go on!");
} return ;
}

博弈论+点分治

Luogu P3727 曼哈顿计划E 点分治+hash的更多相关文章

  1. BZOJ4598: [Sdoi2016]模式字符串(点分治 hash)

    题意 题目链接 Sol 直接考虑点分治+hash匹配 设\(up[i]\)表示\(dep \% M = i\)的从下往上恰好与前\(i\)位匹配的个数 \(down\)表示\(dep \% M = i ...

  2. BZOJ.4598.[SDOI2016]模式字符串(点分治 Hash)

    LOJ BZOJ 洛谷 点分治.考虑如何计算过\(rt\)的答案. 记\(pre[i]\)表示(之前的)子树内循环匹配了\(S\)的前缀\(i\)的路径有多少,\(suf[i]\)表示(之前的)子树内 ...

  3. 【BZOJ4598】[Sdoi2016]模式字符串 树分治+hash

    [BZOJ4598][Sdoi2016]模式字符串 Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每 ...

  4. [luogu P5349] 幂 解题报告 (分治FFT)

    interlinkage: https://www.luogu.org/problemnew/show/P5349 description: solution: 设$g(x)=\sum_{n=0}^{ ...

  5. P3727 曼哈顿计划E

    点分治+SG函数还真是令人意外的组合啊 思路 这道题看到找一条满足条件的链,想到点分治 看到博弈,想到SG函数 然后就变成一道SG函数+点分治的题了 然后1e9的SG函数怎么搞?当然是打表了 然后各种 ...

  6. Luogu 4721 【模板】分治 FFT

    还不会这题的多项式求逆的算法. 发现每一项都是一个卷积的形式,那么我们可以使用$NTT$来加速,直接做是$O(n^2logn)$的,我们考虑如何加速转移. 可以采用$cdq$分治的思想,对于区间$[l ...

  7. BZOJ4598 [Sdoi2016]模式字符串 【点分治 + hash】

    题目 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m 的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有多少对结点< ...

  8. BZOJ 2229 / Luogu P3329 [ZJOI2011]最小割 (分治最小割板题)

    题面 求所有点对的最小割中<=c的数量 分析 分治最小割板题 首先,注意这样一个事实:如果(X,Y)是某个s1-t1最小割,(Z,W)是某个s2-t2最小割,那么X∩Z.X∩W.Y∩Z.Y∩W这 ...

  9. [Luogu P4178]Tree 题解(点分治+平衡树)

    题目大意 给定一棵树,边带权,问有多少点对满足二者间距离$\leq K$,$n \leq 40000$. 题解 点分治专题首杀!$Jackpot!$ (本来看着题意比较简单想捡个软柿子捏,结果手断了… ...

随机推荐

  1. Sublime Text 报“Pylinter could not automatically determined the path to lint.py

    Pylinter could not automatically determined the path to lint.py. please provide one in the settings ...

  2. 重构学习day01 类型码 类型码的上层建筑 与类型码相关的重构方法 1.使用子类代替类型码 2.使用状态或策略模式代替类型码

    名词:类型码 类型码的上层建筑 重构方法 1.使用子类代替类型码 2.使用状态/策略模式代替类型码 类中存在方法把某个字段当作条件,根据字段值的不同,进行不同的处理.(自定义概念)则这个字段叫做:类型 ...

  3. H - String painter

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  4. Zynq7000开发系列-6(QT开发环境搭建:Ubuntu、Zynq)

    操作系统:Ubuntu14.04.5 LTS 64bit Qt:Qt 5.4.2 (qt-opensource-linux-x64-5.4.2.run.qt-everywhere-opensource ...

  5. 缺少mscvr100.dll

    最后使用百度电脑专家修复好的!

  6. bzoj1492 [NOI2007]货币兑换Cash【cdq分治】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1492 推荐博客:http://www.cnblogs.com/zig-zag/archive ...

  7. NFX UNISTACK 介绍

    学习.NET Core和ASP.NET Core,偶然搜索到NFX UNISTACK,现翻译一下Readme,工程/原文:https://github.com/aumcode/nfx NFX Serv ...

  8. 17995 Stupid thief 组合数学

    17995 Stupid thief 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 Description A stupid thie ...

  9. JQueryUI基础知识学习

    JQueryUI官网 http://jqueryui.com/ 菜鸟教程 http://www.runoob.com/jqueryui/jqueryui-tutorial.html

  10. ubuntu下sublime屏蔽alt键显示顶部菜单栏

    在sulime下喜欢把方向键映的上下左右映射为alt+i/k/j/l ,按单词移动alt+h/; 但是在ubuntu下按alt会弹出sublime的顶部菜单栏,如果想屏蔽sublime按alt屏蔽菜单 ...