题意略。

思路:

图论里掺杂了一些动态规划。

有几个注意点:

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的更多相关文章

  1. Sleepy Game CodeForces - 936B

    大意: 给定有向图, 初始点S, 两个人轮流移动, 谁不能移动则输, 但后手睡着了, 先手可以控制后手操作, 求最后先手结果. 刚开始看错了, 还以为后手也是最优策略.... 实际上判断是否有偶数个节 ...

  2. CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS

    题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...

  3. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. php 自己封装一个调用第三方接口的函数

    ①在php.ini中开启php_curl扩展(必须开启) ②建议在php.ini中开启php_openssl扩展(本身不是curl必须的,是调用一些第三方接口需要的 ③如果以上操作重启apache后, ...

  2. PHP ErrorException 积累

    ErrorException [不定时更新] ErrorException1: Undefined index: allocate 描述:PHP默认会对未声明变量进行提示,这种默认的提示是可以进行忽略 ...

  3. 用python输出菱形

    num = eval(input('请输入最多*所在行数:')) a = num b = num #上三角 for i in range(1,num+1): print((a-1) *' ', (2* ...

  4. 第15个算法-实现 Trie (前缀树)(LeetCode)

    解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...

  5. activeMQ_helloworld(一)

    一.activeMQ下载,直接在Linux上wget http://mirror.bit.edu.cn/apache//activemq/5.14.5/apache-activemq-5.14.5-b ...

  6. SpringBoot日志相关

    SpringBoot使用的是SLF4j当门面,Logback当实现完成 日志级别 数字越大,级别越高,框架只会输出大于等于当前日志级别的信息 ERROR 40 WARN 30 INFO 20 DEBU ...

  7. ProcessBuilder waitFor 调用外部应用

    小程序项目最初使用ffmpeg转换微信录音文件为wav格式,再交给阿里云asr识别成文字.视频音频转换最常用是ffmpeg. 1 ffmpeg -i a.mp3 b.wav 相关文章: 小程序实现语音 ...

  8. 关于dfs的套路

    void dfs(答案, 搜索层数, 其他参数) { if (层数==maxdeep) { 更新答案 return; } (剪枝) for(下一层可能的状态){ 更新全局变量表示的状态的变量 dfs( ...

  9. 简洁明了的Noip考场策略 / 平时做题也适用

    1.选择策略: 评估的标准得分的难度不是AC的难度 2.思考问题: 怀疑的眼光审视自己 3.写代码前: 想想可不可以换一种代码实现会好写很多 把自己的思路再理一遍,可以写到纸上,记下来大致关键顺序 4 ...

  10. 3PHP如何用PDO的连接方式方式导出mysql数据

    首先连接mysql,具体看上一篇 接下来在try{}中加入以下代码 $query="select * from 你的数据表名称"          //$query的内容给个SQL ...