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 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ...
 
随机推荐
- 很火的Java题——判断一个整数是否是奇数
			
完成以下代码,判断一个整数是否是奇数: public boolean isOdd(int i) 看过<编程珠玑>的人都知道这道题的答案和其中极为简单的道理. 最普遍的风格,如下: 这个函数 ...
 - 《剑指offer》第八题(重要!查找二叉树的中序遍历的下一个结点)
			
文件一:main.cpp // 面试题:二叉树的下一个结点 // 题目:给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? // 树中的结点除了有两个分别指向左右子结点的指针以外,还有 ...
 - Codeforces 832D - Misha, Grisha and Underground
			
832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...
 - Java 集合-List接口和三个子类实现
			
List List:有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素.与 ...
 - 网络编程基础——System.Net.Socket 命名空间及相关类的使用
			
System.Net.Socket 命名空间主要提供制作 Socket 网络应用程序的相关类.(Socket 类.TcpClient 类.TcpListener 类 和 UdpClient 类) 1. ...
 - Linux文件与目录管理(三)
			
一.Linux文件内容查看 1.cat:由第一行开始显示文件内容 2.tac:从最后一行开始显示,可以看出tac是cat倒着写 3.nl:显示的时候,顺便输出行号 4.more:一页一页的显示文件内容 ...
 - Animation鱼眼效果
			
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
 - iOS UI-QQ聊天布局
			
一.Model BWMessage.h #import <Foundation/Foundation.h> typedef enum{ BWMessageMe = ,//表示自己 BWMe ...
 - Oracle 12cR1中性能优化新特性之全数据库缓冲模式
			
通常情况下,Oracle会决定哪些数据会留在缓冲区中.当没足够的空间时,数据会被写出内存.此外,为了避免大量读取将有用的信息挤出缓冲区,Oracle对有些操作也许会才去绕过缓冲区的措施.Oracle1 ...
 - React路由配置使用
			
Router包安装: 安装包还是要打开命令行工具,使用npm来进行安装. npm install --save react-router react-router-dom 页面login: impor ...