@zzzc18
2017-07-16T21:55:32.000000Z
字数 1890
阅读 1271
AtCoder
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
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.
大意:
一棵树 个点,开始的时候 号点是黑色 ,号点是白色,黑色先手,黑白轮流进行,每次可以选一个树上未染色并与其色块相邻的点染成其对应颜色。如果哪一方最后无法再染色,则输掉
显然,黑白的争端在于与黑白相邻的链上(不在链上的部分可以以后慢慢搞),举个例子
这是开始的情况
抢链上的部分
最终情况
所以让他们在链上打一架就好了,DFS一边就好了,处理size,链
#include<cstdio>
#include<algorithm>
#define MAXN 300000
using namespace std;
int n,loc;
struct E{
int next,to;
}edge[MAXN<<1];
int head[MAXN],edge_num;
int vis[MAXN],path[MAXN];
int size[MAXN];
void addedge(int x,int y){
edge[++edge_num].next=head[x];
edge[edge_num].to=y;
head[x]=edge_num;
}
void INIT(){
scanf("%d",&n);
int i;
for(i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
}
void DFS(int x,int depth){
path[depth]=x;vis[x]=1;
size[x]=1;
if(x==n){
loc=path[depth/2+1];
}
int i;
for(i=head[x];i;i=edge[i].next){
if(vis[edge[i].to])
continue;
DFS(edge[i].to,depth+1);
size[x]+=size[edge[i].to];
}
}
void solve(){
DFS(1,0);
int sz_f=size[1]-size[loc],sz_s=size[loc];
if(sz_f>sz_s)
printf("Fennec\n");
else
printf("Snuke\n");
}
void DEBUG(){
int i;
printf("Size:");
for(i=1;i<=n;i++)
printf("%d ",size[i]);
}
int main(){
INIT();
solve();
//DEBUG();
return 0;
}