D - Fennec VS. Snuke
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
Fennec and Snuke are playing a board game.
On the board, there are N cells numbered 1 through N, and N−1 roads, each connecting two cells. Cell ai is adjacent to Cell bi through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.
Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:
- Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
- Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.
A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.
Constraints
- 2≤N≤105
- 1≤ai,bi≤N
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N a1 b1 : aN−1 bN−1
Output
If Fennec wins, print Fennec
; if Snuke wins, print Snuke
.
Sample Input 1
7 3 6 1 2 3 1 7 4 5 7 1 4
Sample Output 1
Fennec
For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.
Sample Input 2
4 1 4 4 2 2 3
Sample Output 2
Snuke
Fennec选择的点与至少一个黑色点相邻,且该点未被染色,染色之后该点为黑色;
Snuke选择的点与至少一个白色点相邻,且该点未被染色,染色之后该点为白色。
Fennec先手,当其中一个不能选择节点染色时他就输了。假设两人都采取最优策略,问谁会赢。
为方便我们可以分三种情况:
1.n号点要到达该点,必经过1号点。这些点已经是1号点的势力范围之内了,n号点无法到达。
2.1号点要到达该点,必经过n号点。这些点已经是n号点的势力范围之内了,1号点无法到达。
3.剩下的点,未确定归属,是兵家必争之地。
对于1到n路径上的点,双方的最优策略便是尽可能多的占领这些点,占领一个就占领了该点及其除这条路径外上的所有儿子节点。
最后把三种情况的点分黑白叠加,看看哪个比较多就好了。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=200005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
int depth[maxn],visit[maxn],head[maxn],son[maxn];
bool connect[maxn];
int num;
int b,w,n;
struct Edge{
int to,pre;
};
Edge edge[maxn*2];
void addedge(int from,int to) {
edge[num].to=to;
edge[num].pre=head[from];
head[from]=num++;
edge[num].to=from;
edge[num].pre=head[to];
head[to]=num++;
}
void dfs(int now,int step) {
visit[now]=1;
depth[now]=step;
son[now]=1;
if (now==n) connect[now]=1;
for (int i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
if (!visit[to]) {
dfs(to,step+1);
son[now]+=son[to];
connect[now]=connect[now]|connect[to];
}
}
}
void dfs2(int now) {
visit[now]=1;
bool IsBlack;
int p;
if (depth[now]<=depth[n]/2) IsBlack=1; else IsBlack=0;
for (int i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
if (!visit[to]&&connect[to]) {
dfs2(to);
p=to;
}
}
if (now!=n&&now!=1)
if (IsBlack) {
b+=son[now]-son[p];
} else w+=son[now]-son[p];
}
int main() {
int i,j,k,x,y;
mem0(visit);
mem0(son);
mem0(connect);
num=0;
scanf("%d",&n);
memset(head,-1,sizeof(head));
for (i=1;i<n;i++) {
scanf("%d%d",&x,&y);
addedge(x,y);
}
dfs(1,0);
int r;
for (int i=head[1];i!=-1;i=edge[i].pre) {
if (connect[edge[i].to]) {
r=edge[i].to;
break;
}
}
b=n-son[r];
w=son[n];
mem0(visit);
dfs2(1);
if (b>w) printf("Fennec"); else printf("Snuke");
return 0;
}