cf 936B Sleepy Game

本文介绍了一个游戏策略问题,玩家在一个有向图上轮流移动棋子,目标是使对手无法行动而取胜或至少保持不败。文章提供了算法思路和C++代码实现,通过构建辅助图和深度优先搜索来解决此问题。

一 原题

B. Sleepy Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and medges. One of the vertices contains a chip. Initially the chip is located at vertex s. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't move the chip loses. If the game lasts for 106 turns the draw is announced.

Vasya was performing big laboratory work in "Spelling and parts of speech" at night before the game, so he fell asleep at the very beginning of the game. Petya decided to take the advantage of this situation and make both Petya's and Vasya's moves.

Your task is to help Petya find out if he can win the game or at least draw a tie.

Input

The first line of input contain two integers n and m — the number of vertices and the number of edges in the graph (2 ≤ n ≤ 1050 ≤ m ≤ 2·105).

The next n lines contain the information about edges of the graph. i-th line (1 ≤ i ≤ n) contains nonnegative integer ci — number of vertices such that there is an edge from i to these vertices and ci distinct integers ai, j — indices of these vertices (1 ≤ ai, j ≤ nai, j ≠ i).

It is guaranteed that the total sum of ci equals to m.

The next line contains index of vertex s — the initial position of the chip (1 ≤ s ≤ n).

Output

If Petya can win print «Win» in the first line. In the next line print numbers v1, v2, ..., vk (1 ≤ k ≤ 106) — the sequence of vertices Petya should visit for the winning. Vertex v1 should coincide with s. For i = 1... k - 1 there should be an edge from vi to vi + 1 in the graph. There must be no possible move from vertex vk. The sequence should be such that Petya wins the game.

If Petya can't win but can draw a tie, print «Draw» in the only line. Otherwise print «Lose».

Examples
input
Copy
5 6
2 2 3
2 4 5
1 4
1 5
0
1
output
Win
1 2 4 5 
input
Copy
3 2
1 3
1 1
0
2
output
Lose
input
Copy
2 2
1 2
1 1
1
output
Draw
Note

In the first example the graph is the following:

Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins.

In the second example the graph is the following:

Initially the chip is located at vertex 2. The only possible Petya's move is to go to vertex 1. After that he has to go to 3 for Vasya. Now it's Petya's turn but he has no possible move, so Petya loses.

In the third example the graph is the following:

Petya can't win, but he can move along the cycle, so the players will draw a tie.


二 分析

题意:甲乙两人在简单有向图(最多10^5个顶点,2*10^5条边)上玩一个游戏,开始时指定一个顶点作为起点,每一轮一个玩家从当前顶点出发,沿着一条边达到一个新的顶点,轮流进行,一个玩家无路可走时就算失败,进行10^6轮后仍未分胜负算平。你先手,并且可以指定后手玩家的操作,问是否可以必胜,如果不行,是否可以逼和。


分析:和局的充要条件是显然的:图中有从起点可达的环。获胜条件其实是找到一条长度为奇数的路径,终点是一个出度为0的点。把原图中每个点S拆分为S0和S1,如果原图中有一条边(S, T),则连接(S0, T1)和(S1, T0)。这样我们只要从起点S1做一次DFS,如果能找到出度为0的点T0,就找到了一条必胜路径。


三 代码

之后都按照这个代码风格来了 函数名首字母大写 变量全部小写 控制语句关键字(if/for/while)后加空格

/*
AUTHOR: maxkibble
LANG: c++ 17
PROB: cf 936B
*/

#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

#define pb push_back

const int maxn = 1e5 + 10;

int n, m, d[maxn];
vector<int> g1[maxn], g2[maxn << 1], trace;
short vis[maxn << 1];

void Init() {
    scanf("%d%d", &n, &m);
    int c, t;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c);
        while(c--) {
            scanf("%d", &t);
            g1[i].pb(t);
            d[t]++;
        }
    }
}

void BuildGraph() {
    for (int i = 1; i <= n; i++) {
        for (int t: g1[i]) {
            g2[i * 2 - 1].pb(t * 2);
            g2[i * 2].pb(t * 2 - 1);
        }
    }
}

bool Dfs1(int x) {
    if (vis[x]) return false;
    vis[x] = 1;
    trace.pb(x);
    if (x % 2 == 1 && g2[x].size() == 0) return true;
    for (int y: g2[x]) {
        if (Dfs1(y)) return true;
    }
    trace.pop_back();
    return false;
}

