#include<bits/stdc++.h> using namespace std; #define pb push_back #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define vi vector<int> #define ii pair<int,int> #define vii vector<ii> #define vvi vector<vi> #define ll long long const int N = 1e6 + 7; int n; pair<char, char> kolory(string s){ sort(all(s)); return {s[0], s.back()}; } void dfs(int start, vvi & G, vi & kol, int prz = -1){ kol.pb(start); for(auto & u : G[start]) if(u != prz) dfs(u, G, kol, start); } int ciag(int pocz, string & cols, vvi & G){ vector<int> kol; dfs(pocz, G, kol); int dl = 1; for(int i = 0; i < n - 1; i++) if(cols[kol[i]] != cols[kol[i + 1]]) dl++; return dl; } void solve(){ cin >> n; string s, t; cin >> s >> t; vvi G(n); bool jest = false; for(int i = 1; i < n; i++){ int a, b; cin >> a >> b; a--, b--; G[a].pb(b); G[b].pb(a); if(t[a] == t[b]) jest = true; } auto A = kolory(s); auto B = kolory(t); if((B.f != B.s and A.f == A.s) or (A.f == A.s and B.f == B.s and A.f != B.f)){ cout << "NIE\n"; return; } if(s == t){ cout << "TAK\n"; return; } bool sciezka = true; for(int i = 0; i < n; i++) if(sz(G[i]) > 2) sciezka = false; if(sciezka){ int pocz = -1; for(int i = 0; i < n; i++) if(sz(G[i]) == 1){ pocz = i; break; } assert(pocz != -1); int a = ciag(pocz, s, G); int b = ciag(pocz, t, G); a -= s[pocz] != t[pocz]; cout << (b <= a ? "TAK\n" : "NIE\n"); } else cout << (jest ? "TAK\n" : "NIE\n"); } int main(){ ios::sync_with_stdio(0); cin.tie(0); int testy; cin >> testy; while(testy--){ solve(); cout << flush; } 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 | #include<bits/stdc++.h> using namespace std; #define pb push_back #define f first #define s second #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define vi vector<int> #define ii pair<int,int> #define vii vector<ii> #define vvi vector<vi> #define ll long long const int N = 1e6 + 7; int n; pair<char, char> kolory(string s){ sort(all(s)); return {s[0], s.back()}; } void dfs(int start, vvi & G, vi & kol, int prz = -1){ kol.pb(start); for(auto & u : G[start]) if(u != prz) dfs(u, G, kol, start); } int ciag(int pocz, string & cols, vvi & G){ vector<int> kol; dfs(pocz, G, kol); int dl = 1; for(int i = 0; i < n - 1; i++) if(cols[kol[i]] != cols[kol[i + 1]]) dl++; return dl; } void solve(){ cin >> n; string s, t; cin >> s >> t; vvi G(n); bool jest = false; for(int i = 1; i < n; i++){ int a, b; cin >> a >> b; a--, b--; G[a].pb(b); G[b].pb(a); if(t[a] == t[b]) jest = true; } auto A = kolory(s); auto B = kolory(t); if((B.f != B.s and A.f == A.s) or (A.f == A.s and B.f == B.s and A.f != B.f)){ cout << "NIE\n"; return; } if(s == t){ cout << "TAK\n"; return; } bool sciezka = true; for(int i = 0; i < n; i++) if(sz(G[i]) > 2) sciezka = false; if(sciezka){ int pocz = -1; for(int i = 0; i < n; i++) if(sz(G[i]) == 1){ pocz = i; break; } assert(pocz != -1); int a = ciag(pocz, s, G); int b = ciag(pocz, t, G); a -= s[pocz] != t[pocz]; cout << (b <= a ? "TAK\n" : "NIE\n"); } else cout << (jest ? "TAK\n" : "NIE\n"); } int main(){ ios::sync_with_stdio(0); cin.tie(0); int testy; cin >> testy; while(testy--){ solve(); cout << flush; } return 0; } |