AtCoder Beginner Contest 067 D - Fennec VS. Snuke
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
F先走,S后走,且只能走相邻的点,所以,bfs搜索所有的点,s可以走的点不少于f是就获胜
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define PI 3.141592653589793238462 #define INF 1000000000 #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; vector<int>v[100006]; int x,y,n; int ans[4]={0,0,0}; int vis[100006]; void bfs(int x,int y) { mem(vis); vis[x]=1; vis[y]=2; queue<int>q; q.push(x); q.push(y); while(!q.empty()) { int pos=q.front(); ans[vis[pos]]++; q.pop(); for(int i=0;i<v[pos].size();i++) { if(vis[v[pos][i]]) continue; vis[v[pos][i]]=vis[pos]; q.push(v[pos][i]); } } } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&x,&y); v[y].push_back(x); v[x].push_back(y); } bfs(1,n); puts(ans[2]>=ans[1]?"Snuke":"Fennec"); }