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:

Here's the graph in the second sampletest case:


【题意】


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


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

【思路】


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


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


具体细节见代码。

#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
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("");
    }
}




 

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值