// C5 - Drzewa czerwono-czarne #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Node { vector<int> edges; int color1; int color2; bool leaf; bool almostLeaf; bool possibleStart; Node(int c) { color1 = c; leaf = false; almostLeaf = false; possibleStart = false; } }; int main() { int t, n, a, b; int colors1; int colors2; int start; int curr; int prev; int tmpPrev; char c; vector<Node> tree; cin >> t; for (int z = 0; z < t; ++z) { tree.clear(); cin >> n; for (int i = 0; i < n; ++i) { cin >> c; tree.push_back(Node(c - '0')); } for (int i = 0; i < n; ++i) { cin >> c; tree[i].color2 = c - '0'; } for (int i = 0; i < n - 1; ++i) { cin >> a; cin >> b; --a; --b; tree[a].edges.push_back(b); tree[b].edges.push_back(a); } bool redOccurs1; bool redOccurs2; bool blackOccurs1; bool blackOccurs2; redOccurs1 = redOccurs2 = blackOccurs1 = blackOccurs2 = false; for (int i = 0; i < n; ++i) { if (tree[i].color1 == 0) { redOccurs1 = true; } else { blackOccurs1 = true; } if (tree[i].color2 == 0) { redOccurs2 = true; } else { blackOccurs2 = true; } } if (!redOccurs1 && redOccurs2) { cout << "NIE" << endl; continue; } if (!blackOccurs1 && blackOccurs2) { cout << "NIE" << endl; continue; } if (n == 1) { cout << "TAK" << endl; continue; } bool chain; chain = true; for (int i = 0; i < n; ++i) { if (tree[i].edges.size() > 2) { chain = false; break; } } if (chain) { for (int i = 0; i < n; ++i) { if (tree[i].edges.size() <= 1) { curr = tree[i].edges[0]; start = prev = i; break; } } colors1 = 1; colors2 = 1; while (tree[curr].edges.size() > 1) { if (tree[curr].color1 != tree[prev].color1) { ++colors1; } if (tree[curr].color2 != tree[prev].color2) { ++colors2; } tmpPrev = prev; prev = curr; curr = (tree[curr].edges[0] != tmpPrev ? tree[curr].edges[0] : tree[curr].edges[1]); } if (tree[curr].color1 != tree[prev].color1) { ++colors1; } if (tree[curr].color2 != tree[prev].color2) { ++colors2; } if (colors1 < colors2) { cout << "NIE" << endl; continue; } if (colors1 == colors2 && tree[start].color1 != tree[start].color2) { cout << "NIE" << endl; continue; } } bool keyToVictory; bool possibleFailure; vector<int> possibleStarts; keyToVictory = false; possibleFailure = false; for (int i = 0; i < n; ++i) { if (tree[i].edges.size() > 2) { blackOccurs1 = false; redOccurs1 = false; for (int j = 0; j < tree[i].edges.size() && (!blackOccurs1 || !redOccurs1); ++j) { if (tree[tree[i].edges[j]].color2 == 0) { redOccurs1 = true; } else { blackOccurs1 = true; } } if (redOccurs1 && blackOccurs1) { keyToVictory = true; break; } if (redOccurs1 && tree[i].color2 == 1 && tree[i].color1 == 0) { possibleFailure = true; } if (blackOccurs1 && tree[i].color2 == 0 && tree[i].color1 == 1) { possibleFailure = true; } } } if (keyToVictory) { cout << "TAK" << endl; continue; } if (possibleFailure) { cout << "NIE" << endl; continue; } cout << "TAK" << endl; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | // C5 - Drzewa czerwono-czarne #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Node { vector<int> edges; int color1; int color2; bool leaf; bool almostLeaf; bool possibleStart; Node(int c) { color1 = c; leaf = false; almostLeaf = false; possibleStart = false; } }; int main() { int t, n, a, b; int colors1; int colors2; int start; int curr; int prev; int tmpPrev; char c; vector<Node> tree; cin >> t; for (int z = 0; z < t; ++z) { tree.clear(); cin >> n; for (int i = 0; i < n; ++i) { cin >> c; tree.push_back(Node(c - '0')); } for (int i = 0; i < n; ++i) { cin >> c; tree[i].color2 = c - '0'; } for (int i = 0; i < n - 1; ++i) { cin >> a; cin >> b; --a; --b; tree[a].edges.push_back(b); tree[b].edges.push_back(a); } bool redOccurs1; bool redOccurs2; bool blackOccurs1; bool blackOccurs2; redOccurs1 = redOccurs2 = blackOccurs1 = blackOccurs2 = false; for (int i = 0; i < n; ++i) { if (tree[i].color1 == 0) { redOccurs1 = true; } else { blackOccurs1 = true; } if (tree[i].color2 == 0) { redOccurs2 = true; } else { blackOccurs2 = true; } } if (!redOccurs1 && redOccurs2) { cout << "NIE" << endl; continue; } if (!blackOccurs1 && blackOccurs2) { cout << "NIE" << endl; continue; } if (n == 1) { cout << "TAK" << endl; continue; } bool chain; chain = true; for (int i = 0; i < n; ++i) { if (tree[i].edges.size() > 2) { chain = false; break; } } if (chain) { for (int i = 0; i < n; ++i) { if (tree[i].edges.size() <= 1) { curr = tree[i].edges[0]; start = prev = i; break; } } colors1 = 1; colors2 = 1; while (tree[curr].edges.size() > 1) { if (tree[curr].color1 != tree[prev].color1) { ++colors1; } if (tree[curr].color2 != tree[prev].color2) { ++colors2; } tmpPrev = prev; prev = curr; curr = (tree[curr].edges[0] != tmpPrev ? tree[curr].edges[0] : tree[curr].edges[1]); } if (tree[curr].color1 != tree[prev].color1) { ++colors1; } if (tree[curr].color2 != tree[prev].color2) { ++colors2; } if (colors1 < colors2) { cout << "NIE" << endl; continue; } if (colors1 == colors2 && tree[start].color1 != tree[start].color2) { cout << "NIE" << endl; continue; } } bool keyToVictory; bool possibleFailure; vector<int> possibleStarts; keyToVictory = false; possibleFailure = false; for (int i = 0; i < n; ++i) { if (tree[i].edges.size() > 2) { blackOccurs1 = false; redOccurs1 = false; for (int j = 0; j < tree[i].edges.size() && (!blackOccurs1 || !redOccurs1); ++j) { if (tree[tree[i].edges[j]].color2 == 0) { redOccurs1 = true; } else { blackOccurs1 = true; } } if (redOccurs1 && blackOccurs1) { keyToVictory = true; break; } if (redOccurs1 && tree[i].color2 == 1 && tree[i].color1 == 0) { possibleFailure = true; } if (blackOccurs1 && tree[i].color2 == 0 && tree[i].color1 == 1) { possibleFailure = true; } } } if (keyToVictory) { cout << "TAK" << endl; continue; } if (possibleFailure) { cout << "NIE" << endl; continue; } cout << "TAK" << endl; } return 0; } |