Fennec VS. Snuke 搜索

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;
}
 

老规矩,自己敲一遍

# include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+3;
vector<int> Edg[maxn];
map<int,int> vis;
queue<int> Q;
int ans[3],n;
void dfs(int x,int y)
{
    vis[x]=1,vis[y]=2;

    Q.push(x),Q.push(y);

    while(!Q.empty())
    {
        int t = Q.front();
        Q.pop();
        ans[vis[t]]++;
        for(int i=0;i<Edg[t].size();i++)
        {
            int c = Edg[t][i];
            if(vis[c])continue;
            vis[c] = vis[t];
            Q.push(c);
        }
    }
}
int main()
{
    scanf("%d",&n);
    int a,b;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        Edg[a].push_back(b),
        Edg[b].push_back(a);
    }
    dfs(1,n);

    if(ans[1]>ans[2]) printf("Fennec\n");
    else              printf("Snuke\n");

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/81407555