[CF936B]Sleepy Game

122 篇文章 0 订阅

题目

传送门 to VJ

传送门 to CF

思路

就是询问你能不能走奇数步走到一个没有出度的点。

这可以用大力 bfs \text{bfs} bfs 实现。

然后平局就老老实实用 tarjan \text{tarjan} tarjan 吧。

代码

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0' or c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c and c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}
inline void writeint(long long x){
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) writeint(x/10);
	putchar((x%10)^48);
}
# define MB template < class T >
MB void getMax(T &a,const T &b){ if(a < b) a = b; }
MB void getMin(T &a,const T &b){ if(b < a) a = b; }

struct Edge{
	int to, nxt;
	Edge(int T=0,int N=0):to(T),nxt(N){}
};

const int MaxN = 100005;
int head[MaxN], cntEdge; Edge edge[MaxN<<2];

void addEdge(int from,int to){
	edge[cntEdge] = Edge(to,head[from]);
	head[from] = cntEdge ++;
}

bool vis[MaxN][2]; // 0: even 1: odd
int pre[MaxN][2];
queue<int> q;
void bfs(int x){
	q.push(x), vis[x][0] = true;
	while(not q.empty()){
		x = q.front(); q.pop();
		for(int i=head[x]; ~i; i=edge[i].nxt)
			for(int j=0; j<2; ++j)
				if(vis[x][j] and not vis[edge[i].to][j^1]){
					vis[edge[i].to][j^1] = true;
					pre[edge[i].to][j^1] = x;
					q.push(edge[i].to);
				}
	}
}

int dfn[MaxN], low[MaxN], dfsClock;
bool inSta[MaxN], circle;
void tarjan(int x){
	low[x] = dfn[x] = ++ dfsClock;
	inSta[x] = true;
	for(int i=head[x]; ~i; i=edge[i].nxt){
		if(dfn[edge[i].to] == 0){
			tarjan(edge[i].to);
			getMin(low[x],low[edge[i].to]);
		} else if(inSta[edge[i].to])
			getMin(low[x],low[edge[i].to]);
	}
	if(low[x] != dfn[x]) // 存在一个环
		circle = true;
	inSta[x] = false;
}

void printPath(int x,int d){
	if(pre[x][d] != 0)
		printPath(pre[x][d],d^1);
	printf("%d ",x);
}

int main(){
	int n = readint(); readint();
	for(int i=1; i<=n; ++i)
		head[i] = -1;
	for(int i=1; i<=n; ++i){
		int m = readint();
		while(m --)
			addEdge(i,readint());
	}
	int started = readint();
	bfs(started), tarjan(started);
	for(int x=1; x<=n; ++x)
		if(head[x] == -1 and vis[x][1]){
			puts("Win"); printPath(x,1);
			return 0;
		}
	if(circle) return puts("Draw")*0;
	return puts("Lose")*0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值