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.
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 vto 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.
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.
4 4
1 2 b
1 3 a
2 4 c
3 4 b
BAAA
ABAA
BBBA
BBBB
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
BABBB
BBBBB
AABBB
AAABA
AAAAB
Here's the graph in the first sample test case:
Here's the graph in the second sample test case:
题意:两个人在DAG上玩游戏,每个人可以沿着边上走,每条边上有一个字母,每个人只能走的边上的字母必须大于等于上一个人走的边的字母
求两个人起点所有情况的胜负情况。
思路:
设(bool) dp u v k 表示一个人在u 另一个人在v 上一条边是k的胜负值,则若u走到i节点,而dp v i edge(u->i)为0 则dp u v k为1,然后在图上转移。
代码:
1 //#include "bits/stdc++.h" 2 #include "cstdio" 3 #include "map" 4 #include "set" 5 #include "cmath" 6 #include "queue" 7 #include "vector" 8 #include "string" 9 #include "cstring" 10 #include "time.h" 11 #include "iostream" 12 #include "stdlib.h" 13 #include "algorithm" 14 #define db double 15 #define ll long long 16 //#define vec vector<ll> 17 #define Mt vector<vec> 18 #define ci(x) scanf("%d",&x) 19 #define cd(x) scanf("%lf",&x) 20 #define cl(x) scanf("%lld",&x) 21 #define pi(x) printf("%d ",x) 22 #define pd(x) printf("%f ",x) 23 #define pl(x) printf("%lld ",x) 24 #define rep(i, x, y) for(int i=x;i<=y;i++) 25 const int N = 1e6 + 5; 26 const int mod = 1e9 + 7; 27 const int MOD = mod - 1; 28 const db eps = 1e-10; 29 const db PI = acos(-1.0); 30 using namespace std; 31 int ans[105][105][30]; 32 int mp[105][105],n,m; 33 bool dfs(int u,int v,int x) 34 { 35 if(ans[u][v][x]) return ans[u][v][x]; 36 for(int i=1;i<=n;i++){ 37 if(x>mp[u][i]) continue; 38 if(!dfs(v,i,mp[u][i])) return ans[u][v][x]=1;//(v,i,mp[u][i]) lost =>(u,v,x) win! 39 } 40 return ans[u][v][x]=0;//it can't win anyway 41 } 42 int main() 43 { 44 ci(n),ci(m); 45 memset(mp,-1, sizeof(mp)); 46 for(int i=0;i<m;i++) 47 { 48 int x,y; 49 char s[2]; 50 ci(x),ci(y); 51 scanf("%s",s); 52 mp[x][y]=s[0]-'a'; 53 } 54 for(int i=1;i<=n;i++){ 55 for(int j=1;j<=n;j++){ 56 if(dfs(i,j,0)==1) printf("A"); 57 else printf("B"); 58 } 59 puts(""); 60 } 61 return 0; 62 }