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. 开源的SSH框架优缺点分析

    开源是3个框架共有的优点 Struts2框架(MVC框架)的优点如下: 1) 实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现: 2) 丰富的标签库,大大提高了开发的效率: 3) Str ...

  2. 使用Quartz任务调用的时候报错Based on configured schedule, the given trigger will never fire.

    org.quartz.SchedulerException: Based on configured schedule, the given trigger will never fire. 大概意思 ...

  3. wcf问题集锦

    1.处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供 ...

  4. Java并发基础:线程的创建

    线程的创建和管理: 1.应用Thread类显式创建.管理线程 2.应用Executor创建并管理线程. 定义任务: 无返回的任务:实现Runnable接口并编写run()方法. 有响应的任务:实现Ca ...

  5. GreenDao 使用知识小y

    //关于 group by 的实现//--------------------XXXDao.queryBuilder().where(new WhereCondition.StringConditio ...

  6. TFS无法确定工作区解决方案

    当TFS遇到无法确定工作区时,删除掉SUO和VSSSCC文件,当然也有可能不是这两个,反正是SLN文件以外的两个文件,毕竟是昨天做的事,有点忘记了.所以,做掉文件之前,要做好备份O(∩_∩)O~

  7. SQLSERVER编译与重编译

    SQLSERVER编译与重编译 编译的含义 当SQLSERVER收到任何一个指令,包括查询(query).批处理(batch).存储过程.触发器(trigger) .预编译指令(prepared st ...

  8. html5标准

    1.<!DOCTYPE html> html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5 浅谈:htm ...

  9. 如果CDN服务器出了问题,怎么做不影响自己的网站

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3.jquery.min.js"></ ...

  10. BZOJ 1229: [USACO2008 Nov]toy 玩具

    BZOJ 1229: [USACO2008 Nov]toy 玩具 标签(空格分隔): OI-BZOJ OI-三分 OI-双端队列 OI-贪心 Time Limit: 10 Sec Memory Lim ...