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实现
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <vector>
- using namespace std;
- #define LL long long
- #define MX 100010
- int n;
- int total;
- vector<int> G[MX];
- int colr[MX];
- void DFS(int x,int pre,int c,int &tot)
- {
- if (colr[x]==-c) return;
- tot++;
- for (int i=;i<G[x].size();i++)
- if (G[x][i]!=pre)
- DFS(G[x][i],x,c,tot);
- }
- void dfs(int u,int pre,int s,int &ok)
- {
- if (u==n)
- {
- ok=s;
- return;
- }
- for (int i=;i<G[u].size();i++)
- {
- if (ok) break;
- if (G[u][i]!=pre)
- dfs(G[u][i],u,s+,ok);
- }
- if (ok)
- {
- if (u!=&&s<=ok/) colr[u]=;
- if (u!=n&&s>ok/) colr[u]=-;
- }
- return;
- }
- int main()
- {
- scanf("%d",&n);
- for (int i=;i<n;i++)
- {
- int u,v;
- scanf("%d%d",&u,&v);
- G[u].push_back(v);
- G[v].push_back(u);
- }
- colr[]=; //hei
- colr[n]=-;//bai
- int ok=;
- dfs(,-,,ok);
- /*
- for (int i=1;i<=n;i++)
- printf("%d ",colr[i]);
- printf("\n");
- */
- int num_1=;
- DFS(,-,,num_1);
- int num_2=;
- DFS(n,-,-,num_2);
- if (num_1-num_2>=)
- printf("Fennec\n");
- else
- printf("Snuke\n");
- return ;
- }
Fennec VS. Snuke的更多相关文章
- 【AtCoder078D】Fennec VS. Snuke
AtCoder Regular Contest 078 D - Fennec VS. Snuke 题意 给一个树,1是白色,n是黑色,其它没有颜色.Fennec每次可以染白色点的直接邻居为白色.Snu ...
- Fennec VS. Snuke --AtCoder
题目描述 Fennec and Snuke are playing a board game.On the board, there are N cells numbered 1 through N, ...
- AtCoder Beginner Contest 067 D - Fennec VS. Snuke
D - Fennec VS. Snuke Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement F ...
- ABC Fennec VS. Snuke
题目描述 Fennec and Snuke are playing a board game. On the board, there are N cells numbered 1 through N ...
- ARC078 D.Fennec VS. Snuke(树上博弈)
题目大意: 给定一棵n个结点的树 一开始黑方占据1号结点,白方占据n号结点 其他结点都没有颜色 每次黑方可以选择黑色结点临近的未染色结点,染成黑色 白方同理. 最后谁不能走谁输. 题解: 其实简单想想 ...
- AtCoder Regular Contest 078
我好菜啊,ARC注定出不了F系列.要是出了说不定就橙了. C - Splitting Pile 题意:把序列分成左右两部分,使得两边和之差最小. #include<cstdio> #inc ...
- 【AtCoder】ARC078
C - Splitting Pile 枚举从哪里开始分的即可 #include <bits/stdc++.h> #define fi first #define se second #de ...
- AtCoder Regular Contest 078 D
D - Fennec VS. Snuke Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement F ...
- Snuke's Subway Trip
すぬけ君の地下鉄旅行 / Snuke's Subway Trip Time limit : 3sec / Memory limit : 256MB Score : 600 points Problem ...
随机推荐
- php解耦的三种境界
我们有三个类,Db,FileSystem,Session;实际业务需求要组合操作这三个类. 一.常规做法 class Db { public function read($id) { } } clas ...
- C语言面向对象编程(五):单链表实现(转)
这里实现的单链表,可以存储任意数据类型,支持增.删.改.查找.插入等基本操作.(本文提供的是完整代码,可能有些长.) 下面是头文件: #ifndef SLIST_H #define SLIST_H # ...
- Java: 获取当前执行位置的文件名/类名/方法名/行号
在 JAVA 程序有时需要获取当前代码位置, 于是就利用 Thread.currentThread().getStackTrace() 写了下面这个工具类, 用来获取当前执行位置处代码的文件名/类名/ ...
- codeforces #550D Regular Bridge 构造
题目大意:给定k(1≤k≤100),要求构造一张简单无向连通图,使得存在一个桥,且每一个点的度数都为k k为偶数时无解 证明: 将这个图缩边双,能够得到一棵树 那么一定存在一个叶节点,仅仅连接一条桥边 ...
- MATLAB 的数据类型
在MATLAB中有15种基本的数据类型: 8种整型数据类型.单精度浮点型(float).双精度浮点型(double).逻辑型(logical).字符串型(char).单元数组型(cell).结构体类型 ...
- spring事务管理源码解析--加了@Transactional注解后Spring究竟为我们做了哪些事情?
大家都知道事务管理是基于AOP的,对AOP还不了解的请自行百度. 实现一个事务需要以下几步:1.获取数据库连接 2.执行数据库操作 3.如果2步骤发生异常就回滚,否则就提交 4.释放资源. 然后 ...
- js - 类模拟
JavaScript 中并没有真正的类,但JavaScript 中有 构造函数 和 new 运算符. - 任何JavaScript 函数都可以用做构造函数, - 构造函数必须使用 new 运算符来创建 ...
- 在CentOS 6.3中安装拼音输入法
安装:su root yum install "@Chinese Support" // 安装中文输入法 exit安装完毕,在“系统-->首选项”会看到“输入法”一 ...
- mongodb 实现关系型数据库中查询某一列 的效果
近期在tornado\mongodb\ansible mongodb中有个find()方法非常牛逼,能够将集合中全部的表都传出来,一開始我这么写 class Module_actionHandler( ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...