Josif_tepe

Untitled

Aug 26th, 2025
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. //#include <bits/stdc++.h>
  5. using namespace std;
  6. int n,m;
  7. const int INF = 1e9;
  8. vector<pair<int,int>>g[105];
  9. int memo[105][105][26][2];
  10. int dfs(int maks, int lukas, int turn, int last_char) {
  11.     if(memo[maks][lukas][last_char][turn] != -1) {
  12.         return memo[maks][lukas][last_char][turn];
  13.     }
  14.    
  15.     int res;
  16.    
  17.     if(turn == 0) {
  18.         res = -INF;
  19.         for(pair<int, int> p: g[maks]) {
  20.             if(last_char == -1) {
  21.                 res = max(res, dfs(p.first, lukas, 1 - turn, p.second));
  22.             }
  23.             else {
  24.                 if(p.second >= last_char) {
  25.                     res = max(res, dfs(p.first, lukas, 1 - turn, p.second));
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     else {
  31.         res = INF;
  32.         for(pair<int, int> p: g[lukas]) {
  33.             if(p.second >= last_char) {
  34.                 res = min(res, dfs(maks, p.first, 1 - turn, p.second));
  35.             }
  36.         }
  37.     }
  38.     memo[maks][lukas][last_char][turn] = res;
  39.     return res;
  40. }
  41.  
  42.  
  43.  
  44. int main()
  45. {
  46.     ios_base::sync_with_stdio(false);
  47.   cin>>n>>m;
  48.   for(int i = 0;i<m;i++){
  49.       int v,u;
  50.       char ch;
  51.       cin>>v>>u>>ch;
  52.       g[v].push_back({u,ch-'a'});
  53.   }
  54.   memset(memo,-1,sizeof(memo));
  55.  
  56.   for(int i = 1;i<=n;i++){
  57.       for(int j = 1;j<=n;j++){
  58.           if(dfs(i, j, 0, -1) == INF) {
  59.               cout << "A";
  60.           }
  61.           else {
  62.               cout << "B";
  63.           }
  64.       }
  65.       cout<<"\n";
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment