Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m arcs, loops are allowed. Gennady and Georgiy have a token placed in one of the graph vertices. Players take turns moving the token along one of the arcs that starts in the vertex the token is currently in. When there is no such arc, then this player loses the game. For each initial position of the token and the player who is moving first, your task is to determine what kind of result the game is going to have. Does it seem to be easy? Not so much. On one side, Gennady is having a lot of fun playing this game, so he wants to play as long as possible. He even prefers a strategy that leads to infinite game to a strategy that makes him a winner. But if he cannot make the game infinite, then he obviously prefers winning to losing. On the other side, Georgiy has a lot of other work, so he does not want to play the game infinitely. Georgiy wants to win the game, but if he cannot win, then he prefers losing game to making it infinite. Both players are playing optimally. Both players know preferences of the other player.
Input
In the first line there are two integers — the number of vertices n (1 ≤ n ≤ 100000) and the number of arcs m (1 ≤ m ≤ 200000). In the next m lines there are two integers a and b on each line, denoting an arc from vertex a to vertex b. Vertices are numbered from 1 to n. Each (a,b) tuple appears at most once.
Output
In the first line print n characters — i-th character should denote the result of the game if Gennady starts in vertex i. In the second line print n characters — i-th character should denote the result of the game if Georgiy starts in vertex i. The result of the game is denoted by “W” if the starting player wins the game, “L” if the starting player loses the game, and “D” (draw) if the game runs infinitely.

题意: 给定有向图,选定起点,每次可以沿着有向图走到下一个点,没有点走的人输。这样很常规的SG图就可以搞定,但是现在加入新规定,A选手如果可以走无限步也算赢。而B选手宁愿输也不愿意走无限步。 现在对于每个点作为起点,输出A做为先手的结果,以及B选手作为先手的结果。

思路: 有向图,而且有环,不能简单的拓扑 :首先我们想一下SG图的概念,如果后续节点有一个为输,那么当前结点就为赢;如果后续节点都为赢,那么当前节点就为输。

对于平局的情况,我们去找A可以平的点,以及B必平的点:

默认每个点都会平,把入度为0的点加入队列,状态为不平,然后从跑队列。对于当前节点,如果是A,当后缀节点有一个的平是,则其是平;如果是B,后缀节点都跑完了,而且都是平,则其是平。其他的都是不平。

对于输赢的情况,我们去找A必输和B可以输的点:

默认每个点都不知道,把入度为0的点加入队列,状态为输,然后跑队列。对于A,如果上面得出它可以平,则平。否则,如果后缀节点有输,则赢。否则输; 对于B,如果上面得出它必须平,则平。否则后缀节点有输,则赢。否则,输。

(666

#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define A 0
#define B 1
#define fi first
#define se second
#define Lose 0
#define Win 1
using namespace std;
const int maxn=;
int draw[maxn][],vict[maxn][],c[maxn][];
int outd[maxn],tag[maxn],head,tail;
pii q[maxn];
vector<int>G[maxn];
int main()
{
int N,M,u,v,i,j;
scanf("%d%d",&N,&M);
for(i=;i<=M;i++){
scanf("%d%d",&u,&v);
G[v].push_back(u);
outd[u]++;
}
for(i=;i<=N;i++){
draw[i][A]=draw[i][B]=;//默认会平
tag[i]=outd[i];
if(!outd[i]){
q[++head]=mp(i,A); q[++head]=mp(i,B);
draw[i][A]=; draw[i][B]=;//平不了。
}
}
while(tail<head){
pii now=q[++tail];
for(i=;i<G[now.fi].size();i++){
if((now.se==A&&draw[G[now.fi][i]][B])||(now.se==B&&!(--outd[G[now.fi][i]]))){
draw[G[now.fi][i]][now.se^]=;
q[++head]=mp(G[now.fi][i],now.se^);
}
}
}
//平局算完,来算victory。
head=tail=;
for(i=;i<=N;i++){
outd[i]=tag[i];
vict[i][A]=-; vict[i][B]=-; //默认不知道输赢 ,去找必输必赢的点
}
for(i=;i<=N;i++){
for(j=;j<G[i].size();j++){
if(!draw[i][B]) c[G[i][j]][A]++;
if(!draw[i][A]) c[G[i][j]][B]++; //如果非平的点后缀全是赢,则输
}
if(!outd[i]){
vict[i][A]=Lose,q[++head]=mp(i,A);
vict[i][B]=Lose,q[++head]=mp(i,B);
}
}
while(tail<head){
pii now=q[++tail];
for(i=;i<G[now.fi].size();i++){
if(draw[G[now.fi][i]][-now.se]) continue;
if(vict[now.fi][now.se]==Win){
if(!(--c[G[now.fi][i]][-now.se])){
vict[G[now.fi][i]][-now.se]=;
q[++head]=mp(G[now.fi][i],-now.se);
}
}
else if(vict[G[now.fi][i]][-now.se]==-){
vict[G[now.fi][i]][-now.se]=;
q[++head]=mp(G[now.fi][i],-now.se);
}
}
}
for(i=;i<=N;i++) {
if(vict[i][A]==-) vict[i][A]=;
if(vict[i][B]==-) vict[i][B]=;
}
for(j=;j<;j++,puts(""))
for(i=;i<=N;i++) {
if(draw[i][j]) putchar('D');
else if(vict[i][j]) putchar('W');
else putchar('L');
}
return ;
}

Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)的更多相关文章

  1. Codeforces Gym 101190 NEERC 16 .D Delight for a Cat (上下界的费用流)

    ls是一个特别堕落的小朋友,对于n个连续的小时,他将要么睡觉要么打隔膜,一个小时内他不能既睡觉也打隔膜 ,因此一个小时内他只能选择睡觉或者打隔膜,当然他也必须选择睡觉或打隔膜,对于每一个小时,他选择睡 ...

  2. Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)

    ls特别喜欢素数,他总是喜欢把素数集合的所有子集写下来,并按照一定的顺序和格式.对于每一个子集,集合内 的元素在写下来时是按照升序排序的,对于若干个集合,则以集合元素之和作为第一关键字,集合的字典序作 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  6. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

  7. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  8. Codeforces Round #547 (Div. 3) G 贪心

    https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...

  9. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

随机推荐

  1. VC进程间通信之消息传递PostMessge()或SendMessage()

    1.  进程内消息: (1). 仅仅传消息码 (2). 传送消息串 发送端: void CTestDlg::OnBnClickedButtonSend() { CString* msg = new C ...

  2. java 内存与内存溢出

    学习自:http://www.codeceo.com/article/jvm-memory-overflow.html 讲的很清楚

  3. Java 加载器

    类的加载是由类加载器完成的,类加载器包括: 根加载器( BootStrap ).扩展加载器( Extension ).系统加载器( System )和用户自定义类加载器( java.lang.Clas ...

  4. cartographer Ubuntu16.04 ros环境配置

    首先要正确安装 ROS ,然后第12步应注意,proto的版本是个关键容易出错.   1.添加ROS源http:/packages.ros.org/ros/ubuntu xenial main   ( ...

  5. Enumerate Combination C(k, n) in a bitset

    Suppose n<=32, we can enumerate C(k, n), with bits representing absence or presence, in the follo ...

  6. 也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强 (续)

    在上一篇文章也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强中,我提到了隐式数据类型转换添加对于数据分布非常不平均的表.评估的数据行数与实际值有非常大出入的问题,进一步測试之 ...

  7. PHP面向对象——构造函数、析构函数

    在PHP中构造函数和析构函数是固定的,如下: // 构造函数 function __construct([argument1,argument2,...,argumentN]){ /* Class i ...

  8. Docker入门系列4:命令行小结

    百度Baidu App Engine(BAE)平台也是以Docker作为其PaaS云基础. 目的就是为了解决以下问题: 1) 环境管理复杂: 从各种OS到各种中间件再到各种App,一款产品能够成功发布 ...

  9. CPI

    CPI (Consumer Price Index 物价指数) 是政府用来衡量通货膨胀的其中一个数据.通俗的讲,CPI就是市场上的货物价格增长百分比.一般市场经济国家认为CPI在2-3%属于可接受范围 ...

  10. c 字符串 函数

    c编辑 strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NUL结 ...