time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.

Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.

Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?

You have to determine the winner of the game for all initial positions of the marbles.

Input

The first line of input contains two integers n and m (2 ≤ n ≤ 100, ).

The next m lines contain the edges. Each line contains two integers vu and a lowercase English letter c, meaning there's an edge from vto u written c on it (1 ≤ v, u ≤ nv ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.

Output

Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.

Examples
input
4 4
1 2 b
1 3 a
2 4 c
3 4 b
output
BAAA
ABAA
BBBA
BBBB
input
5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
output
BABBB
BBBBB
AABBB
AAABA
AAAAB
题意:给出一个有向无环图,每条边的边权都是小写字母,两个人分别从i点和j点出发,每个人能走的边边权必须大于等于上一个人走的边权,如果一个人无路可走,他就输了。
打印i=1~n,j=1~n的胜负表。 题解:GG我又双叒叕被题意杀了,写了个bfs结果呵呵
第一次接触到博弈dp
dp[i][j][v]表示先手在i,后手在j,上一条边走的边权为v的胜负情况
0表示后手必胜,1表示先手必胜
开始考虑转移
假设u点是与i点相连,边权为c
则只要有满足状态dp[j][u][c]=0的v存在,那么dp[i][j][v]=1(对手必败,则我们必胜)否则dp[i][j][v]=0
然后记忆化搜索一下,就可以了 代码如下:
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; vector< pair<int,int> > g[];
int n,m;
int f[][][]; int dfs(int a,int b,int v)
{
if(f[a][b][v]!=-)
{
return f[a][b][v];
}
f[a][b][v]=;
for(int i=;i<g[a].size();i++)
{
int y=g[a][i].first;
int w=g[a][i].second;
if(w>=v)
{
if(!dfs(b,y,w))
{
f[a][b][v]=;
break;
}
}
}
return f[a][b][v];
} int main()
{
memset(f,-,sizeof(f));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
char c;
int from,to;
scanf("\n%d %d %c",&from,&to,&c);
g[from].push_back(make_pair(to,c-'a'));
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(dfs(i,j,))
{
printf("A");
}
else
{
printf("B");
}
}
printf("\n");
}
}


CodeForces 918D MADMAX(博弈+记忆化搜索)的更多相关文章

  1. Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)

    D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...

  2. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  3. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  4. CodeForces 132C Logo Turtle (记忆化搜索)

    Description A lot of people associate Logo programming language with turtle graphics. In this case t ...

  5. BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)

    转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...

  6. Codeforces 667C Reberland Linguistics 记忆化搜索

    链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...

  7. UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)

    题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...

  8. HDU 4111 Alice and Bob (博弈+记忆化搜索)

    题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...

  9. Codeforces #564div2 E1(记忆化搜索)

    虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...

随机推荐

  1. 巨人大哥谈Java中的Synchronized关键字用法

    巨人大哥谈Java中的Synchronized关键字用法 认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方价格synchronized基本上就 ...

  2. Alpha冲刺No.10

    一.站立式会议 我们的阿尔法冲刺也基本宣告血崩,虽然很多功能已经实现,但是并没有串联在一起,好在这周不需要上课,我们也能好好睡一觉 实现手机的定位系统 细化界面设计 解决数据库和软件交互的一些问题 二 ...

  3. Beta冲刺NO.3

    Beta冲刺 第三天 1. 昨天的困难 1.昨天的困难主要集中在对Ajax的使用上,不熟悉这种语法,所以也就浪费了时间,导致昨天的批量删除没有完全完成. 2.由于之前的网页构造style很乱,导致修改 ...

  4. 201621123040《Java程序设计》第4周学习总结

    1.本周学习总结 1.1写出你认为本周学习中比较重要的知识点关键词 关键词:继承 多态性 基本语法 重新定义Override 1.2尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多 ...

  5. C语言博客作业--字符数组-陈张鑫

    一.PTA实验作业(4分) 题目1:7-5 查验身份证 1. 本题PTA提交列表(要提交列表,不是结果) 2. 设计思路(伪代码或流程图) 定义变量身份证个数n,合法个数count=0,flag=0, ...

  6. 四则运算----C++版

    一.设计思想 因java中已做过,就是简单的将java中的语句调换为C++的语句. 二.代码 #include<iostream.h> #include<Stdlib.h> v ...

  7. Linux下进程间通信--共享内存:最快的进程间通信方式

    共享内存: 一.概念: 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间. 进程A可以即时看到进程B ...

  8. 第二次作业:APP案例分析

    App案例分析 产品:三国杀-页游手游双通 选择理由 当今社会手机已经渐渐取代了电脑在人们日常生活的需求,既然要选择APP进行案例分析,首推的估计就是手机APP了.三国杀是陪伴我高中时代的主要娱乐方式 ...

  9. scrapy 数据存储mysql

    #spider.pyfrom scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Ru ...

  10. idea搭建springdata+mongodb+maven+springmvc

    idea搭建springdata+mongodb+maven+springmvc 今天我们来学习一下SpringData操作MongoDB. 项目环境:IntelliJ IDEA2017+maven3 ...