Fennec VS. Snuke

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

//n个格子,编号为1-n,1开始是黑色,n开始是白色。有m条边,且为树,说明格子的相邻情况,然后Fnc先开始涂黑色,涂色规则是:格子没被涂过色,并且相邻有黑色格子然后Snu涂色,类似的规则,snu涂白色,相邻要有白色。轮流涂色,直到有一方不能涂了,另一方获胜。

显然,他们玩游戏会采取这样的策略,fnc先向着n点去涂色,snu向着1点去涂色,这样可以尽可能获得更多的地盘,然后就是比谁的地盘大咯用神奇DFS实现

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <vector>
 6 using namespace std;
 7 #define LL long long
 8 #define MX 100010
 9
10 int n;
11 int total;
12 vector<int> G[MX];
13 int colr[MX];
14
15 void DFS(int x,int pre,int c,int &tot)
16 {
17     if (colr[x]==-c) return;
18     tot++;
19     for (int i=0;i<G[x].size();i++)
20         if (G[x][i]!=pre)
21             DFS(G[x][i],x,c,tot);
22 }
23
24 void dfs(int u,int pre,int s,int &ok)
25 {
26     if (u==n)
27     {
28         ok=s;
29         return;
30     }
31     for (int i=0;i<G[u].size();i++)
32     {
33         if (ok) break;
34         if (G[u][i]!=pre)
35             dfs(G[u][i],u,s+1,ok);
36     }
37     if (ok)
38     {
39         if (u!=1&&s<=ok/2) colr[u]=1;
40         if (u!=n&&s>ok/2) colr[u]=-1;
41     }
42     return;
43 }
44
45 int main()
46 {
47     scanf("%d",&n);
48     for (int i=1;i<n;i++)
49     {
50         int u,v;
51         scanf("%d%d",&u,&v);
52         G[u].push_back(v);
53         G[v].push_back(u);
54     }
55     colr[1]=1; //hei
56     colr[n]=-1;//bai
57
58     int ok=0;
59     dfs(1,-10,0,ok);
60 /*
61     for (int i=1;i<=n;i++)
62         printf("%d ",colr[i]);
63     printf("\n");
64 */
65
66     int num_1=0;
67     DFS(1,-10,1,num_1);
68     int num_2=0;
69     DFS(n,-10,-1,num_2);
70     if (num_1-num_2>=1)
71         printf("Fennec\n");
72     else
73         printf("Snuke\n");
74     return 0;
75 }


				
时间: 2024-08-08 05:24:06

Fennec VS. Snuke的相关文章

[图论][BFS]Fennec VS. Snuke

题目描述 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 re

ARC078 D.Fennec VS. Snuke(树上博弈)

题目大意: 给定一棵n个结点的树 一开始黑方占据1号结点,白方占据n号结点 其他结点都没有颜色 每次黑方可以选择黑色结点临近的未染色结点,染成黑色 白方同理. 最后谁不能走谁输. 题解: 其实简单想想就可以想明白. 黑方肯定要往通往白方的最短路延伸,白方也是这样. 因为这样每次你可以最大化可行动次数. 所以先以1为根,dfs一遍,然后找到路径. 模拟一下走路径的过程,路径走光了就比谁的可行动次数多(有点像围棋的气的感觉),输出结果就可以了 #include <iostream> #includ

实践编译 Firefox Fennec

编译 Firefox的官方link在 https://wiki.mozilla.org/Mobile/Fennec/Android, 我只是记录下实践过程而已 Linux 下的 Firefox 是用gcc/g++ 基于glibc编译的 编译Android 下的 Firefox 需要使用 Android SDK 和Android NDK Android 的 SDK/NDK tools 使用了32 libs, 所以在 64位 Linux下,还要安装若干32位的libs If you're using

Snuke&#39;s Coloring 2-1

问题 H: Snuke's Coloring 2-1 时间限制: 1 Sec  内存限制: 128 MB提交: 141  解决: 59[提交][状态][讨论版][命题人:admin] 题目描述 There is a rectangle in the xy-plane, with its lower left corner at (0,0) and its upper right corner at (W,H). Each of its sides is parallel to the x-axi

問題の解決策 ARC099 D - Snuke Numbers

题目链接 题目链接2 题解 显然这是一道数论的题目..打表可以找出规律. code: #include "bits/stdc++.h" using namespace std; typedef long long ll; double Snuke(ll x) { ll tmp = 0, c = x; while(c) { tmp += c % 10; c /= 10; } return 1.0 * x / tmp; } int main() { int K; cin >>

Atcoder Snuke&#39;s Subway Trip 重构图

题目链接 这题主要是重构图的方法很难思考. 方法一:考虑在每个公司意义下的联通块,每个联通块对应一个虚拟节点,联通块内的节点到联通块对应的虚拟节点有一条边,构建出一个二分图,跑一遍BFS,将经过的边数除以2.这里有两种实现,                             细节都注释在了程序里 1 #include<bits/stdc++.h> 2 using namespace std; 3 vector<pair<int,int> > nei[1000005

【例题收藏】◇例题&#183;I◇ Snuke&#39;s Subway Trip

◇例题·I◇ Snuke's Subway Trip 题目来源:Atcoder Regular 061 E题(beta版) +传送门+ 一.解析 (1)最短路实现 由于在同一家公司的铁路上移动是不花费的,只有在换乘时会花费1日元.我们可以视换乘的花费为点权--当换乘到不同公司时花费1,同一家公司时花费0. 对于这种点权因情况而变化的题,最常见的做法便是拆点.设节点 u 相连的铁路由 c1,c2,...,cp p个公司修建,则将节点u拆分为节点 uc1~ucp,点uci表示到达点u的铁路是由公司c

[ARC061E]すぬけ君の地下鉄旅行 / Snuke&#39;s Subway Trip

题目大意:Snuke的城镇有地铁行驶,地铁线路图包括$N$个站点和$M$个地铁线.站点被从$1$到$N$的整数所标记,每条线路被一个公司所拥有,并且每个公司用彼此不同的整数来表示. 第$i$条线路($1\le i \le M$)是直接连接$p_i$与$q_i$的双向铁路,中间不存在其他站点,且这条铁路由$c_i$公司所拥有. 如果乘客只乘坐同一公司的铁路,他只需要花费一元,但如果更换其他公司的铁路需要再花一元.当然,如果你要再换回原来的公司,你还是要花一元. Snuke在1号站的位置出发,他想通

AtCoder ARC061E Snuke&#39;s Subway Trip 最短路

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门 ?Portal ?原题目描述在最下面. ?\(n(1e5)\)个点, \(m(2e5)\)条边, 每条边有一个属性值.经过一条同一属性值的连续路径花费为1.问从1到n的最小花费. Solution: 我的解法 ?直接边最短路搞,然后t的飞起.仔细一想,这样写的话有\(1e5\)个点,边数更是多到飞起,拿命跑啊,不过代码我还是放下面. 正解:拆点 ?把一条\(u