AtCoder Regular Contest 078-D

来源:互联网 发布:4518无法网络打印 编辑:程序博客网 时间:2024/06/07 04:51

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

题目大意:有一棵树,编号1~n,1为黑色,n为白色,其余尚未染色,Fennec先开始染黑色相邻的点,Snuke后染白色相邻的点,交替反复,谁无法染色谁就输,二者都采取最优策略,问最后的胜者。
解题思路:
(1)比赛时,手动模拟比了一下,发现要想赢,最好的方法就是尽快堵住对手,缩小对手可染色的点数,那么从1~n的那条路径就是双方最先需要染色的,所以找到中间交界处,判断两边的点数谁多,就能得出胜者。
(2)比赛后,看了官方题解,发现只要用两次dfs得出各点到1和n的距离,通过比较大小,就能得出结果,实在是太妙了!

(1)AC代码一(比赛时写的,有点丑)

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;typedef long long LL;const int MAXN=1e5+5;int tot,head[MAXN];bool mark[MAXN];int ans1,ans2;int n;int num;int slit;int father;struct Edge{    int from,to;    int next;}e[MAXN*2];void add(int u,int v){    e[tot].from=u;    e[tot].to=v;    e[tot].next=head[u];    head[u]=tot++;}bool dfs(int rt,int fa){    for(int i=head[rt];i!=-1;i=e[i].next)    {        int to=e[i].to;        if(to==fa) continue;        if(to==n) return true;        if(dfs(to,rt))        {            mark[to]=true;            return true;        }    }    return false;}int dfs2(int rt,int fa){    for(int i=head[rt];i!=-1;i=e[i].next)    {        int to=e[i].to;        if(to==fa) continue;        if(!mark[to]) continue;        else        {            num++;            if(num==slit)            {                father=rt;                return to;            }            else                return dfs2(to,rt);        }    }}void dfs3(int rt,int fa){    for(int i=head[rt];i!=-1;i=e[i].next)    {        int to=e[i].to;        if(to==fa) continue;        ans1++;        dfs3(to,rt);    }}void dfs4(int rt,int fa){    for(int i=head[rt];i!=-1;i=e[i].next)    {        int to=e[i].to;        if(to==fa) continue;        ans2++;        dfs4(to,rt);    }}int main(){    //ios::sync_with_stdio(false);    while(cin>>n)    {        memset(head,-1,sizeof(head));        memset(mark,false,sizeof(mark));        tot=0;        int u,v;        for(int i=1;i<=n-1;i++)        {            cin>>u>>v;            add(u,v);add(v,u);        }        dfs(1,0);        mark[1]=true;mark[n]=true;        int cnt=0;        for(int i=1;i<=n;i++)        {            if(mark[i])            {                cnt++;                //cout<<i<<" ";            }        }        //cout<<endl;        if(cnt&1)            slit=cnt/2+1;        else slit=cnt/2;        //cout<<"slit: "<<slit<<endl;        num=1;        int id=dfs2(1,0);        //cout<<id<<endl;        int child;        for(int i=head[id];i!=-1;i=e[i].next)        {            int to=e[i].to;            if(mark[to]&&to!=father)                child=to;        }        //cout<<father<<" "<<id<<" "<<child<<endl;        ans1=0;ans2=0;        dfs3(id,child);        dfs4(child,id);        //cout<<ans1<<" "<<ans2<<endl;        if(ans1>ans2)            cout<<"Fennec"<<endl;        else            cout<<"Snuke"<<endl;    }    return 0;}/*111 21 61 83 64 65 85 74 94 1010 11*/

(2)AC代码二(赛后参考官方题解和大神代码写的,简洁了许多)

#include<iostream>#include<cstdio>#include<algorithm>#include<vector>using namespace std;typedef long long LL;const int MAXN=1e5+5;int n;vector<int> g[MAXN*2];int d1[MAXN],d2[MAXN];void dfs(int rt,int fa,int d,int dist[]){    dist[rt]=d;    for(int x:g[rt])    {        if(x!=fa)            dfs(x,rt,d+1,dist);    }}int main(){    ios::sync_with_stdio(false);    while(cin>>n)    {        for(int i=0;i<n;i++)            g[i].clear();        int u,v;        for(int i=0;i<n-1;i++)        {            cin>>u>>v;            u--;v--;            g[u].push_back(v);            g[v].push_back(u);        }        dfs(0,-1,0,d1);        dfs(n-1,-1,0,d2);        int ans=0;        for(int i=0;i<n;i++)        {            if(d1[i]<=d2[i])  ++ans;            else --ans;        }        if(ans>0)            cout<<"Fennec"<<endl;        else            cout<<"Snuke"<<endl;    }    return 0;}
原创粉丝点击