树的删边游戏。。

由于题目的特殊性,我们只需计算环的边数值。若为偶环,则直接把环的根节点置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. 解析HTML文件

    #!/usr/bin/env python3 # -*- coding: UTF-8 -*- from bs4 import BeautifulSoup import operator import ...

  2. Scala学习2 ———— 三种方式完成HelloWorld程序

    三种方式完成HelloWorld程序 分别采用在REPL,命令行(scala脚本)和Eclipse下运行hello world. 一.Scala REPL. 按照第一篇在windows下安装好scal ...

  3. ROS-TF-Time

    前言:如何在特定时间进行转换.让第二只乌龟去第一只乌龟在5秒前的地方. 参考自:http://wiki.ros.org/tf/Tutorials/Time%20travel%20with%20tf%2 ...

  4. C#接入第三方支付一些小问题

    13年第一次接入支付宝的时候,支付宝的api还不是很好用,费了些劲才完成,本月再次接入的时候发现已经很好用了,接入过程非常顺畅,只出现了一个小问题,我的金额默认是保留了4位小数,支付宝api只接受最多 ...

  5. 6月7号shiro

    Retains all Cache objects maintained by this cache manager :保留此缓存管理器维护的所有缓存对象 Destroyable可毁灭的 retain ...

  6. Spring 错误日志

    13:19:14.437 [main] DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListen ...

  7. oracle排序union和union all区别

    是这样的,表格中有几个属性,比如age吧是之一,age是字符类型的数字,select之间由union连接,此时是无法对前面的select语句进行order by的,也就是无法排序,无法达成我要的按ag ...

  8. 删除git上已经提交的文件

    1.先查看有哪些文件可以删除,但是不真执行删除 git rm -r -n job-executor-common/target/* -r  递归移除目录 -n 加上这个参数,执行命令时,是不会删除任何 ...

  9. Linux while和for循环简单分析

    一.循环重定向 最近遇到了一种新的循环重定向写法,由于没看懂,说以网上搜索了一下,然后再此分享一下: while read line do ...... done  < file 刚开始看,不明 ...

  10. SLAM: Structure From Motion-移动中三维场景重建

    wiki链接:https://en.wikipedia.org/wiki/Structure_from_motion 三维重建: 三维物体建模总结 1. 视野内三维物体重建 : Kinect fusi ...