Codeforces Round #467 (Div. 1) B. Sleepy Game
我一开始把题目看错了 我以为是博弈。。
这题就是一个简单的判环+dfs(不简单,挺烦的一题)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
const int N = 2e5 + 5;
struct GraphNode {
int toVertex, nextPoint;
} GraphTable[N];
int headOfVertex[N];
bool visitedVertex[N][2];
int visitedCircle[N];
class Graph {
private:
int startVertex_;
int totEdge_;
int hasCircle_;
int hasSuccess_;
std::vector<int> ansPath;
bool dfsGraph(int vertex, int step) {
if (headOfVertex[vertex] == -1) {
// printf("%d %d\n", vertex, headOfVertex[vertex]);
return step & 1;
}
for (int i = headOfVertex[vertex]; ~i; i = GraphTable[i].nextPoint) {
int toVertex = GraphTable[i].toVertex;
if (!visitedVertex[toVertex][step]) {
visitedVertex[toVertex][step] = true;
bool hasSuccess = dfsGraph(toVertex, step ^ 1);
if (hasSuccess) {
ansPath.push_back(toVertex);
// printf("to %d\n", toVertex);
return true;
}
}
}
return false;
}
bool judgeCircle(int vertex) {
visitedCircle[vertex] = -1;
for (int i = headOfVertex[vertex]; ~i; i = GraphTable[i].nextPoint) {
int toVertex = GraphTable[i].toVertex;
if (visitedCircle[toVertex] == 0) {
bool flag = judgeCircle(toVertex);
if (flag)
return true;
visitedCircle[toVertex] = 1;
} else if (visitedCircle[toVertex] == -1)
return true;
}
return false;
}
public:
Graph() {
ansPath.clear();
totEdge_ = 0;
hasSuccess_ = false;
hasCircle_ = false;
memset(visitedVertex, 0, sizeof(visitedVertex));
memset(visitedCircle, 0, sizeof(visitedCircle));
memset(headOfVertex, -1, sizeof(headOfVertex));
}
void setStartVertex(int startVertex) { startVertex_ = startVertex; }
void addEdge(int fromVertex, int toVertex) {
// printf("%d %d\n", fromVertex, toVertex);
GraphTable[totEdge_].toVertex = toVertex;
GraphTable[totEdge_].nextPoint = headOfVertex[fromVertex];
headOfVertex[fromVertex] = totEdge_++;
}
void solve() {
visitedVertex[startVertex_][1] = true;
hasCircle_ = judgeCircle(startVertex_);
hasSuccess_ = dfsGraph(startVertex_, 0);
if (hasSuccess_) {
ansPath.push_back(startVertex_);
reverse(ansPath.begin(), ansPath.end());
printf("Win\n");
for (int i = 0; i < ansPath.size(); ++i)
printf("%d ", ansPath[i]);
printf("\n");
} else if (hasCircle_)
printf("Draw\n");
else
printf("Lose\n");
}
};
int main() {
int n, m;
while (~scanf("%d %d", &n, &m)) {
Graph Basa = Graph();
for (int i = 1; i <= n; ++i) {
int numberOfEdge;
scanf("%d", &numberOfEdge);
for (int j = 0; j < numberOfEdge; ++j) {
int toVertex;
scanf("%d", &toVertex);
Basa.addEdge(i, toVertex);
}
}
int startVertex;
scanf("%d", &startVertex);
Basa.setStartVertex(startVertex);
Basa.solve();
}
return 0;
}
Codeforces Round #467 (Div. 1) B. Sleepy Game的更多相关文章
- Codeforces Round #467 (div.2)
Codeforces Round #467 (div.2) 我才不会打这种比赛呢 (其实本来打算打的) 谁叫它推迟到了\(00:05\) 我爱睡觉 题解 A. Olympiad 翻译 给你若干人的成绩 ...
- Codeforces Round #467 (Div. 2) B. Vile Grasshoppers
2018-03-03 http://codeforces.com/problemset/problem/937/B B. Vile Grasshoppers time limit per test 1 ...
- Codeforces Round #467 Div.2题解
A. Olympiad time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #467 (Div. 1). C - Lock Puzzle
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> ...
- Codeforces Round #467 Div. 1
B:显然即相当于能否找一条有长度为奇数的路径使得终点出度为0.如果没有环直接dp即可.有环的话可以考虑死了的spfa,由于每个点我们至多只需要让其入队两次,复杂度变成了优秀的O(kE).事实上就是拆点 ...
- Codeforces Round #467 (Div. 2) E -Lock Puzzle
Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...
- Codeforces Round #467 (Div. 2) B. Vile Grasshoppers[求去掉2-y中所有2-p的数的倍数后剩下的最大值]
B. Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #467 (Div. 2) A. Olympiad[输入一组数,求该数列合法的子集个数]
A. Olympiad time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器
本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...
- webrtc初探之一对一的连接过程(一)
说明,我研究的是muan-khan的一个github项目,针对的是chrome对chrome,也就是pc对pc的一对一,一对多通话,感兴趣的可以继续往下看. github地址:https://gith ...
- typeof面试题解答
面试题 alert(typeof null); // object alert(typeof undefined); // undefined alert(typeof NaN); // number ...
- java中的@Override标签
@Override标签的作用: @Override是伪代码,表示方法重写. @Override标签的好处: 1.作为注释,帮助自己检查是否正确的复写了父类中已有的方法 2.便于别人理解代码 3.编译器 ...
- php 压缩函数gzencode gzdeflate gzcompress
gzencode 默认使用ZLIB_ENCODING_GZIP编码,使用gzip压缩格式,实际上是使用defalte 算法压缩数据,然后加上文件头和adler32校验 gzdeflate 默认使用ZL ...
- Javascript Sting(字符串)对象
一, 如何计算字符串的长度? 可以通过.length属性来计算 <script type="text/javascript"> var txt="Hello ...
- 好用的Google漏洞爬虫:Google Mass Explorer
这是一款基于谷歌搜索引擎的自动化爬虫. 爬虫介绍 爬虫大体机制就是: 先进行一次谷歌搜索,将结果解析为特定格式,然后再提供给exp使用. 大家可以尝试使用–help来列出所有参数. 这个项目笔者会持续 ...
- error: Autoconf version 2.67 or higher is required
error: Autoconf version 2.67 or higher is required 今天linux下遇到这种错误,顺便记录下来. #rpm -qf /usr/bin/autoconf ...
- php + 和 array_merge的区别
(1)对于+,当key相同时,都是舍弃后面的结果: array_merge ,当key相同时,key是字符,则后面的覆盖前面的:key是数字,则不发生覆盖,会重新建立数组索引. $arr1 = arr ...
- [CQOI2006]凸多边形
很明显是一道半平面交的题. 先说一下半平面交的步骤: 1.用点向法(点+向量)表示直线 2.极角排序,若极角相同,按相对位置排序. 3.去重,极角相同的保留更优的 4.枚举边维护双端队列 5.求答案 ...