lightoj1355(green博弈)

这个显然要运用green博弈,那么问题就落在怎么处理边权。。

想了好久都没想出。。然后直接打表。。

发现结论如下:

边权为1直接按照green博弈的步骤来

边权偶数其sg值不变

边权为大于1的奇数其sg值需要和1进行异或

然后这题能过了。。


证明待补充。。。

然后又经过了V8的提示,想出了一个证明方法(orzV8

考虑一条链上的情况,且只有与根相连的边的权值大于1,设为A边,其他部分称为B子树

那么先考虑A边边权为偶数的情况。。

如果B子树sg值为0,那么如果取A边,那么对手也取A边,如果取B子树,那么此时其 sg值必定不为0,那么对手又能将其sg值变成0,因此这样下去对手必胜,这个状态为必败态,sg值为0

如果B子树sg值不为0,那么将其sg值变成0丢给对手,对手必败,这个为必胜态。当然,这个状态也可以转移到其他的数,除了sg值等于他本身的状态,那么这个状态的sg值不变。。

这样可以证明边权为偶数的边对sg值没有影响


再来考虑A边边权奇数的情况。。

如果B字数的sg值为0,那么直接取走A边,对手必败

如果



/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1005
#define nm 5005
#define pi 3.1415926535897931
const ll inf=1000000007;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



struct edge{int t,v;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y,int v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
int n,_x,_y,_v,d[NM];
bool v[NM];

void dfs(int x){
    v[x]++;
    link(x)if(!v[j->t]){
	dfs(j->t);
	if(j->v==1)d[j->t]++;
	else d[j->t]=d[j->t]^(j->v%2);
	d[x]^=d[j->t];
    }
}

int main(){
    int _=read();inc(p,1,_){
        n=read();mem(v);mem(d);mem(e);mem(h);o=e;
        inc(i,1,n-1){_x=read();_y=read();_v=read();add(_x,_y,_v);add(_y,_x,_v);}
        v[0]++;dfs(0);
	printf("Case %d: ",p);puts(d[0]?"Emily":"Jolly");
    }
    return 0;
}


打表代码:

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1005
#define nm 5000005
#define pi 3.1415926535897931
const ll inf=1000000007;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}





int n,d[NM][NM];
bool v[NM];
int main(){
    n=20;
    inc(i,1,n){
	if(d[i-1][0])d[i][0]=0;else d[i][0]=1;
	inc(j,1,n){
	    mem(v);
	    v[d[i-1][j]]++;
	    inc(k,0,j-1)v[d[i][k]]++;
	    for(int k=0;;k++)if(!v[k]){d[i][j]=k;break;}
	}
    }
    inc(i,1,n){inc(j,0,n)printf("%d ",d[i][j]);putchar('\n');}
    return 0;
}




1355 - Game of CS
Time Limit: 2 second(s) Memory Limit: 32 MB

Jolly and Emily are two bees studying in Computer Science. Unlikeother bees they are fond of playing two-player games. They used to playTic-tac-toe, Chess etc. But now since they are in CS they invented a new gamethat definitely requires some knowledge of computer science.

Initially they draw a random rooted tree (a connected graphwith no cycles) in a paper which consists of nnodes, where the nodes are numbered from 0 to n-1 and 0 isthe root, and the edges are weighted. Initially all the edges are unmarked. Andan edge weigh w, has w identical units.

1.      Jollyhas a green marker and Emily has a red marker. Emily starts the game first andthey alternate turns.

2.      Ineach turn, a player can color one unit of an edge of the tree if thatedge has some (at least one) uncolored units and the edge can be traversed fromthe root using only free edges. An edge is said to be free if the edge is notfully colored (may be uncolored or partially colored).

3.      Ifit's Emily's turn, she finds such an edge and colors one unit of it using thered marker.

4.      Ifit's Jolly's turn, he finds such an edge and colors one unit of it with thegreen 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. Thetree 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 colorany edge she wants; she colors one unit of edge (0 1) with her redmarker (Fig 2). Since the weight of edge (0 1) is 1 so, this edge isfully colored.

Fig 1

Fig 2

Fig 3

Fig 4

Now it's Jolly's turn. He can only color one unit of edge (03). He can't color edge (1 2) since if he wants to traverse it from theroot (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). Andnow 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 wouldhave colored edge (1 2) instead of edge (0 1), then Jolly wouldwin. 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 twointegers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 109)denoting that there is an edge between u and v and their weightis w. You can assume that the given tree is valid.

Output

For each case, print the case number and the name of thewinner. See the samples for details.

Sample Input

Output for 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

Case 1: Emily

Case 2: Emily

Case 3: Jolly

Case 4: Emily

Note

Dataset is huge, use faster I/O methods.


猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/80551578