算法学习笔记(74): 二分图博弈

二分图博弈是一类博弈模型,它可以被抽象为:给出一张二分图起始点 H ,A和B轮流操作,每次只能选与上个被选择的点(第一回合则是点 H相邻的点,且不能选择已选择过的点,无法选点的人输掉。一个经典的二分图博弈模型是在国际象棋棋盘上,双方轮流移动一个士兵,不能走已经走过的格子,问谁先无路可走。

这类模型其实很好处理。考虑二分图的最大匹配,如果最大匹配一定包含 H ,那么先手必胜,否则先手必败。

如果最大匹配一定包含 H,那么先手只需要一直按照匹配选点即可。后手不可能选到非匹配点,如果后手选到一个非匹配点,设路径为 H→P_1→\cdots→P_{n-1}→P_n ,那么把现在的匹配 \{H-P_1,\dots,P_{n-2}-P_{n-1}\} 换成\{P_1-P_2,\dots,P_{n-1}-P_{n}\},匹配数不变但不包含 H ,与最大匹配一定包含 H 矛盾。

如果最大匹配不一定包含 H ,考虑某个不包含 H 的最大匹配 \mathcal{M} 。先手无论选择哪个点,它都一定是匹配点,否则设这个点为 P ,则发现了新匹配 H-P ,与 \mathcal{M} 是最大匹配矛盾。之后后手一直按照匹配选点即可,先手不可能选到非匹配点,否则设路径为H→P_1→\cdots→P_{n-1}→P_n ,把现在的匹配 \{P_1-P_2,\dots,P_{n-2}-P_{n-1}\} 换成 \{H-P_1,\dots,P_{n-1}-P_{n}\} ,明显匹配数变多了,与 \mathcal{M} 是最大匹配矛盾。

如何确定最大匹配是否一定包含 H 呢?我们可以对删除和不删除 H 的情形分别做二分图最大匹配,如果删除两个匹配数一样多,说明 H 是可有可无的,存在不包含 H 的最大匹配。否则,说明 H 是不可或缺的。

具体实现可以根据数据范围选用匈牙利算法Dinic。需要注意的是,如果采用Dinic,不要根据有没有 H 点建两次图。而是在建图时把涉及 H 点的边存下来,跑完第一次Dinic后再建这些边,第二次Dinic看有没有增加流量。

2020CCPC长春H Combination Lock

Once there was a combination lock, which consisted of m rings with digits from 0 to 9 . It was broken so one could never open the lock no matter how he spun it. Alice and Bob wanted to play a game with it. They in turn spun one ring of the lock for one step in either direction, and tried to open the lock (although they knew the lock would never open). If the password on the lock had already been tried, the player would be considered lost. Additionally, they had discussed a set S of n passwords before the game started. The player would also lose if (s)he tried a password among S . Alice would move first.
A 5 -digit combination lockNow you can only recall m , S and the initial password on the lock, and you wonder who was the winner supposing Alice and Bob played optimally.
Input
Each test contains multiple test cases. The first line contains the number of test cases T (1≤T≤10) . Description of the test cases follows.
The first line contains two integers m and n ( 1≤m≤5 , 0≤n<10^m ), and a string t ( |t|=m ) composed of digits from 0 to 9 , representing the initial password on the lock.
The i -th of the next n lines contains a string u_i ( |u_i|=m ) composed of digits from 0 to 9 , representing a password in S . It is guaranteed that all u_i are distinct, and not the same as t .
Output
For each test case, print on a line Alice or Bob, the winner of the game.

这道题就是明显的二分图匹配,每个状态根据数字和的奇偶性明显形成一张二分图,而又不能达到曾达到的状态。按上面说的方法跑dinic即可。


发布于 2021-03-24 09:26