Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bitset>
- using namespace std;
- const int N_MAX = 5000;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int N;
- cin >> N;
- vector<string> edges(N - 1);
- for (int i = 0; i < N - 1; i++) {
- cin >> edges[i];
- }
- vector<vector<int>> red_adj(N);
- vector<vector<int>> blue_adj(N);
- for (int i = 0; i < N - 1; i++) {
- for (int j = 0; j < edges[i].size(); j++) {
- int neighbor = i + 1 + j;
- if (edges[i][j] == 'R') {
- red_adj[i].push_back(neighbor);
- } else if (edges[i][j] == 'B') {
- blue_adj[i].push_back(neighbor);
- }
- }
- }
- vector<bitset<N_MAX>> reach_red(N);
- vector<bitset<N_MAX>> reach_blue(N);
- for (int i = 0; i < N; i++) {
- reach_red[i].set(i);
- reach_blue[i].set(i);
- }
- for (int A = 0; A < N; A++) {
- for (int B : red_adj[A]) {
- reach_red[B] |= reach_red[A];
- }
- for (int B : blue_adj[A]) {
- reach_blue[B] |= reach_blue[A];
- }
- }
- for (int B = 0; B < N; B++) {
- bitset<N_MAX> intersection = reach_red[B] & reach_blue[B];
- intersection[B] = 0; // Exclude B itself
- if (intersection.any()) {
- cout << "NO" << endl;
- return 0;
- }
- }
- cout << "YES" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement