#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(const T &t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(const std::pair<T1, T2> &t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(const std::vector<T> &t) { for (auto a : t) print(a); std::cout << std::endl; } std::vector<vi> G; vi A, B; void solve(int i, int p, std::string &S, vi &V) { if (!V.size() || V.back() != S[i] - '0') V.push_back(S[i] - '0'); for (auto a : G[i]) if (a != p) solve(a, i, S, V); } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; std::cin >> Z; while (Z--) { int n; std::cin >> n; std::string S1, S2; std::cin >> S1 >> S2; G.clear(); G.resize(n); for (int i = 0; i < n - 1; i++) { int a, b; std::cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } if (S1 == S2) { std::cout << "TAK" << std::endl; continue; } bool t1 = 1, t2 = 1; for (auto &a : S1) if (a == '0') t1 = 0; else t2 = 0; if (t1 || t2) { std::cout << "NIE" << std::endl; continue; } t1 = 1, t2 = 0; int start = -1; for (int i = 0; i < n; i++) { for (auto &a : G[i]) if (S2[a] == S2[i]) t1 = 0; if (G[i].size() > 2) t2 = 1; if (G[i].size() == 1) start = i; } if (t1) { std::cout << "NIE" << std::endl; continue; } if (t2) { std::cout << "TAK" << std::endl; continue; } A.clear(); B.clear(); solve(start, -1, S1, A); solve(start, -1, S2, B); if (B.size() < A.size() || A == B) std::cout << "TAK" << std::endl; else std::cout << "NIE" << std::endl; } }
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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(const T &t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(const std::pair<T1, T2> &t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(const std::vector<T> &t) { for (auto a : t) print(a); std::cout << std::endl; } std::vector<vi> G; vi A, B; void solve(int i, int p, std::string &S, vi &V) { if (!V.size() || V.back() != S[i] - '0') V.push_back(S[i] - '0'); for (auto a : G[i]) if (a != p) solve(a, i, S, V); } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; std::cin >> Z; while (Z--) { int n; std::cin >> n; std::string S1, S2; std::cin >> S1 >> S2; G.clear(); G.resize(n); for (int i = 0; i < n - 1; i++) { int a, b; std::cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } if (S1 == S2) { std::cout << "TAK" << std::endl; continue; } bool t1 = 1, t2 = 1; for (auto &a : S1) if (a == '0') t1 = 0; else t2 = 0; if (t1 || t2) { std::cout << "NIE" << std::endl; continue; } t1 = 1, t2 = 0; int start = -1; for (int i = 0; i < n; i++) { for (auto &a : G[i]) if (S2[a] == S2[i]) t1 = 0; if (G[i].size() > 2) t2 = 1; if (G[i].size() == 1) start = i; } if (t1) { std::cout << "NIE" << std::endl; continue; } if (t2) { std::cout << "TAK" << std::endl; continue; } A.clear(); B.clear(); solve(start, -1, S1, A); solve(start, -1, S2, B); if (B.size() < A.size() || A == B) std::cout << "TAK" << std::endl; else std::cout << "NIE" << std::endl; } } |