[codeforces917B]MADMAX

time limit per test : 1 second
memory limit per test : 256 megabytes

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 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 v to vertex in in if there’s an outgoing edge from v v to in in ). If the player moves his/her marble from vertex v v to vertex in in , the “character” of that round is the character written on the edge from v v to in in . There’s one additional rule; the ASCII code of character of round i i should be greater than or equal to the ASCII code of character of round i 1 i - 1 (for i > 1 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 n and m ( 2 n 100 , 1 m n ( n 1 ) / 2 ) m (2 ≤ n ≤ 100, 1≤m≤n*(n-1)/2 ) .

The next m m lines contain the edges. Each line contains two integers v v , in in and a a lowercase English letter c c , meaning there’s an edge from v v to in in written c c on it ( 1 v , in n , v (1 ≤ v, u ≤ n, v in )  in) . 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 n in each one. The j j -th character in i i -th line should be ‘A’ if Max will win the game in case her marble is initially at vertex i i and Lucas’s marble is initially at vertex j 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

The Graph apos in The Here First Sample Test Case:
Here Insert Picture Description
Here apos Graph in The Case The SECOND Test Sample:
Here Insert Picture Description
meaning of the questions:
Given a directed acyclic graph, each edge has a weight c, max and lucas have a piece, taking turns, max go, go after lucas, in addition to the first action, after each side of affirmative action should be greater than the value of the edge weights equal to the last pass, if people turn to when a man walking unable to move, the person fails. Required to arrive at a n n n*n matrix a n s years , a n s [ i ] [ j ] years [i] [j] as a starting point for the max i i starting point, lucasd for j j time who can triumph (A max represents victory, or that lucas victory)

Solution:
memory search.
Enumeration starting point, the normal process can be simulated game.
f [ i ] [ j ] [ k ] f[i][j][k] represents the current position is FLAC i i , the current position is the upper hand j j , the last time through the weight of the side action is k k
and then see if you can reach the upper hand losing state, if not the current state is losing the upper hand, otherwise the upper hand to win.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int mp[104][104];
short f[104][104][27],ans[104][104];
int n,m;
short dfs(int l,int m,int p){
    if(f[l][m][p])return f[l][m][p];
    f[l][m][p]=2;
    for(int i=1;i<=n;i++){
        if(mp[m][i]!=-1){
            int v=mp[m][i];
            if(p==26||v>=p){
                int g=dfs(i,l,v);
                if(g==2){
                    f[l][m][p]=1;
                    break;
                }
            }
        }
    }
    return f[l][m][p];
}
int main(){
    memset(mp,-1,sizeof(mp));
    memset(f,0,sizeof(f));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;char ch[4];
        scanf("%d%d%s",&u,&v,ch+1);
        mp[u][v]=ch[1]-'a';
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(ans[i][j]==0){
                ans[i][j]=dfs(i,j,26);
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            printf("%c",'A'+ans[j][i]-1);
        }
        puts("");
    }
	return 0;
}

Guess you like

Origin blog.csdn.net/dxyinme/article/details/95309370