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.

  1. Jolly has a green marker and Emily has a red marker. Emily starts the game first and they alternate turns.
  2. 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).
  3. If it's Emily's turn, she finds such an edge and colors one unit of it using the red marker.
  4. If it's Jolly's turn, he finds such an edge and colors one unit of it with the green marker.
  5. 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

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博弈)的更多相关文章

  1. LightOJ1355 Game Of CS(green 博弈)

    Jolly and Emily are two bees studying in Computer Science. Unlike other bees they are fond of playin ...

  2. codevs 1421 秋静叶&秋穣子(树上DP+博弈)

    1421 秋静叶&秋穣子   题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一 起,自然会想到烤红薯 ...

  3. Atcoder #017 agc017 D.Game on Tree 树上NIM 博弈

    LINK 题意:树上NIM的模板题,给出一颗树,现有操作删去端点不为根节点的边,其另一端节点都将被移除,不能取者为败 思路:一看就是个NIM博弈题,只是搬到树上进行,树上DFS进行异或 记得#014D ...

  4. #417 Div2 E (树上阶梯博弈)

    #417 Div2 E 题意 给出一颗苹果树,设定所有叶子节点的深度全是奇数或偶数,并且包括根在内的所有节点上都有若干个苹果. 两人进行游戏,每回合每个人可以做下列两种操作中的一种: 每个人可以吃掉某 ...

  5. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  6. LightOJ 1224 - DNA Prefix - [字典树上DFS]

    题目链接:https://cn.vjudge.net/problem/LightOJ-1224 Given a set of $n$ DNA samples, where each sample is ...

  7. 【51nod】1531 树上的博弈

    题解 我们发现每次决策的时候,我们可以判断某个点的决策,至少小于等于几个点或者至少大于等于几个点 我们求最大值 dp[u][1 / 0] dp[u][1]表示u这个点先手,至少大于等于几个点 dp[u ...

  8. hihocoder1545 : 小Hi和小Ho的对弈游戏(树上博弈&nim博弈)

    描述 小Hi和小Ho经常一起结对编程,他们通过各种对弈游戏决定谁担任Driver谁担任Observer. 今天他们的对弈是在一棵有根树 T 上进行的.小Hi和小Ho轮流进行删除操作,其中小Hi先手. ...

  9. HDU 5996:dingyeye loves stone(阶梯博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ...

随机推荐

  1. angular2版本迭代之特性追踪

    一. 2.0.0 升级到 2.4 升级前: 1.确保没有使用extends关键字实现OnInit的继承,以及没有用任何的生命周期中,而是全部改用implements. 2.停止使用deep impor ...

  2. tomcat和java环境

    mac tomcat http://blog.csdn.net/huyisu/article/details/38372663 mac jdk 1.8 http://wlb.wlb.blog.163. ...

  3. C# DataTable列名不区分大小写

    一直很纠结的就是DataTable的列名如何才能规范,从Oracle取出的DataTable都是大写,最后尝试了一下,原来C#的DataTable列名并不区分大小写,具体例子如下: DataTable ...

  4. Java基础八--构造函数

    Java基础八--构造函数 一.子父类中构造函数的特点 1.1 为什么在子类构造对象时,发现,访问子类构造函数时,父类也运行了呢? 原因是:在子类的构造函数中第一行有一个默认的隐式语句. super( ...

  5. 只能输入float

    if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (in ...

  6. Sidekiq(部分基础,有几个使用案例和active_job的用法)

    Sidekiq (8700✨) git :  https://github.com/mperham/sidekiq https://www.cnblogs.com/richard1234/p/3829 ...

  7. python-day4笔记

    1.文件后缀名对python运行没关系 2.Python解释器执行python程序的过程:python3 C:\test.py 1)启动python解释器(内存中) 2)将C:\test.py内容从硬 ...

  8. RAC配置(启停库)

    关库顺序 :先关闭数据库 然后关闭节点资源 [root@rac1 ~]# srvctl stop   database  -d 数据库名[root@rac1 ~]# srvctl stop   ins ...

  9. 在linux下出现cannot restore segment prot after reloc: Permission denied

    应用程序连接oracle的库时会出现如下错误:XXXXX:: error while loading shared libraries: /usr/local/oracle/product/10.2. ...

  10. Markdown语法笔记

    1.文字和图片中怎么让图片换行? 答:在文字和图片之间加入多个空格或者直接按Tab健即可