POJ 3710
树的删边游戏。。
由于题目的特殊性,我们只需计算环的边数值。若为偶环,则直接把环的根节点置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的更多相关文章
- POJ 3710 无向图简单环树上删边
结论题,这题关键在于如何转换环,可以用tarjan求出连通分量后再进行标记,也可以DFS直接找到环后把点的SG值变掉就行了 /** @Date : 2017-10-23 19:47:47 * @Fil ...
- POJ 3710 Christmas Game#经典图SG博弈
http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏 ...
- poj 3710 Christmas Game(树上的删边游戏)
Christmas Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1967 Accepted: 613 Des ...
- poj 3710 Christmas Game 博弈论
思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...
- POJ 3710 Christmas Game
知识储备: 解决办法(奇偶去环): (1) 对于长度为奇数的环,去掉其中任意一个边之后,剩下的 两个链长度同奇偶,抑或之后的 SG 值不可能为奇数,所 以它的 SG 值为 1: (2) 对于长度为 ...
- POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)
题目链接 \(Description\) 给定n棵"树",每棵"树"的节点可能"挂着"一个环,保证没有环相交,且与树只有一个公共点. 两人轮 ...
- 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 ...
- POJ 3710 Christmas Game [博弈]
题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...
- poj 3710 Christmas Game【博弈论+SG】
也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...
随机推荐
- operator[] 重载
#include <iostream>#include <vector>#include <string> class Assoc { struct Pair ...
- curl强制下载文件
<?phpfunction download_remote_file_with_curl($file_url, $save_to) { $ch = curl_init(); curl_setop ...
- Python 34(进程了解)
一:僵尸进程与孤儿进程 测试程序: 基本概念: 一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中. ...
- OOM三种类型
OOM的三种类型: 堆OOM /** * -Xmx1g -XX:+PrintGCDetails -XX:MaxDirectMemorySize=100m * * @param args */ publ ...
- 使用logback实现http请求日志导入mongodb
spring boot自带logback作为其日志新系统,但是在实际工作中,常常需要对日志进行管理或分析,如果只是单纯的将日志导入文本文件,则在查询时操作过于繁琐,如果将其导入mysql等关系型数据库 ...
- Centos6系列安装nginx
设置ssh服务为开机启动 输入命令:chkconfig sshd on 即可.注:若是chkconfig sshd off则禁止SSH开机启动 设定账号为bizuser ,密码为123456 第一步: ...
- PHP序列化 反序列化
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...
- php实时推送系统消息给客户端
在我们实际开发过程中,有些数据需要实时获取:比如erp系统中的订单信息,OA系统中的流程审批等都需要及时处理,这时我们就不能再使用http协议了:当然也可以使用轮询的机制.但是轮询请求中有大半是无用, ...
- SQLServer2008 关于数值字段列的累计
create table #temp20110610( id int identity(1,1), date varchar(8), qty float) insert int ...
- Leetcode0100--Same Tree 相同树
[转载请注明]http://www.cnblogs.com/igoslly/p/8707664.html 来看一下题目: Given two binary trees, write a functio ...