lightoj1355——树上green博弈

题目链接:lightoj.com/volume_showproblem.php?problem=1355

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

#include<iostream>
#include<cstring>
using namespace std; 
const int maxn=1e4+7;
int tot;
int sg[maxn];
int head[maxn],nxt[maxn],to[maxn],ww[maxn];
void init(){
	memset(head,0,sizeof(head));
	tot=0;
}
void addedge(int u,int v,int w){
	nxt[++tot]=head[u];
	head[u]=tot;
	to[tot]=v;
	ww[tot]=w;
}
void dfs(int cur,int fa){
	sg[cur]=0;
	for(int i=head[cur];i;i=nxt[i]){
		if(to[i]!=fa){
			dfs(to[i],cur);
			if(ww[i]==1) sg[cur]^=(sg[to[i]]+1);
			else sg[cur]^=(sg[to[i]]^(ww[i]%2));	
		}
	}
}
int main()
{
    int T,n;
    scanf("%d",&T);
    for(int kcase=1;kcase<=T;kcase++){
    	scanf("%d",&n);
    	init();
    	for(int i=1;i<=n-1;++i){
    		int u,v,w;
    		scanf("%d%d%d",&u,&v,&w);
    		addedge(u,v,w);
    		addedge(v,u,w);
		}
		dfs(0,0);
		if(sg[0]) printf("Case %d: Emily\n",kcase);
		else printf("Case %d: Jolly\n",kcase);
	}
    return 0;
}

题目翻译:

给定有根带权树,玩家可以给长度为1的树枝染色,不能染为输,可以给一个边染色,需要满足它到根的所有边被染色的长度<边权。

green博弈的变形题,比较难。

当权值为1时,正常的green博弈套路,

如果边权为偶数,没有贡献,如果为奇数且不为1,等于子节点与节点值对2取余后的抑或的值的抑或和。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值