@zzzc18 2017-07-16T13:55:32.000000Z 字数 1890 阅读 1185

# Regular Contest 078

AtCoder

### 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

$1\leq N\leq 2\times10^5$

## Solution

#include<cstdio>#include<algorithm>#define MAXN 300000using 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;}

• 私有
• 公开
• 删除