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. 粗看ES6之函数

    标签: es6 javascript 箭头函数 ES6为了书写方便引入了函数的全新简写方式-箭头函数 <!DOCTYPE html> <html> <head> & ...

  2. vue.js ------ 大牛和网站

    hellogirl前端网站 : http://www.jqhtml.com/category/article FungLeo: http://blog.csdn.net/FungLeo/article ...

  3. Static 用法

    1.Static关键字含意:static译文是静态的,静止的,因此使用 static 修饰符声明属于类型本身而不是属于特定对象(new创建的对象)的静态成员. 2.修饰使用范围 static 修饰符可 ...

  4. mysql-5.7安装配置指导

    mysql 安装 yum 安装mysql 源码编译安装mysql 通过yum安装 下载yum仓库配置安装包 MySQL Yum Repository http://dev.mysql.com/down ...

  5. JDBC源码解析

    参考:https://blog.csdn.net/silviakafka/article/details/46225045

  6. Python输入与循环

    python while循环 while 语句: 执行语句 结束条件 #应用while输出1到11 counts = 1 while True: print("counts:", ...

  7. ring0 进程隐藏实现

    最近在学习内核编程,记录一下最近的学习笔记. 原理:将当前进程从eprocess结构的链表中删除 无法被! process 0 0 看见 #include "HideProcess.h&qu ...

  8. April 30 2017 Week 18 Sunday

    Our lives stretched out ahead of us, like a perpetual sunrise. 生命如永恒的日出,生生不息. Please respect yoursel ...

  9. leetcode:回溯——permutation-sequence,

    1. permutation-sequence 顺序排列第k个序列 The set[1,2,3,…,n]contains a total of n! unique permutations. By l ...

  10. chromedp自动启动为headless模式

    // Command click is a chromedp example demonstrating how to use a selector to // click on an element ...