树的删边游戏。。

由于题目的特殊性,我们只需计算环的边数值。若为偶环,则直接把环的根节点置0。若为奇环,则留下一条边与根结点相连,并那它们的SG置0;

注意的是,两个点也可构成环,因为允许重边。所以,我们只需求点双连通分量,并判断分量中边的数量即可。然后DFS求树的SG值。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int N=;
const int M=;
int n,m;
struct {
int v,next;
}edge[M];
struct {
int u,v;
}edge_stack[M],tmp;
int dfn[N],low[N],index;
int edge_top,tot;
bool vis[N],vis_e[M];
int head[N],sg[N]; void addedge(int u,int v){
vis_e[tot]=false;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
} void tarjan(int u){
int i,j,k,v,e;
dfn[u]=low[u]=++index;
for(e=head[u];e!=-;e=edge[e].next){
v=edge[e].v;
if(dfn[v]==-){
vis_e[e]=vis_e[e^]=true;
edge_stack[++edge_top].u=u;
edge_stack[edge_top].v=v;
tarjan(v);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v]){
int cnt=;
do{
tmp.u=edge_stack[edge_top].u;
tmp.v=edge_stack[edge_top].v;
edge_top--;
cnt++;
vis[tmp.u]=vis[tmp.v]=true;
// printf("edge=%d %d ",tmp.u,tmp.v);
}while(!(tmp.u==u&&tmp.v==v));
// printf("\n");
if((cnt&)){
vis[tmp.u]=vis[tmp.v]=false;
}
else vis[tmp.u]=false;
}
}
else{
if(!vis_e[e]){
low[u]=min(low[u],dfn[v]);
if(dfn[u]>dfn[v]){
edge_stack[++edge_top].u=u;
edge_stack[edge_top].v=v;
}
}
}
}
} int dfs(int u){
int e,v;
vis[u]=true;
int ans=sg[u];
for(e=head[u];e!=-;e=edge[e].next){
int v=edge[e].v;
if(!vis[v]){
ans^=(dfs(v)+);
}
}
return ans;
} int main(){
int k,u,v;
while(scanf("%d",&k)!=EOF){
int ans=;
while(k--){
edge_top=-;
scanf("%d%d",&n,&m);
tot=index=;
for(int i=;i<=n;i++){
vis[i]=false; sg[i]=;
head[i]=dfn[i]=low[i]=-;
}
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
tarjan();
ans^=dfs();
}
if(ans) printf("Sally\n");
else printf("Harry\n");
}
return ;
}

POJ 3710的更多相关文章

  1. POJ 3710 无向图简单环树上删边

    结论题,这题关键在于如何转换环,可以用tarjan求出连通分量后再进行标记,也可以DFS直接找到环后把点的SG值变掉就行了 /** @Date : 2017-10-23 19:47:47 * @Fil ...

  2. POJ 3710 Christmas Game#经典图SG博弈

    http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏 ...

  3. poj 3710 Christmas Game(树上的删边游戏)

    Christmas Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1967   Accepted: 613 Des ...

  4. poj 3710 Christmas Game 博弈论

    思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...

  5. POJ 3710 Christmas Game

    知识储备: 解决办法(奇偶去环):   (1) 对于长度为奇数的环,去掉其中任意一个边之后,剩下的 两个链长度同奇偶,抑或之后的 SG 值不可能为奇数,所 以它的 SG 值为 1: (2) 对于长度为 ...

  6. POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)

    题目链接 \(Description\) 给定n棵"树",每棵"树"的节点可能"挂着"一个环,保证没有环相交,且与树只有一个公共点. 两人轮 ...

  7. POJ 3710:Matrix Power Series

    Description 给出矩阵 \(n*n\) 的 矩阵\(A\) , 求 \(A^1+A^2+A^3...+A^k\) Solution 首先我们设 \(S_n=\sum_{i=1}^{n}A^i ...

  8. POJ 3710 Christmas Game [博弈]

    题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...

  9. poj 3710 Christmas Game【博弈论+SG】

    也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...

随机推荐

  1. 【POJ 1475】 Pushing Boxes

    [题目链接] http://poj.org/problem?id=1475 [算法] 双重BFS [代码] #include <algorithm> #include <bitset ...

  2. zoj3478

    最短路 吐槽一下...最先开始写了个地图哈希,6kb,然后不是正解,又写了个spfa,4kb,还是不对,无奈抄标程,结果把spfa改成dijiestra就对了... 由于只有两个变量,所以我们设一个四 ...

  3. Java获取NTP网络时间

    最近项目中涉及到一个时间验证的问题,需要根据当前时间来验证业务数据是否过期.所以直接写代码如下: new java.util.Date().getTime();          结果测试的时候出现了 ...

  4. thinkphp3.2 验证码生成和点击刷新验证码

    生成验证码的时候: public function verify_c(){ $Verify = new \Think\Verify(); $Verify->fontSize = 18; $Ver ...

  5. Unity5.3.6升级到Unity5.4.4 NGUI出现Ignoring menu item NGUI because it is in no submenu!问题解决方案

    目录Assets/Plugins/NGUI/Scripts/Editor/NGUIMenu.cs文件中找到下图(左)所示,改成(右)图所示

  6. 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

  7. ASP.NET的Eval方法和Bind方法的区别

    Eval是只读的方法(单向数据在邦定),所邦定的内容为不会提交回服务器. 比如图书的ISBN,并不想让用户做任何修改,可以使用<%# Eval('ISBN').TOString().Trim() ...

  8. Eclipse 每次ctrl-c ctrl-v 就变慢?

    继续闲着,所以继续写 大小: 60.7 KB 查看图片附件

  9. 有关css的选择器优先级以及父子选择器

    css,又称样式重叠表,如今的网页的样式基本是div+css写出来的,功能十分强大,要想在html文件中引入css文件需要在<head></head>标签内输入一行:<l ...

  10. js仿新浪游戏频道导航条

    js仿新浪游戏频道导航条 在线演示本地下载