Codeforces 918D - MADMAX 【记忆化搜索+博弈】


D. MADMAX

time limit per test  1second

memory limit per test      256megabytes


As we all know, Max is the best videogame player among her friends. Her friends were so jealous of hers, that theycreated an actual game just to prove that she's not the best at games. The gameis played on a directed acyclic graph (a DAG) with n vertices and m edges. There'sa character written on each edge, a lowercase English letter.

Max and Lucas are playing the game. Maxgoes 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 movehis/her marble along some edge (a player can move the marble from vertex v to vertex u if there's anoutgoing edge from v to u). If the playermoves 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 oneadditional rule; the ASCII code of character of round i should be greater than orequal to the ASCIIcode of character of round i - 1 (for i > 1). The roundsare 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 andLucas and Max have to focus on finding Dart, they don't have time to play. Sothey asked you, if they both play optimally, who wins the game?

You have to determine the winner of thegame for all initial positions of the marbles.

Input

The first line of input contains twointegers n and m (2 ≤ n ≤ 100, http://codeforces.com/predownloaded/bd/80/bd8007444f2883fa93836e448269d9092c4fdad4.png).

The next m lines containthe edges. Each line contains two integers v, u and a lowercaseEnglish letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's atmost one edge between any pair of vertices. It is guaranteed that the graph isacyclic.

Output

Print n lines, a string of length n in each one.The j-th character in i-th line shouldbe 'A' if Max will win the game in case her marble is initially at vertex i and Lucas'smarble is initially at vertex j, and 'B' otherwise.

Examples

Input

Copy

4 4
1 2 b
1 3 a
2 4 c
3 4 b

Output

BAAA
ABAA
BBBA
BBBB

Input

Copy

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 sampletest case:

Codeforces 918D - MADMAX 【记忆化搜索+博弈】_第1张图片

Here's the graph in the second sampletest case:

Codeforces 918D - MADMAX 【记忆化搜索+博弈】_第2张图片


【题意】


有一个n个节点m条边的DAG图,每条边上有一个小写字母作为权值,A与B玩一个游戏,一开始两人分别在i,j节点上,然后两个人轮流移动,每次只能经过一条边,且每次走过边的权值要大于等于上一个人的,不能移动算输


如果两人都play perfect,且A先手,输出对于所有的(i,j)谁将获胜。

【思路】


用dp[i][j][pre]表示当前轮到的人在i,另一人在j,且上一轮经过边的权值为pre的情况下的输赢状态,为1的话说明处于必胜态,否则为必败态。


为了时间优化采用记忆化搜索。


具体细节见代码。

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 105;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;

int n,m;
char w[maxn][maxn];
int dp[maxn][maxn][30];

int dfs(int u,int v,int pre)
{
    if(dp[u][v][pre]) return dp[u][v][pre];
    for(int i=1; i<=n; i++)
    {
        if(i==u) continue;
        if(w[u][i]-'a'>=pre&&dfs(v,i,w[u][i]-'a')==2)  //当前的人可以往i走,且走完后下一个人处于必败态,说明当前的人必胜
            return dp[u][v][w[u][i]-'a']=1;
    }
    return dp[u][v][pre]=2;   //没有方案使当前的人处于必胜态,即为必败态
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int u,v;
        char s[5];
        scanf("%d%d%s",&u,&v,s);
        w[u][v]=s[0];
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(dfs(i,j,0)==1) printf("A");
            else printf("B");
        }
        puts("");
    }
}




 

你可能感兴趣的:(数位DP&记忆化搜索,博弈,918D,-,MADMAX,Codeforces,Round#459)