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

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

Here’s the graph in the first sample test case:
在这里插入图片描述
Here’s the graph in the second sample test case:
在这里插入图片描述
题意:
给定一张有向无环图,每条边有一个权值c,max和lucas各有一个棋子,轮流行动,max先走,lucas后走,除了第一次行动外,每次行动经过的边的权值都要大于等于上一次经过的边的权值,如果轮到一个人走的时候这个人无法行动,则此人失败。要求得出一个 n ∗ n n*n nn的矩阵 a n s ans ans a n s [ i ] [ j ] ans[i][j] ans[i][j]为max的起点为 i i i,lucas的起点为 j j j的时候谁能够胜利(A表示max胜利,否则表示lucas胜利)

题解:
记忆化搜索。
枚举起点,正常模拟博弈过程即可。
f [ i ] [ j ] [ k ] f[i][j][k] f[i][j][k] 表示当前后手的位置是 i i i,当前先手的位置是 j j j,上一次行动走过的边的权值是 k k k
然后看看是否能够到达先手必败态,不能的话当前状态就是先手必败的,否则是先手必胜的。

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值