D. MADMAX
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
Note

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

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

题意:两个人在DAG上玩游戏,每个人可以沿着边上走,每条边上有一个字母,每个人只能走的边上的字母必须大于等于上一个人走的边的字母

求两个人起点所有情况的胜负情况。

思路:

设(bool) dp u v k 表示一个人在u 另一个人在v 上一条边是k的胜负值,则若u走到i节点,而dp v i edge(u->i)为0 则dp u v k为1,然后在图上转移。

代码:

 //#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int ans[][][];
int mp[][],n,m;
bool dfs(int u,int v,int x)
{
if(ans[u][v][x]) return ans[u][v][x];
for(int i=;i<=n;i++){
if(x>mp[u][i]) continue;
if(!dfs(v,i,mp[u][i])) return ans[u][v][x]=;//(v,i,mp[u][i]) lost =>(u,v,x) win!
}
return ans[u][v][x]=;//it can't win anyway
}
int main()
{
ci(n),ci(m);
memset(mp,-, sizeof(mp));
for(int i=;i<m;i++)
{
int x,y;
char s[];
ci(x),ci(y);
scanf("%s",s);
mp[x][y]=s[]-'a';
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(dfs(i,j,)==) printf("A");
else printf("B");
}
puts("");
}
return ;
}

Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈的更多相关文章

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

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

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

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

  3. 【Codeforces Round #459 (Div. 2) D】MADMAX

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] f[x][y][z][2] 表示第一个人到了点x,第二个人到了点y,当前轮的字母(1..26),当前轮到谁走的情况下,谁赢. 写个记 ...

  4. Codeforces Round #459 (Div. 2)

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

  5. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

  6. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  7. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

  8. Codeforces Round #321 (Div. 2)C(tree dfs)

    题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...

  9. Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 函数的返回值 return

    布尔类型返回 return 0:返回假: return 1:返回真:

  2. sass命令

    tip:sass报错解决 通过ruby编译scss时,发现编译报错,内容如下:   Conversion error: Jekyll::Converters::Scss encountered an ...

  3. python-gearman使用

    yum -y install gearmand chkconfig gearmand on && /etc/init.d/gearmand start # /etc/sysconfig ...

  4. 监控系统-mod-gearman

    doc http://labs.consol.de/nagios/mod-gearman/ 安装 yum -y install gearmand-server-0.33-2.rhel6.x86_64. ...

  5. centos 卸载 docker

    yum list installed | grep docker //查看安装过的包 docker-engine.x86_64                 17.03.0.ce-1.el7.cen ...

  6. Html : 将submit变成像文字一样的按钮

    直接上代码: <html> <head> <title>像文字一样的按钮</title> <style> body{ background- ...

  7. IOS Git源代码管理工具

    .新建一个“本地仓库” $ git init .配置仓库 >告诉git你是谁 git config user.name lnj >告诉git怎么联系你 git config user.em ...

  8. IOError: [Errno 22] invalid mode ('rb') or filename: 'C

    dataset = scipy.io.loadmat('F:\test_data.mat') 报错 IOError: [Errno ] invalid mode ('rb') or filename: ...

  9. python读取mat文件

    一.mat文件 mat数据格式是Matlab的数据存储的标准格式.在Matlab中主要使用load()函数导入一个mat文件,使用save()函数保存一个mat文件.对于文件 load('data.m ...

  10. P2082 区间覆盖(加强版)

    题目 #include<iostream> #include<algorithm> #include<cstring> using namespace std; s ...