LightOJ 1355 :Game of CS(树上green博弈)
Jolly and Emily are two bees studying in Computer Science. Unlike other bees they are fond of playing two-player games. They used to play Tic-tac-toe, Chess etc. But now since they are in CS they invented a new game that definitely requires some knowledge of computer science.
Initially they draw a random rooted tree (a connected graph with no cycles) in a paper which consists of n nodes, where the nodes are numbered from 0 to n-1 and 0 is the root, and the edges are weighted. Initially all the edges are unmarked. And an edge weigh w, has w identical units.
- Jolly has a green marker and Emily has a red marker. Emily starts the game first and they alternate turns.
- In each turn, a player can color one unit of an edge of the tree if that edge has some (at least one) uncolored units and the edge can be traversed from the root using only free edges. An edge is said to be free if the edge is not fully colored (may be uncolored or partially colored).
- If it's Emily's turn, she finds such an edge and colors one unit of it using the red marker.
- If it's Jolly's turn, he finds such an edge and colors one unit of it with the green marker.
- The player, who can't find any edges to color, loses the game.
For example, Fig 1 shows the initial tree they have drawn. The tree contains four nodes and the weights of the edge (0, 1), (1, 2) and (0, 3) are 1, 1 and 2 respectively. Emily starts the game. She can color any edge she wants; she colors one unit of edge (0 1) with her red marker (Fig 2). Since the weight of edge (0 1) is 1 so, this edge is fully colored.
| Fig 1 | Fig 2 | Fig 3 | Fig 4 | 
Now it's Jolly's turn. He can only color one unit of edge (0 3). He can't color edge (1 2) since if he wants to traverse it from the root (0), he needs to use (0, 1) which is fully colored already. So, he colors one unit of edge (0 3) with his green marker (Fig 3). And now Emily has only one option and she colors the other unit of (0 3) with the red marker (Fig 4). So, both units of edge (0 3) are colored. Now it's Jolly's turn but he has no move left. Thus Emily wins. But if Emily would have colored edge (1 2) instead of edge (0 1), then Jolly would win. So, for this tree Emily will surely win if both of them play optimally.
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case starts with a line containing an integer n (2 ≤ n ≤ 1000). Each of the next n-1 lines contains two integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 109) denoting that there is an edge between u and v and their weight is w. You can assume that the given tree is valid.
Output
For each case, print the case number and the name of the winner. See the samples for details.
Sample Input
4
4
0 1 1
1 2 1
0 3 2
5
0 1 1
1 2 2
0 3 3
0 4 7
3
0 1 1
0 2 1
4
0 1 1
1 2 1
1 3 1
Sample Output
Case 1: Emily
Case 2: Emily
Case 3: Jolly
Case 4: Emily
题意:给定有根带权树,玩家可以给长度为1的树枝染色,不能染为输,可以给一个边染色,需要满足它到根的所有边被染色的长度<边权。
思路:如果没有边权(即长度都为1),那么就是一个裸的green博弈,即每个点的sg函数=子节点的sg函数+1的异或和。
这里有边权,我们可以先得到几种比较特别的情况。
1:边权为1,那么就是正常的考虑。
2:边权为偶数,其贡献为0,因为无论先手如何染色,后手有地方可以染色。
那就只剩下一种情况,我也不知道怎么回事。
3:为奇数而且不为1...std是^1。占位。
#include<bits/stdc++.h>
#define rep(i,o,l) for(int i=o;i<=l;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn],cnt,sg[maxn];
void add(int u,int v,int c)
{
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=c;
}
void dfs(int u,int f)
{
sg[u]=;
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=f){
dfs(To[i],u);
if(Len[i]==) sg[u]^=(sg[To[i]]+);
else sg[u]^=(sg[To[i]]^(Len[i]%));
}
}
int main()
{
int T,N,u,v,d,C=;
scanf("%d",&T);
while(T--){
scanf("%d",&N); cnt=;
rep(i,,N) Laxt[i]=;
rep(i,,N-) {
scanf("%d%d%d",&u,&v,&d);
add(u,v,d); add(v,u,d);
}
dfs(,);
printf("Case %d: ",++C);
puts(sg[]?"Emily":"Jolly");
}
return ;
}
LightOJ 1355 :Game of CS(树上green博弈)的更多相关文章
- LightOJ1355 Game Of CS(green 博弈)
		Jolly and Emily are two bees studying in Computer Science. Unlike other bees they are fond of playin ... 
