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. 【Django】 视图层说明

    [Django视图层] 视图层的主要工作是衔接HTTP请求,Python程序和HTML模板,使他们能够有机互相合作从模型层lou到数据并且反馈.说到视图层的工作就有以下几个方面要说 ■ URL映射 对 ...

  2. 【Linux】 linux中的进程信息相关的一些内容

    _ linux进程信息 ■ top top命令用于动态地查看系统的进程和其他一些资源的信息.开启top的时候可以加上-t <sec>来设置top更新的频率高低.进入top界面之后,可以输入 ...

  3. mysql-5.7.12安装

    CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载   # wget http://dev.mysql.com/get/mysql-communit ...

  4. java程序性能调优---------------性能概述

    一.程序的性能通过哪几个方面表现 1.执行速度(程序反应反应是否迅速.响应时间是否足够短) 2.分配内存 (分配内存是否合理,是否过多的消耗内存或者内存溢出) 3.启动时间(程序从运行到可以正常处理业 ...

  5. [jdoj1090]矩阵_区间dp

    矩阵 jdoj-1910 题目大意:给你连续的n个矩阵的长和宽,保证每连续的两个相邻矩阵满足相乘的条件,不能改变题目中矩阵的位置,求将这些矩阵相乘为一个矩阵的最小乘法次数. 注释:1<=n< ...

  6. Idea  调试代码

    ---恢复内容开始--- set DEBUG_PORT=8787 set JAVA_DEBUG=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,addr ...

  7. PHP 密码重置,发送邮件,随机长度字母数字密码

    <?php include ("database.php"); require_once ('email.class.php'); date_default_timezone ...

  8. Oracle创建用户、角色、授权、建表

    oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...

  9. New UWP Community Toolkit - RadialProgressBar

    概述 UWP Community Toolkit  中有一个圆形的进度条控件 - RadialProgressBar,本篇我们结合代码详细讲解  RadialProgressBar 的实现. Radi ...

  10. C语言博客作业--函数嵌套调用

    一.实验作业(6分) 本周作业要求: 选一题PTA题目介绍. 学习工程文件应用,设计实现学生成绩管理系统. 学生成绩管理系统要求 设计一个菜单驱动的学生成绩管理程序,管理n个学生m门考试科目成绩,实现 ...