cf936B
题意简述:给出一个有向图,问从s出发是否能找到一条长度为奇数的路径并且路径的端点出度为0,存在就输出路径,如果不存在判断图中是否存在环,存在输出Draw,否则输出lose
题解:类似于DP,将每一个点拆成两个点,d[x][0]=1表示存在一条s到x存在一条长度为奇数的路径,d[x][1]相反,然后正常bfs计算就好了
判环什么的都是小意思
#include<bits/stdc++.h>
#define forn(i, n) for (int i = 0 ; i < int(n) ; i++)
#define fore(i, s, t) for (int i = s ; i < (int)t ; i++)
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pf2(x,y) printf("%d %d\n",x,y)
#define pf(x) printf("%d\n",x)
#define each(x) for(auto it:x) cout<<it<<endl;
#define pii pair<int,int>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=2e5+5;
const int inf=1e9;
int n,m,s;
vector<int> g[maxn],ig[maxn];
int d[maxn][2],pre[maxn][2],vis[maxn];
bool dfs(int x){
vis[x]=1;
for(auto y:g[x])
if(vis[y]==1 || dfs(y)) return 1;
vis[x]=2;
return 0;
}
int main(){
cin>>n>>m;
queue<pair<int,int>> q;
for(int i=1;i<=n;i++){
int x=i,y;
int cc;
cin>>cc;
while(cc--){
cin>>y;
g[x].push_back(y);
ig[y].push_back(x);
}
if(g[x].size()==0) q.push({x,1});
}
while(q.size()){
pii x=q.front();q.pop();
for(auto y:ig[x.fi]){
if(d[y][x.se^1]==0) {
d[y][(x.se)^1]=1;
pre[y][(x.se)^1]=x.fi;
q.push({y,(x.se)^1});
}
}
}
cin>>s;
if(d[s][0]){
puts("Win");
vector<int> ans;
ans.push_back(s);
int p=s,px=0;
while(pre[p][px]){
p=pre[p][px],px^=1;
ans.push_back(p);
}
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<' ';
cout<<"\n";
}
else {
if(dfs(s)) puts("Draw");
else puts("Lose");
} }
cf936B的更多相关文章
随机推荐
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Visual Studio Code | 报错 command 'markdown.extension.onBackspaceKey' not found
背景 今天使用Visual Strudio Code想用键盘Back去删除一些字符,发现报如下错误: command 'markdown.extension.onBackspaceKey' not f ...
- Linux学习笔记-Centos7搭建owncloud私有云盘
使用环境:虚拟机centos7 1.下载安装LAMP相关软件 [root@localhost yum.repos.d]# yum install httpd –y [root@localhost yu ...
- Linux防火墙之iptables常用扩展匹配条件(一)
上一篇博文讲了iptables的基本匹配条件和隐式匹配条件,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12269717.html:今天在来说说iptabel ...
- Horizontal Pod Autoscaler(Pod水平自动伸缩)
Horizontal Pod Autoscaler 根据观察到的CPU利用率(或在支持自定义指标的情况下,根据其他一些应用程序提供的指标)自动伸缩 replication controller, de ...
- 练习2-15 求简单交错序列前N项和 (15 分)
练习2-15 求简单交错序列前N项和 (15 分) 本题要求编写程序,计算序列 1 - 1/4 + 1/7 - 1/10 + ... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格 ...
- SetConsoleTextAttribute设置颜色后的恢复
1. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <s ...
- Redis5.x五种数据类型常见命令
关注公众号:CoderBuff,回复"redis"获取<Redis5.x入门教程>完整版PDF. <Redis5.x入门教程>目录 第一章 · 准备工作 第 ...
- VFP CursorAdapter 起步一(作者:Doug Hennig 译者:fbilo)
CursorAdapter 类是 VFP 8 中最重要的新功能之一,因为它提供了一种简单易用.接口统一的访问远程数据源方式.在这个月的文章里,Dung Hennig 将向你展示 CursorAdapt ...
- Transformer 详解
感谢:https://www.jianshu.com/p/04b6dd396d62 Transformer模型由<Attention is all your need>论文中提出,在seq ...