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,最后一点颜色为2,1颜色可以将它相邻的点染色成1颜色,2颜色同理,现在F先手,S后手,最后不能染色算输,最后谁赢了
解法:
1 F开始染色第1点和它周围点(ABC....),然后S开始染色第N点和它周围点(abc..),然后F染色A点和A点相邻点,然后S染色a点和a点相邻点...
2 明白了吗?最后谁染色的点多谁就赢了
3 这里染色讲究先后,我们用队列广搜
1 #include <iostream> 2 #include <stdio.h> 3 #include <vector> 4 #include <queue> 5 #define N 100005 6 using namespace std; 7 vector <int> vec[N]; 8 int color[N]; 9 int main(){ 10 queue <int> que; 11 int n , x , y; 12 scanf("%d",&n); 13 for(int i = 1 ; i < n ; i ++){ 14 scanf("%d%d",&x,&y); 15 vec[x].push_back(y); 16 vec[y].push_back(x); 17 } 18 color[1] = 1 , color[n] = 2; 19 int cnt[3] = {0 , 0}; 20 que.push(1) ; 21 que.push(n); 22 while(!que.empty()){ 23 int x = que.front(); 24 que.pop(); 25 cnt[color[x]] ++; 26 27 for(int i = 0 ; i < vec[x].size() ; i ++){ 28 int v = vec[x][i]; 29 if(color[v]) continue; 30 color[v] = color[x]; 31 que.push(v); 32 } 33 } 34 if(cnt[2] >= cnt[1]){ 35 printf("Snuke "); 36 }else{ 37 printf("Fennec "); 38 } 39 40 }