bool Dfs2(int x) {
    if (vis[x] == 2) return true;
    vis[x] = 2;
    for (int y: g1[x]) {
        if (Dfs2(y)) return true;
    }
    vis[x] = 1;
    return false;
}

int main() {
    Init();
    BuildGraph();
    
    int pos;
    scanf("%d", &pos);
    
    if (Dfs1(pos * 2)) {
        puts("Win");
        for(int item: trace) printf("%d ", (item + 1) >> 1);
        return 0;
    }
    
    if (Dfs2(pos)) puts("Draw");
    else puts("Lose");

    return 0;
}


Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [30 lines of output] C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\util.py:75: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. import pkg_resources Error parsing Traceback (most recent call last): File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\core.py", line 105, in pbr attrs = util.cfg_to_args(path, dist.script_args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\util.py", line 272, in cfg_to_args pbr.hooks.setup_hook(config) File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\hooks\__init__.py", line 25, in setup_hook metadata_config.run() File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\hooks\base.py", line 27, in run self.hook() File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\hooks\metadata.py", line 25, in hook self.config['version'] = packaging.get_version( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\packaging.py", line 854, in get_version version = _get_version_from_pkg_metadata(package_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\luohb\AppData\Local\Temp\pip-build-env-r_dgqpgq\normal\Lib\site-packages\pbr\packaging.py", line 822, in _get_version_from_pkg_metadata pkg_metadata = email.message_from_file(pkg_metadata_file) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\tools\Python312\Lib\email\__init__.py", line 53, in message_from_file return Parser(*args, **kws).parse(fp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\tools\Python312\Lib\email\parser.py", line 52, in parse while data := fp.read(8192): ^^^^^^^^^^^^^ UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 795: illegal multibyte sequence error in setup command: Error parsing C:\Users\luohb\AppData\Local\Temp\pip-install-96xe5t7l\weditor_286f525530204299b904115d936b3afb\setup.cfg: UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 795: illegal multibyte sequence [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. [notice] A new release of pip is available: 24.0 -> 25.1.1 [notice] To update, run: python.exe -m pip install --upgrade pip C:\Users\luohb>
最新发布
07-08
从错误信息来看,这是在安装 Python 包时遇到的一个 `UnicodeDecodeError` 错误: ``` UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 795 ``` ### ❌ 问题原因分析: 这个错误通常发生在 Windows 系统上,特别是在使用默认的中文编码(`gbk`)读取文件时,而文件实际使用的编码是 UTF-8 或其他格式。在这个案例中,`setup.cfg` 文件里包含了一些非 `gbk` 编码的字符(比如版权符号、特殊空格等),Python 使用默认的 `gbk` 解码失败。 具体来说,下面这行代码报错: ```python pkg_metadata = email.message_from_file(pkg_metadata_file) ``` 它试图用默认的 `gbk` 编码打开一个非 `gbk` 编码的文件,导致解码失败。 --- ### ✅ 解决方案: #### 方法一:修改 pip 默认编码为 UTF-8(临时解决) 你可以在命令行中设置环境变量 `PYTHONIOENCODING=utf-8` 来强制 pip 使用 UTF-8 编码处理输入输出。 ```bash set PYTHONIOENCODING=utf-8 pip install your-package-name ``` 如果你使用的是 PowerShell: ```powershell $env:PYTHONIOENCODING = "utf-8" pip install your-package-name ``` --- #### 方法二:手动修改 setup.py 或 setup.cfg 文件(永久解决) 如果你可以下载源码包并修改内容,可以尝试将 `setup.cfg` 文件另存为 UTF-8 编码,或者在 `setup.py` 中指定编码方式来读取文件。 例如,修改读取文件的方式如下: ```python with open('setup.cfg', encoding='utf-8') as f: # 处理文件内容 ``` --- #### 方法三:升级或降级 setuptools 和 pbr 该错误也可能是由于某些依赖库版本不兼容引起的,特别是 `pbr` 这个包对编码支持不好。你可以尝试升级/降级相关工具: ```bash pip install --upgrade setuptools wheel pip pip install pbr==5.10.0 # 固定一个稳定版本 ``` --- #### 方法四:使用虚拟环境隔离依赖 创建一个新的干净虚拟环境,避免旧版本依赖干扰: ```bash python -m venv venv venv\Scripts\activate pip install --upgrade pip pip install your-package-name ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值