codeforces 917B. MADMAX

B. 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.

codeforces 917B. MADMAX_第1张图片

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 v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ 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:

codeforces 917B. MADMAX_第2张图片

Here's the graph in the second sample test case:

codeforces 917B. MADMAX_第3张图片
题意:
给你一个有向图,每条边的边权都是一个字母的ASCLL码值,有两个人从起点i和起点j走起,每次所走的边权必须大于等于上一个人所走的边权,第一个无法走的人失败另一个人获胜,输出所有起点i和j的获胜情况。

思路:
dp[i][j][k]表示先手在i,后手在j,上次走的边权为k。
则对于i所能到达的点v,可由dp[j][v][nk]表示,nk表示i->v的边权值。
对于所有可以到的的点v,只要存在至少一个v,满足dp[j][v][nk]必败,则表明dp[i][j][k]为必胜态。否则dp[i][j][k]为必败态。
dp[i][j][k]为0表示先手必败,为1表示先手必胜。
#include
#include
#include
#include
#include
using namespace std;
const int maxm = 105;
int dp[maxm][maxm][maxm], cost[maxm][maxm];
vectorp[maxm];
int dfs(int x,int y,int now)
{
	if (dp[x][y][now] >= 0) return dp[x][y][now];
	for (int i = 0;i < p[x].size();i++)
	{
		int v = p[x][i];
		if (cost[x][v] >= now&&dfs(y, v, cost[x][v]) == 0)
			return dp[x][y][now] = 1;
	}
	return dp[x][y][now] = 0;
}
int main()
{
	int n, i, j, k, sum, m, u, v;
	char ch;
	scanf("%d%d", &n, &m);
	memset(dp, -1, sizeof(dp));
	for (i = 1;i <= m;i++)
	{
		scanf("%d%d %c", &u, &v, &ch);
		p[u].push_back(v);
		cost[u][v] = ch - 'a';
	} 
	for (i = 1;i <= n;i++)
		for (j = 1;j <= n;j++)
			dfs(i, j, 0);
	for (i = 1;i <= n;i++)
	{
		for (j = 1;j <= n;j++)
		{
			if (dp[i][j][0] == 0) printf("B");
			else printf("A");
		}
		printf("\n");
	}
	return 0;
	return 0;
}





你可能感兴趣的:(DP,codeforces)