- codevs 1421 秋静叶&秋穣子(树上DP+博弈)
		1421 秋静叶&秋穣子 题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一 起,自然会想到烤红薯 ... 
- Atcoder #017 agc017 D.Game on Tree 树上NIM 博弈
		LINK 题意:树上NIM的模板题,给出一颗树,现有操作删去端点不为根节点的边,其另一端节点都将被移除,不能取者为败 思路:一看就是个NIM博弈题,只是搬到树上进行,树上DFS进行异或 记得#014D ... 
- #417 Div2 E (树上阶梯博弈)
		#417 Div2 E 题意 给出一颗苹果树,设定所有叶子节点的深度全是奇数或偶数,并且包括根在内的所有节点上都有若干个苹果. 两人进行游戏,每回合每个人可以做下列两种操作中的一种: 每个人可以吃掉某 ... 
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
		G - Game of Hyper Knights Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & ... 
- LightOJ 1224 - DNA Prefix - [字典树上DFS]
		题目链接:https://cn.vjudge.net/problem/LightOJ-1224 Given a set of $n$ DNA samples, where each sample is ... 
- 【51nod】1531 树上的博弈
		题解 我们发现每次决策的时候,我们可以判断某个点的决策,至少小于等于几个点或者至少大于等于几个点 我们求最大值 dp[u][1 / 0] dp[u][1]表示u这个点先手,至少大于等于几个点 dp[u ... 
- hihocoder1545 : 小Hi和小Ho的对弈游戏(树上博弈&nim博弈)
		描述 小Hi和小Ho经常一起结对编程,他们通过各种对弈游戏决定谁担任Driver谁担任Observer. 今天他们的对弈是在一棵有根树 T 上进行的.小Hi和小Ho轮流进行删除操作,其中小Hi先手. ... 
- HDU 5996:dingyeye loves stone(阶梯博弈)
		http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ... 
随机推荐
- Observable类API
			包java.util 类 Observable public class Observable extends Object 此类表示模型视图范例中的 observable 对象,或者说“数据”.可将 ... 
- Codeforces 861D - Polycarp's phone book
			861D - Polycarp's phone book 思路:用map做的话,只能出现一次循环,否则会超时. 代码: #include<bits/stdc++.h> using name ... 
- 你真的了解Spring Framework吗?
			Java 框架 上世纪90年代,使用Java开发Web应用普遍使用J2EE标准,J2EE具有平台无关性,对事务.消息等企业级的特性都有很好的支持,但当时的J2EE仍存在一些问题: 非常复杂:EJB的诞 ... 
- java web mysql.jar java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
			java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 折腾了一上午,找到了这错误的原因.哎……悲剧! 确认包已经被导入web工程目录. 原来是 ... 
- Confluence 6 从外部目录中同步数据如何工作
			下面是有关缓存功能的一些摘要信息: 用户和用户组的缓存信息保存在应用程序的数据库中. 当你连接一个新的外部目录到系统中的时候,一个同步任务将会启动被,并且在后台运行拷贝所有需要的用户和用户组信息,以及 ... 
- LICEcap 和 FS Capture入门教程
			上一篇介绍了如何使用 Visio 图形图表工具,文中贴了一张gif图,留言的小伙伴们迫不及待想知道如何录制 GIF 图,强哥姑且卖弄一次,把 PC 端截图工具和教程分享给大家,分别为 LICEcap ... 
- h1026 BFS(打印x与路径)
			题意: Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ... 
- git在eclipse中的配置 转载
			git在eclipse中的配置 转载 一_安装EGIT插件 http://download.eclipse.org/egit/updates/ 或者使用Eclipse Marketplace,搜索EG ... 
- Linux文件与目录管理(三)
			一.Linux文件内容查看 1.cat:由第一行开始显示文件内容 2.tac:从最后一行开始显示,可以看出tac是cat倒着写 3.nl:显示的时候,顺便输出行号 4.more:一页一页的显示文件内容 ... 
- Oracle12c中性能优化增强新特性之数据库智能闪存
			智能闪存功能最初在XD中引入.从Oracle11.2.0.2开始,除了用于XD存储,还可用于任何闪盘.Oracle12c中,不需卷管理器就可以使用闪盘. 1. 简介 智能闪存在solaris和lin ... 
