【题目】
Description Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees on a paper: Then they took turns to cut a branch of a tree, and removed the part of the tree which had already not connected with the root. A step shows as follows: Sally always moved first. Who removed the last part of the trees would win the game. After a while, they all figured out the best strategy and thought the game was too simple for them. Harry said, “The Christmas trees should have some gifts in them!” So Sally drew some gifts (simple polygons) in the initial trees: You may assume the initial picture is a tree with some simple polygons, in which each edge is involved in at most one polygon. We also know that every polygon has only one node involved in the main tree (the hanging point of the giftJ) .In every sub-tree (connected subgraph), there was one and only one node representing the “root”. According to these assumptions, following graphs will never appear: Sally and Harry took turns (Sally was always the first person to move), to cut an edge in the graph, and removed the part of the tree that no longer connected to the root. The person who cannot make a move lost the game. Your job is to decide who will finally win the game if both of them use the best strategy. Input The input file contains multiply test cases. Output For each test case, output the name of the winner. Sample Input 2 2 1 1 2 4 4 1 2 2 3 2 4 3 4 Sample Output Sally |
【题意】
【分析】
这道题在贾志豪的论文《组合游戏略述——浅谈SG游戏的若干拓展及变形》中有。
于是做这题的时候我花了很长时间苦思冥想、然后花了很长时间看题解看不懂,然后花了很长时间理解整个过程。表示脑子不够用啊。其实一开始那一步,就是变成很多棵根节点只有一个孩子的树的子游戏,把他们异或起来,这一步我是想到的。但是对于子游戏的sg值的求法和环的处理我不是很明白。先说环,偶环是无用的,因为别人做什么我就做什么。对于奇环,我们给他留一个点就好了,同样是别人做什么我就做什么。其实这个思路很常规了,在扫楼梯等题目中已经多次用到,但是我还是没想到这个层面(或许是因为一开始看错题了,不知道环只会在尾部吧)。最难的就是对于一棵树(含边u->v以及以v为根的子树组成的树),它的sg等于v这棵树的sg+1。这个的证明用到了数学归纳法(貌似,就是先说明在数据较小的情况下是成立的,以小推大再推到全部),具体看论文吧,就这个点比较难,需要好好理解。
算出每棵树的sg后,再按NIM游戏异或即可。
代码如下:(巨巨巨巨丑)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<stack> 8 using namespace std; 9 #define Maxn 110 10 #define Maxm 110 11 #define Maxk 550 12 13 struct node 14 { 15 int x,y,next,p; 16 }t[Maxk*2];int len; 17 int first[Maxn*2],fa[Maxn*2],dep[Maxn*2]; 18 int n,m; 19 20 bool vis[Maxn],mark[Maxn]; 21 22 void ins(int x,int y) 23 { 24 t[++len].x=x;t[len].y=y;t[len].p=1; 25 t[len].next=first[x];first[x]=len; 26 } 27 28 stack<int > s; 29 30 int ffind(int x,int f) 31 { 32 vis[x]=1; 33 dep[x]=dep[f]+1; 34 //s.push(x); 35 int now=0; 36 for(int i=first[x];i;i=t[i].next) if(t[i].p==1) 37 { 38 int y=t[i].y; 39 t[i].p=0;t[(i%2==0)?i-1:i+1].p=0; 40 if(vis[y]) 41 { 42 t[i].p=-1,t[(i%2==0)?i-1:i+1].p=-1; 43 if((dep[x]-dep[y]+1)%2==1) ins(y,++m); 44 return y; 45 } 46 else if(now=ffind(y,x)) t[i].p=-1,t[(i%2==0)?i-1:i+1].p=-1; 47 } 48 if(x==now) return 0; 49 return now; 50 } 51 52 int get_sg(int x,int f) 53 { 54 int ans=0; 55 for(int i=first[x];i;i=t[i].next) if(t[i].y!=f&&t[i].p!=-1) 56 { 57 ans^=(get_sg(t[i].y,x)+1); 58 } 59 return ans; 60 } 61 62 int main() 63 { 64 while(scanf("%d",&n)!=EOF) 65 { 66 int ans=0; 67 while(n--) 68 { 69 int k; 70 scanf("%d%d",&m,&k); 71 memset(first,0,sizeof(first)); 72 len=0; 73 for(int i=1;i<=k;i++) 74 { 75 int x,y; 76 scanf("%d%d",&x,&y); 77 ins(x,y);ins(y,x); 78 } 79 memset(vis,0,sizeof(vis)); 80 memset(mark,1,sizeof(mark)); 81 dep[0]=0; 82 ffind(1,0); 83 ans^=get_sg(1,0); 84 } 85 if(ans==0) printf("Harry\n"); 86 else printf("Sally\n"); 87 } 88 return 0; 89 }
2016-04-21 12:58:40