poj 3710 Christmas Game (树形删边游戏)

本文介绍了一种基于图论的游戏——圣诞树游戏。玩家轮流切割圣诞树的树枝,并移除与根部不再连接的部分。文章提供了详细的算法实现,包括如何通过深度优先搜索和Tarjan算法确定游戏胜负。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Christmas Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2166 Accepted: 675

Description

Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees on a paper:

Then they took turns to cut a branch of a tree, and removed the part of the tree which had already not connected with the root. A step shows as follows:

Sally always moved first. Who removed the last part of the trees would win the game.

After a while, they all figured out the best strategy and thought the game was too simple for them. Harry said, “The Christmas trees should have some gifts in them!” So Sally drew some gifts (simple polygons) in the initial trees:

You may assume the initial picture is a tree with some simple polygons, in which each edge is involved in at most one polygon. We also know that every polygon has only one node involved in the main tree (the hanging point of the giftJ) .In every sub-tree (connected subgraph), there was one and only one node representing the “root”. According to these assumptions, following graphs will never appear:

Sally and Harry took turns (Sally was always the first person to move), to cut an edge in the graph, and removed the part of the tree that no longer connected to the root. The person who cannot make a move lost the game.

Your job is to decide who will finally win the game if both of them use the best strategy.

Input

The input file contains multiply test cases.
The first line of each test case is an integer N (N<100), which represents the number of sub-trees. The following lines show the structure of the trees. The first line of the description of a tree is the number of the nodes m (m<100) and the number of the edges k (k<500). The nodes of a tree are numbered from 1 to m. Each of following lines contains 2 integers a and b representing an edge <ab>. Node 1 is always the root.

Output

For each test case, output the name of the winner.

Sample Input

2
2 1
1 2
4 4
1 2
2 3
2 4
3 4

Sample Output

Sally

Hint

The sample graph is


Source

[Submit]   [Go Back]   [Status]   [Discuss]


题解:树形删边游戏


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define N 200003
using namespace std;
int n,m,point[N],next[N],tot,v[N],cnt,sz,top;
int belong[N],dfsn[N],low[N],size[N],ins[N],x1[N],y2[N],st[N],sc[N];
void add(int x,int y)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void init()
{
	tot=cnt=sz=top=0;
	memset(point,0,sizeof(point));
	memset(dfsn,0,sizeof(dfsn));
	memset(belong,0,sizeof(belong));
	memset(low,0,sizeof(low));
	memset(size,0,sizeof(size));
	memset(ins,0,sizeof(ins));
	memset(sc,0,sizeof(sc));
}
void tarjan(int x,int fa)
{
	ins[x]=1; st[++top]=x;
	dfsn[x]=low[x]=++sz;
	bool pd=false;
	for (int i=point[x];i;i=next[i]){
		if (v[i]==fa&&!pd) {
			pd=true;
			continue;
		}
		if (!dfsn[v[i]]) tarjan(v[i],x),low[x]=min(low[x],low[v[i]]);
		else if (ins[v[i]]) low[x]=min(low[x],dfsn[v[i]]);
	}
	if (dfsn[x]==low[x]){
		++cnt; int j;
		do{
			j=st[top--];
			belong[j]=cnt;
			size[cnt]++;
			ins[j]=0;
		}while (j!=x);
		if (size[cnt]>1&&size[cnt]%2) sc[cnt]^=1;
	}
}
int dfs(int x,int fa)
{
	int sg=sc[x];
	for (int i=point[x];i;i=next[i])
	 if (v[i]!=fa) sg^=(dfs(v[i],x)+1);
	return sg;
}
int main()
{
	freopen("a.in","r",stdin);
	int T;
	while (scanf("%d",&T)!=EOF){
		int ans=0; 
		for (int t=1;t<=T;t++){
			init();
			scanf("%d%d",&n,&m);
			for (int i=1;i<=m;i++){
				int x,y; scanf("%d%d",&x,&y);
				add(x,y); x1[i]=x; y2[i]=y;
			}
			for (int i=1;i<=n;i++)
			 if (!dfsn[i]) tarjan(i,0);
			tot=0;
			memset(point,0,sizeof(point));
			for (int i=1;i<=m;i++) 
			 if (belong[x1[i]]!=belong[y2[i]]) {
			 	int x=belong[x1[i]];
			 	int y=belong[y2[i]];
			 	add(x,y);
			 }
			ans^=dfs(belong[1],0);
		}
		if (ans) printf("Sally\n");
		else printf("Harry\n");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值