Description

This summer's heat wave and drought unleashed devastating wildfires all across the Earth. Of course, a tiny country on the island "Yars and Eva" is also affected by this ecological disaster. Thanks to the well-organized actions of rescuers, all the citizens were evacuated to the nearby planets on a spaceship.

To save the country, a small fire robot was left on its territory. He managed to extinguish fire in all cities except the capital before running out of liquid. The robot can't extinguish fire anymore, so the country is still in danger at the moment.

There are n cities in the country connected by m two-way roads. Each road connects a pair of cities. There is at most one road between any pair of cities. The cities are numbered from 1 to n, with capital having the number 1.

The fire spreads very quickly. On the very first day only the capital is on fire. But with every subsequent day, the fire devours all the cities connected by a road with the cities that are already on fire. Once the fire gets to a certain city, this city will continue to stay on fire till the very end.

The robot can't extinguish the fire anymore and there are no other means of firefighting left in the country, so obviously the country is going to be burned down to the ground. And you don't have to be a hero and save it. The key thing is that the robot is going to be destroyed by fire as well, and you need to figure out who will actually pay for the loss of government property.

Two pilots, Nikolay and Vladimir, are on Earth's natural satellite. They alternately take turns controlling the robot. The pilots alternate each day. Robot's speed is equal to the speed of fire, so the robot can get to the neighboring city in a day. Each pilot does not want the robot to be destroyed on his turn. For such a valuable loss they will have to pay a huge fee to the government.

On the first day the robot is located in the capital. Nikolay controls the robot on the first day. Thus, Nikolay controls the robot on the days with odd numbers, and Vladimir controls it on the days with even numbers. Taking turn, a pilot has to move the robot from the current city to any city connected by a road with the current one. If a pilot moves the robot to a city which is on fire, the robot is destroyed.

You task is to figure out who will pay the fine for the destroyed robot, assuming both pilots act optimally.

Input

The first line of input contains the amount of cities n and the amount of roads m in the country (2 ≤ n ≤ 1000, n-1 ≤ m ≤ 1000). The following m lines contain description of the roads: ab — indices of the cities connected by roads (1 ≤ a ≤ n, 1 ≤ b ≤ na ≠ b). The roads are bidirectional. No pair of cities will be connected by more than one road. There will be a path between any two cities.

Output

Output the name of the pilot who will pay the fine, assuming both pilots act optimally ("

Nikolay

" — if it is Nikolay, "

Vladimir

" — if it is Vladimir).

 
题目大意:有n个点,m条无向边,棋子在点1,一人走一步,只能走到直接关联的点,然后点1着火了,每次从1开始扩散,与着火点直接关联的点都会着火,而且不会灭,谁往火力走谁输。
思路:先看火的蔓延,每次都会往外扩散,棋子也每次往外走一步。BFS求每个点到1的最短路径,然后棋子每一步都只能走与之直接相连而且最短路比之大1的点。谁走到无路可走谁就输,那么不管怎么走走到某一点的必然是同一个人,到1的最短路径为单数的是对方,复数的是自己。而每一点的后续都是固定的,那么每一点的胜败状态也是固定的(假设两人都足够聪明)。那么考虑任意一个状态,如果是自己走,那么只要有一个下一步是必胜状态,那么这个状态就是必胜状态,否则为必败状态(怎么走都输);如果是对方走,那么对方只要有一个下一步是我的必败状态,这个状态就是我的必败状态,否则为必胜状态(对方怎么走都是我赢)。记录状态的话,时间复杂度就是O(n)。(其实还有更简单的解法不过我就不说了……)
PS:代码能力不够啊WA了无数次……
 
 #include <cstdio>
#include <cstring>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = ; int n, m, f, ecnt;
int head[MAXN], dis[MAXN];
int to[MAXE], next[MAXE]; void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void bfs() {
queue<int> que; que.push();
dis[] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(!dis[v]) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int win[MAXN]; int dfs(int u) {
if(win[u] != -) return win[u];
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(dis[v] == dis[u] + ) {
if(dis[u] & ) {if(dfs(v)) return win[u] = ;}
else if(!dfs(v)) return win[u] = ;
}
}
return win[u] = !(dis[u] & );
} int main() {
scanf("%d%d", &n, &m);
ecnt = ;
while(m--) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
}
bfs();
memset(win, -, sizeof(win));
if(dfs()) puts("Vladimir");
else puts("Nikolay");
}

SGU 520 Fire in the Country(博弈+搜索)的更多相关文章

  1. HDU 5723 Abandoned country 最小生成树+搜索

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move

    poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...

  3. Gym 101246D Fire in the Country(dfs求SG函数)

    http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从 ...

  4. D - Stone Division HackerRank - stone-division (博弈+搜索)

    题目链接:https://cn.vjudge.net/problem/HackerRank-stone-division 题目大意:给你n,m,然后是m个数.每一次你可以选择一个a[i],如果能被n整 ...

  5. B. Sleepy Game 博弈搜索

    题意:给一个有向图和起点,然后只有一名选手,这名选手可以随意挪动棋子,最终不能动的时候走过的边为奇数边为Win并输出路径,否则如果有环输出Draw,否则输出Lose; 题目链接 知道状态数最多只有n* ...

  6. 极小极大搜索方法、负值最大算法和Alpha-Beta搜索方法

    1. 极小极大搜索方法    一般应用在博弈搜索中,比如:围棋,五子棋,象棋等.结果有三种可能:胜利.失败和平局.暴力搜索,如果想通过暴力搜索,把最终的结果得到的话,搜索树的深度太大了,机器不能满足, ...

  7. 转:极小极大搜索方法、负值最大算法和Alpha-Beta搜索方法

    转自:极小极大搜索方法.负值最大算法和Alpha-Beta搜索方法 1. 极小极大搜索方法    一般应用在博弈搜索中,比如:围棋,五子棋,象棋等.结果有三种可能:胜利.失败和平局.暴力搜索,如果想通 ...

  8. shodan搜索

    扫描一切联网的设备 www.shodan.io 一.ip 直接搜索:123.123.123.123 二.搜索服务 http http country:"DE" 指定搜索德国 htt ...

  9. Shodan新手入坑指南

    *本文原创作者:xiaix,本文属FreeBuf原创奖励计划,未经许可禁止转载 亲们~黑五 Shodan Membership 只要5刀,你剁手了没? 什么是 Shodan? 首先,Shodan 是一 ...

随机推荐

  1. Hibernate一级缓存和三种状态

    Hibernate一级缓存又称session缓存,生命周期很短,跟session生命周期相同. 三种状态:1.transient(瞬时态):刚new出来的对象,既不在数据库中,也不在session管理 ...

  2. js的垃圾回收机制

    Js具有自动垃圾回收机制.垃圾收集器会按照固定的时间间隔周期性的执行. JS中最常见的垃圾回收方式是标记清除. 工作原理:是当变量进入环境时,将这个变量标记为“进入环境”.当变量离开环境时,则将其标记 ...

  3. wpf中使用cefsharp加载本地html网页并实现cs和js的交互,并且cefsharp支持any cpu

    废话少说,直接上代码: 第一步: 第二步: 第三步: 第四步: App.xaml.cs对应的代码: using CefSharp; using CefSharp.Wpf; using System; ...

  4. UDP实现网络通信程序

    VC6.0创建基于UDP协议的网络聊天程序 只有一个工程UDP,服务器和客户端都是这个工程,因为UDP中C/S区分不强化 只讲关键部分,避免累赘 1.为对话框添加控件 2.为控件绑定变量和消息函数 启 ...

  5. .Net core 还原Nuget包失败的解决方法

    今天是2018最后一天了,真是神奇的一年啊,写个博客压压惊,来年继续加油吧..... 正文: 当我们打开.net core 项目时候,发现输出提示nuget包还原失败,这个时候首先要考虑.Net Co ...

  6. mysql8.0新增用户及密码加密规则修改

    MySQL8.0已经发布GA版,当前最新GA版本为8.0.12.虽然相对于之前版本,MySQL8.0没有加入新元素,但是,经过代码重构,MySQL8.0的优化器更加强大,同时也有一些新特性,如支持索引 ...

  7. Python系列之入门篇——pytables及其客户端

    pytables及其客户端查看 pytables # ubuntu sudo apt-get install python-tables pip install flask flask-httpaut ...

  8. hadoop 1.x 集群环境的搭建

    本文主要以个人工作学习总结为主,同时也为了方便更多的兴趣爱好者参与学习交流,现将具体的搭建步骤分享如下: 一.基础环境 1.1 jdk的安装与配置 Hadoop是用Java开发的,Hadoop的编译及 ...

  9. Leecode刷题之旅-C语言/python-217存在重复元素

    /* * @lc app=leetcode.cn id=217 lang=c * * [217] 存在重复元素 * * https://leetcode-cn.com/problems/contain ...

  10. pyhton 下 使用getch(), 输入字符无需回车

    原代码来自 https://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin ...