atcoder Fennec VS. Snuke 搜索

本文介绍了一个简单的博弈问题FennecVS.Snuke,并通过深度优先搜索(DFS)策略解决了这一问题。玩家轮流将未着色的节点染成与已着色相邻节点相同颜色,直到无法继续为止。通过比较最终染色结果来判断胜者。

6581: Fennec VS. Snuke

时间限制: 1 Sec  内存限制: 128 MB
提交: 113  解决: 43
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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 is given from Standard Input in the following format:
N
a1 b1
:
aN−1 bN−1

 

输出

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

 

样例输入

7
3 6
1 2
3 1
7 4
5 7
1 4

 

样例输出

Fennec

 

提示

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

比赛第一眼看感觉是博弈,因为对博弈一无所知,所以当时瞬间放弃这题

赛后看题解才发现,只要动动脑子就可以跳出博弈

因为只可以选择已经染过色的相邻节点,所以可以用dfs遍历所有点,确定该点谁可以选择,最后比较即可

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=1e5+100;
const int INF=1e9;
const int MOD=1e9+7;
vector<int> mapp[maxx];
int vis[maxx];
int n;
int ans[5];
void dfs(int x,int y)
{
    vis[x]=1;
    vis[y]=2;
    queue<int> q;
    q.push(x);
    q.push(y);
    while(!q.empty()){
        int p=q.front();
        ans[vis[p]]++;
        q.pop();
        for(int i=0; i<mapp[p].size(); i++){
            if(vis[mapp[p][i]])   continue;
            vis[mapp[p][i]]=vis[p];
            q.push(mapp[p][i]);
        }
    }
}
int main()
{
    cin>>n;
    int a,b;
    for(int i=1; i<n; i++){
        cin>>a>>b;
        mapp[a].push_back(b);
        mapp[b].push_back(a);
    }
    dfs(1,n);
    if(ans[1]>ans[2])  cout<<"Fennec"<<endl;
    else               cout<<"Snuke"<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值