#include <bits/stdc++.h> using namespace std; int t, n, a, b; vector<int> v[100000]; vector<int> liscie; string s1, s2; void clear() { for(int i=0; i<n; i++) { v[i].clear(); } liscie.clear(); } bool str_contains(string source, char letter) { for(int i = 0; i < source.size(); i++) { if (source[i] == letter) { return true; } } return false; } bool process_path() { int prev = -1; int current = liscie[0]; char firstOriginal = s1[current]; char firstDest = s2[current]; int changesSrc = 0; int changesDst = 0; while (current != liscie[1]) { int next = (v[current][0] == prev) ? v[current][1] : v[current][0]; if (s1[current] != s1[next]) { changesSrc++; } if (s2[current] != s2[next]) { changesDst++; } prev = current; current = next; } if (changesSrc > changesDst) { return true; } if (changesSrc == changesDst && firstDest == firstOriginal) { return true; } return false; } bool DFS() { for (int i=0; i < n; i++) { for(int j=0; j < v[i].size(); j++) { if (s2[i] == s2[v[i][j]]) { return true; } } } return false; } bool process_tree() { if (s1 == s2) { return true; } bool srcHas0 = str_contains(s1, '0'); bool srcHas1 = str_contains(s1, '1'); bool dstHas0 = str_contains(s2, '0'); bool dstHas1 = str_contains(s2, '1'); if (dstHas1 && !srcHas1) { return false; } if (dstHas0 && !srcHas0) { return false; } bool sciezka = true; for (int i = 0; i < n; i++) { int s = v[i].size(); if (s == 1) { liscie.push_back(i); } else if (s >= 3) { sciezka = false; } } if (sciezka) { // sciezka return process_path(); } return DFS(); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> t; for(int i=0; i<t; i++) { cin >> n; cin >> s1 >> s2; for(int j=0; j < n-1; j++) { cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } int out = process_tree(); if (out) { cout << "TAK\n"; } else { cout << "NIE\n"; } clear(); } 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 | #include <bits/stdc++.h> using namespace std; int t, n, a, b; vector<int> v[100000]; vector<int> liscie; string s1, s2; void clear() { for(int i=0; i<n; i++) { v[i].clear(); } liscie.clear(); } bool str_contains(string source, char letter) { for(int i = 0; i < source.size(); i++) { if (source[i] == letter) { return true; } } return false; } bool process_path() { int prev = -1; int current = liscie[0]; char firstOriginal = s1[current]; char firstDest = s2[current]; int changesSrc = 0; int changesDst = 0; while (current != liscie[1]) { int next = (v[current][0] == prev) ? v[current][1] : v[current][0]; if (s1[current] != s1[next]) { changesSrc++; } if (s2[current] != s2[next]) { changesDst++; } prev = current; current = next; } if (changesSrc > changesDst) { return true; } if (changesSrc == changesDst && firstDest == firstOriginal) { return true; } return false; } bool DFS() { for (int i=0; i < n; i++) { for(int j=0; j < v[i].size(); j++) { if (s2[i] == s2[v[i][j]]) { return true; } } } return false; } bool process_tree() { if (s1 == s2) { return true; } bool srcHas0 = str_contains(s1, '0'); bool srcHas1 = str_contains(s1, '1'); bool dstHas0 = str_contains(s2, '0'); bool dstHas1 = str_contains(s2, '1'); if (dstHas1 && !srcHas1) { return false; } if (dstHas0 && !srcHas0) { return false; } bool sciezka = true; for (int i = 0; i < n; i++) { int s = v[i].size(); if (s == 1) { liscie.push_back(i); } else if (s >= 3) { sciezka = false; } } if (sciezka) { // sciezka return process_path(); } return DFS(); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> t; for(int i=0; i<t; i++) { cin >> n; cin >> s1 >> s2; for(int j=0; j < n-1; j++) { cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } int out = process_tree(); if (out) { cout << "TAK\n"; } else { cout << "NIE\n"; } clear(); } return 0; } |