Advertisement
coloriot

HA32_Colored(15)

Nov 24th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bitset>
  4.  
  5. using namespace std;
  6.  
  7. const int N_MAX = 5000;
  8.  
  9. int main() {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(NULL);
  12.  
  13.     int N;
  14.     cin >> N;
  15.     vector<string> edges(N - 1);
  16.  
  17.     for (int i = 0; i < N - 1; i++) {
  18.         cin >> edges[i];
  19.     }
  20.  
  21.     vector<vector<int>> red_adj(N);
  22.     vector<vector<int>> blue_adj(N);
  23.  
  24.     for (int i = 0; i < N - 1; i++) {
  25.         for (int j = 0; j < edges[i].size(); j++) {
  26.             int neighbor = i + 1 + j;
  27.             if (edges[i][j] == 'R') {
  28.                 red_adj[i].push_back(neighbor);
  29.             } else if (edges[i][j] == 'B') {
  30.                 blue_adj[i].push_back(neighbor);
  31.             }
  32.         }
  33.     }
  34.  
  35.     vector<bitset<N_MAX>> reach_red(N);
  36.     vector<bitset<N_MAX>> reach_blue(N);
  37.  
  38.     for (int i = 0; i < N; i++) {
  39.         reach_red[i].set(i);
  40.         reach_blue[i].set(i);
  41.     }
  42.  
  43.     for (int A = 0; A < N; A++) {
  44.         for (int B : red_adj[A]) {
  45.             reach_red[B] |= reach_red[A];
  46.         }
  47.         for (int B : blue_adj[A]) {
  48.             reach_blue[B] |= reach_blue[A];
  49.         }
  50.     }
  51.  
  52.     for (int B = 0; B < N; B++) {
  53.         bitset<N_MAX> intersection = reach_red[B] & reach_blue[B];
  54.         intersection[B] = 0; // Exclude B itself
  55.         if (intersection.any()) {
  56.             cout << "NO" << endl;
  57.             return 0;
  58.         }
  59.     }
  60.  
  61.     cout << "YES" << endl;
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement