B. Sleepy Game
http://codeforces.com/problemset/problem/936/B
Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and m edges. One of the vertices contains a chip. Initially the chip is located at vertex s. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't move the chip loses. If the game lasts for 106 turns the draw is announced.
Vasya was performing big laboratory work in "Spelling and parts of speech" at night before the game, so he fell asleep at the very beginning of the game. Petya decided to take the advantage of this situation and make both Petya's and Vasya's moves.
Your task is to help Petya find out if he can win the game or at least draw a tie.
The first line of input contain two integers n and m — the number of vertices and the number of edges in the graph (2 ≤ n ≤ 105, 0 ≤ m ≤ 2·105).
The next n lines contain the information about edges of the graph. i-th line (1 ≤ i ≤ n) contains nonnegative integer ci — number of vertices such that there is an edge from i to these vertices and cidistinct integers ai, j — indices of these vertices (1 ≤ ai, j ≤ n, ai, j ≠ i).
It is guaranteed that the total sum of ci equals to m.
The next line contains index of vertex s — the initial position of the chip (1 ≤ s ≤ n).
If Petya can win print «Win» in the first line. In the next line print numbers v1, v2, ..., vk (1 ≤ k ≤ 106) — the sequence of vertices Petya should visit for the winning. Vertex v1 should coincide with s. For i = 1... k - 1 there should be an edge from vi to vi + 1 in the graph. There must be no possible move from vertex vk. The sequence should be such that Petya wins the game.
If Petya can't win but can draw a tie, print «Draw» in the only line. Otherwise print «Lose».
5 6
2 2 3
2 4 5
1 4
1 5
0
1
Win
1 2 4 5
3 2
1 3
1 1
0
2
Lose
2 2
1 2
1 1
1
Draw
In the first example the graph is the following:
Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins.
In the second example the graph is the following:
Initially the chip is located at vertex 2. The only possible Petya's move is to go to vertex 1. After that he has to go to 3 for Vasya. Now it's Petya's turn but he has no possible move, so Petya loses.
In the third example the graph is the following:
Petya can't win, but he can move along the cycle, so the players will draw a tie.
这题很坑,还是我太弱了,套路太浅
1.这种图论问奇偶数的问题,一定要考虑奇环的存在(走一个奇环可以改变路径的奇偶)(就是这个地方让我一直TLE,mmp)
所以呢,打标记数组改成vis[maxn][2](用上^1),且就是为了防止重复走不必要的一个顶点,此题的dfs回溯不需要清空vis!!!
2.判环的存在这题就直接过程中book掉就好,不过也要去看看tarjan算法(算环的个数)
http://blog.csdn.net/qq_34374664/article/details/77488976
3.判出度为0的学到可以新开一个chu[]存(这样看起来舒服),不用head[u]==0这种,且链式前向星存图注意顶点为0的情况时需要初始化为-1
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = 2e5 + ;
// name*******************************
struct edge
{
int to,next;
} e[maxn];
int tot=;
int head[maxn];
int n,m,s;
int book[maxn];
int vis[maxn][];
int chu[maxn];
int res[maxn];
int cnt=;
int flag=;
// function******************************
void add(int u,int v)
{
tot++;
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot;
}
void dfs(int u,int mk)
{
for(int p=head[u]; p; p=e[p].next)
{
int v=e[p].to;
// cout<<"u:"<<u<<" v:"<<v<<" cnt:"<<cnt<<" flag:"<<flag<<" mk:"<<(mk^1)<<endl;
if(book[v])flag=;
book[v]=;
if(vis[v][mk^])continue;
vis[v][mk^]=;
res[++cnt]=v;
if(!chu[v]&&!mk)
{
cout<<"Win"<<endl;
For(i,,cnt)
{
cout<<res[i]<<" ";
}
exit();
}
dfs(v,mk^);
book[v]=;
cnt--;
}
} //***************************************
int main()
{
ios::sync_with_stdio();
cin.tie();
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin>>n>>m;
For(i,,n)
{
int t;
cin>>t;
chu[i]=t;
For(j,,t)
{
int x;
cin>>x;
add(i,x);
}
}
cin>>s;
book[s]=;
res[++cnt]=s;
dfs(s,);
if(flag)
cout<<"Draw";
else
cout<<"Lose"; return ;
}
B. Sleepy Game的更多相关文章
- 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting
题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...
- Codeforces 937D - Sleepy Game
937D - Sleepy Game 思路: dfs. vis[u][0]==1表示u这个点能从s点偶数路径到达 vis[u][1]==1表示u这个点能从s点奇数路径到达 这个样就能保证dfs时每个点 ...
- Codeforces 937 D. Sleepy Game(DFS 判断环)
题目链接: Sleepy Game 题意: Petya and Vasya 在玩移动旗子的游戏, 谁不能移动就输了. Vasya在订移动计划的时候睡着了, 然后Petya 就想趁着Vasya睡着的时候 ...
- Codeforces 937.D Sleepy Game
D. Sleepy Game time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
\(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...
- P5200 [USACO19JAN]Sleepy Cow Sorting
P5200 [USACO19JAN]Sleepy Cow Sorting 题目描述 Farmer John正在尝试将他的N头奶牛(1≤N≤10^5),方便起见编号为1…N,在她们前往牧草地吃早餐之前排 ...
- C Sleepy Kaguya
链接:https://ac.nowcoder.com/acm/contest/338/C来源:牛客网 题目描述 Houraisan☆Kaguya is the princess who lives i ...
- Sleepy与DbgHlp库学习
参考:http://msdn.microsoft.com/en-us/library/windows/desktop/ms679291(v=vs.85).aspx http://msdn.micros ...
- UVa 10427 - Naughty Sleepy Boys
题目大意:从1开始往后写数字,构成一个如下的字符串 123456789101112... .求第n位的数字是多少. 找规律,按数字的位数可以构建一个类似杨辉三角的东西,求出第n位是哪个数的第几位即可. ...
- Codeforces Round #467 (Div. 1) B. Sleepy Game
我一开始把题目看错了 我以为是博弈.. 这题就是一个简单的判环+dfs(不简单,挺烦的一题) #include <algorithm> #include <cstdio> #i ...
随机推荐
- 小程序视图层(xx.xml)和逻辑层(xx.js)
整个系统分为两块视图层(View)和逻辑层(App Service) 框架可以让数据与视图非常简单地保持同步.当做数据修改的时候,只需要在逻辑层修改数据,视图层就会做相应的更新. 通过这个简单的例子来 ...
- TensorFlow分布式部署【单机多卡】
让TensorFlow飞一会儿 面对大型的深度神经网络训练工程,训练的时间非常重要.训练的时间长短依赖于计算处理器也就是GPU,然而单个GPU的计算能力有限,利用多个GPU进行分布式部署,同时完成一个 ...
- 网络 互联网接入方法、Mbit与MB的转换
ADSL:非对称数字用户环路(绝大多数家庭接入方法,使用电话线).可以提供最高1Mbps的上行速率和最高8Mbps的下行速率.最新的ADSL2+可以提供最高24Mbps的下行速率. 千千兆TB 千兆G ...
- SqlServer横向扩展负载均衡终极利器SqlServerProxy 不限功能永久免费
一直以来,MySQL因为开源,诞生了很多扩展方案,类似Amoeba.Atlas.Cobar.MySQLProxy等,大都基于MySQL通信协议来定制解决方案,让我们很羡慕嫉妒,但没办法,Microso ...
- 将 ExpressRoute 线路从经典部署模型转移到 Resource Manager 部署模型
本文概述将 Azure ExpressRoute 线路从经典部署模型转移到 Azure Resource Manager 部署模型的效果. Azure 当前使用两种部署模型:Resource Mana ...
- MySQL: OPTIMIZE TABLE: Table does not support optimize, doing recreate + analyze instead
show create table history;-------------------------- CREATE TABLE `foo` ( `itemid` bigint(20) unsig ...
- 标准的Flask启动文件
最近有一些同学问了我一些项目结构的问题 所以今天给大家专门讲解 解耦后的项目 目录我会分为两种方式:一种是普通解耦 一种是多mvc解耦 首先 我没先建立我们程序的文件夹并且在这个文件夹内写一个和这个文 ...
- python安装lib库
time:2015/11/11 双十一 一.初衷 看到一篇帖子[1],里面有python代码,就想实现一下,代码如下: import cv2 as cv import numpy as np from ...
- spine获取骨骼位置
time: 2015/07/23 版本: /****************************************************************************** ...
- Spring Boot+MyBabits静态连接多个数据库
1.修改.properties first.datasource.jdbc-url=jdbc:mysql://localhost/forwind first.datasource.username=r ...