D. MADMAX

time limit per test1 second

memory limit per test256 megabytes

Problem Description

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 v, u and a lowercase English letter c, meaning there’s an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ 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

Note

Here’s the graph in the first sample test case:

Here’s the graph in the second sample test case:


解题心得:

  1. 其实是一个简单的记忆化搜索加博弈,难点就是dp需要开一个四维的数组来表示状态,dp[x1][x2][x3][x4]表示A在x1的位置,B在x2的位置,当前行走的路权值至少是x3,该轮到x4走。每次代表一个状态,这也是博弈论相关的思想,不管该怎么走,只要知道在这个状态所代表的输赢关系就可以了。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
vector <pair<int,int> > ve[maxn];
int n,m;
int dp[maxn][maxn][maxn][3]; void init()
{
memset(dp,-1,sizeof(dp));
for(int i=0;i<m;i++)
{
int a,b;
char s[10];
scanf("%d%d",&a,&b);
scanf("%s",s);
int c = s[0] - 'a';
ve[a].emplace_back(b,c);
}
} int dfs(int x,int y,int va,int turn)
{
if(dp[x][y][va][turn] != -1)
return dp[x][y][va][turn];
if(turn == 0)//该A走
{
for(int i=0;i<ve[x].size();i++) {
pair<int, int> v = ve[x][i];
if (v.second >= va)
if (dfs(v.first, y, v.second, 1 - turn) == 0)
return dp[x][y][va][turn] = 0;
}
return dp[x][y][va][turn] = 1;
}
else
{
for(int i=0;i<ve[y].size();i++) {
pair<int,int> v = ve[y][i];
if(v.second >= va)
if(dfs(x,v.first,v.second,1 - turn) == 1)
return dp[x][y][va][turn] = 1;
}
return dp[x][y][va][turn] = 0;
}
} int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(dfs(i,j,-1,0))
printf("B");
else
printf("A");
}
printf("\n");
}
return 0;
}

Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)的更多相关文章

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

    题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...

  2. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  3. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  4. Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索

    https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...

  5. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  6. Codeforces Round #459 (Div. 2)

    A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  7. codeforces 793 D. Presents in Bankopolis(记忆化搜索)

    题目链接:http://codeforces.com/contest/793/problem/D 题意:给出n个点m条边选择k个点,要求k个点是联通的而且不成环,而且选的边不能包含选过的边不能包含以前 ...

  8. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  9. Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)

    E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...

随机推荐

  1. C#对象和集合初始值设定项

    对象初始值设定项 使用对象初始值设定项,你可以在创建对象时向对象的任何可访问字段或属性分配值,而无需调用后跟赋值语句行的构造函数. 利用对象初始值设定项语法,你可为构造函数指定参数或忽略参数(以及括号 ...

  2. 记录下laravel 5.2的auth/logout路由工作不正常的问题

  3. Linux软件相关记录

    Pidgin+lw-web的聊天记录的文件对应的目录为.purple/logs/webqq/你的QQ号码/,进入之后有选择的删除. mkdir -p 递归创建目录:pwd 显示当前目录:cd .. 回 ...

  4. 玩转spring ehcache 缓存框架

    一.简介 Ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache ...

  5. Every ending is just a new beginning.

    Every ending is just a new beginning.每次结束都是新的开始.

  6. linux nginx 404错误页面设置

    配置nginx 实现404错误 返回一个页面 1.配置nginx.conf 在http代码块 添加 fastcgi_intercept_errors on; 2.在网站的sever代码块 添加 err ...

  7. 【来龙去脉系列】QRCode二维码的生成细节和原理

    二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...

  8. 绿盟网站安全防护服务(vWAF)

    平台: linux 类型: 虚拟机镜像 软件包: basic software devops nsfocus security waf 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即部署 ...

  9. php使用GD库实现图片水印和缩略图——给图片添加文字水印

    今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...

  10. 【强力卸载】使用Uninstall Tool卸载各类不易卸载的软件

    Uninstall Tool 经测试卸载MySql5.7.18成功. 下载地址: http://files.cnblogs.com/files/xiaohi/%E3%80%90%E8%BD%AF%E4 ...