CF918D:MADMAX(博弈 & DFS)

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:

题意:一幅有向无环图,边权有小写字母,两人玩游戏,他们起点分别是i和j节点,每次可以沿出边方向移动一格,前提是该边的字母>=上一轮移动经过的边的字母,不能移动的就输,问所有i和j为起点结果谁能赢。

思路:枚举每个起点i和j,DFS判断必胜态和必败态即可。若下一轮都是必胜态,这轮就是必败态;若下一轮存在必败态,这轮就是必胜态。然而100个点每次暴力会T,记忆化搜索一下,dp[a][b][c][d]表示先手和后手分别在a和b,上一轮的字母是c,轮到d移动时d的胜负状态。一点优化就是若dp为1就可以break了。

# include <bits/stdc++.h>
# define mp make_pair
# define pb push_back
# define A first
# define B second
using namespace std;
typedef long long LL;
vector<pair<int,char> >g[103];
int dp[103][103][28][2];
char ans[103][103];
int dfs(int a, int b, int pre, int man)
{
    if(dp[a][b][pre][man] != -1) return dp[a][b][pre][man];
    dp[a][b][pre][man] = 0;
    if(man)
    {
        for(auto it:g[a])
        {
            if(it.B-'a'+1>=pre && !dfs(it.A, b, it.B-'a'+1, !man))
            {
                dp[a][b][pre][man]=1;
                break;
            }
        }
    }
    else
    {
        for(auto it:g[b])
        {
            if(it.B-'a'+1>=pre && !dfs(a, it.A, it.B-'a'+1, !man))
            {
                dp[a][b][pre][man]=1;
                break;
            }
        }
    }
    return dp[a][b][pre][man];
}
int main()
{
    memset(dp, -1, sizeof(dp));
    int n, m;
    char c;
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int a, b;
        scanf("%d %d %c",&a,&b,&c);
        g[a].pb(mp(b,c));
    }
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=n; ++j)
        {
            if(dfs(i, j, 0, 1)) ans[i][j] = 'A';
            else ans[i][j] = 'B';
        }
    }
    for(int i=1; i<=n; ++i,puts(""))
        for(int j=1; j<=n; ++j)
            printf("%c",ans[i][j]);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值