Codeforces 936B
题意略。
思路:
图论里掺杂了一些动态规划。
有几个注意点:
1.dp时状态的设计:因为我们要寻求的是出度为0并且可以从起点走奇数步抵达的点,由于同一个点可以通过多种方式到达。
并且我们在获得奇数步点的时候,需要偶数步点作为支撑,所以visit[ i ][ j ]表示第i个点能否具备j状态(0、1),也即奇偶数步。
2.dp采用spfa,也即刷表法。在使用spfa时,最好将点和状态一起打包。
3.判断有向图是否存在环,可以采用拓扑排序。
详见代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ; struct node{
int v,state;
node(int v = ,int state = ){
this->v = v;
this->state = state;
}
}; int visit[maxn][],out[maxn],indegree[maxn],n,m,s;
bool vis[maxn];
vector<int> graph[maxn],vs;
queue<node> que;
queue<int> topque; void spfa(){
memset(visit,,sizeof(visit));
visit[s][] = -;
que.push(node(s,));
while(que.size()){
node nd = que.front();
que.pop();
int v = nd.v,state = nd.state;
for(int i = ;i < graph[v].size();++i){
int u = graph[v][i];
if(!visit[u][state ^ ]){
visit[u][state ^ ] = v;
que.push(node(u,state ^ ));
}
}
}
}
bool judcircle(){
int cnt = ;
for(int i = ;i <= n;++i){
if(!visit[i][] && !visit[i][]) continue;
if(indegree[i] == )
topque.push(i);
++cnt;
}
while(topque.size()){
int v = topque.front();
topque.pop();
--cnt;
for(int i = ;i < graph[v].size();++i){
int u = graph[v][i];
indegree[u] -= ;
if(indegree[u] == ) topque.push(u);
}
}
return cnt > ;
}
void dfs(int cur){
for(int i = ;i < graph[cur].size();++i){
int v = graph[cur][i];
++indegree[v];
if(indegree[v] == ){
dfs(v);
}
}
} int main(){
scanf("%d%d",&n,&m);
memset(indegree,,sizeof(indegree));
for(int i = ,to;i <= n;++i){
scanf("%d",&out[i]);
for(int j = ;j < out[i];++j){
scanf("%d",&to);
graph[i].push_back(to);
}
}
scanf("%d",&s);
dfs(s);
spfa();
bool circle = judcircle();
bool jud = false;
for(int i = ;i <= n && !jud;++i){
if(!out[i] && visit[i][] != ){
jud = true;
printf("Win\n");
int now = ;
for(int v = i;v != -;v = visit[v][now],now = now ^ ){
vs.push_back(v);
}
for(int j = vs.size() - ;j >= ;--j){
printf("%d%c",vs[j],j ? ' ' : '\n');
}
}
}
if(!jud){
printf("%s\n",circle ? "Draw" : "Lose");
}
return ;
} /*
6 6
1 2
2 3 4
1 5
1 5
1 6
0
3
*/
Codeforces 936B的更多相关文章
- Sleepy Game CodeForces - 936B
大意: 给定有向图, 初始点S, 两个人轮流移动, 谁不能移动则输, 但后手睡着了, 先手可以控制后手操作, 求最后先手结果. 刚开始看错了, 还以为后手也是最优策略.... 实际上判断是否有偶数个节 ...
- CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS
题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...
- codeforces的dp专题
1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- CentOS EPEL yum源
CentOS EPEL yum源 用yum安装软件时,经常发现我们的yum源里面没有该软件,比如htop.网上查到的一个方案是需要自己去wget源码,然后configure,make,make ins ...
- 爬虫之解析库pyquery
初始化 安装: pip install pyquery 字符串的形式初始化 html = """ <html lang="en"> < ...
- MacBook Air多出一块磁盘?
今天将MAC的系统升级到Mojave,启动之后发现系统挂载的磁盘变了,我记得升级之前文件系统是挂载在/dev/disk0上的,但是升级之后,文件系统挂载在/dev/disk1上了. 用diskutil ...
- CentOS7安装高版本gcc
CentOS7安装高版本gcc 下载 从hust镜像站下载gcc源码包. http://mirror.hust.edu.cn/gnu/gcc/ 我选择的是gcc-8.3.0.tar.gz. cd mk ...
- Is it a full physical image???
My friend asked me why she could not find some important files in a physical image acquired from an ...
- 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表
1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...
- idea 2018.3.4安装破解
电脑环境:win10 64位 1.idea官网下载: 链接:https://www.jetbrains.com/idea/,如下图: 2.JDK官网下载: 链接:https://www.oracle. ...
- 以太坊RLPx传输协议
本文主要内容翻译自:The RLPx Transport Protocol,其中添加了一些个人的理解,由于密码学水平有限,不正确之处望指正.另外原文可能已经更新,最新内容请直接阅读原文. 本文档定义了 ...
- 无法正常卸载pr
控制面板找不到pr和ps,根本无法卸载,我试过官方工具没用,也试过ccleaner,也检测不到?
- Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F
传送门 A 贪心的取 每个字母n/k次 令r=n%k 让前r个字母各取一次 #include <bits/stdc++.h> using namespace std; typedef lo ...