#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long using namespace std; const int nax = 1e5 + 5; int a[nax]; int b[nax]; vector<int> adj[nax]; int n; bool ok = true; bool vis[nax]; int col[nax]; void dfs(int v){ vis[v] = true; for(int x : adj[v]){ if(vis[x] && b[x] == b[v]) ok = false; if(!vis[x]) dfs(x); } } bool bip(){ ok = true; for(int i=1;i<=n;i++) vis[i] = false; dfs(1); return ok; } void solve(){ cin >> n; string x1, x2; cin >> x1 >> x2; set<int> cola, colb; for(int i=1;i<=n;i++){ a[i] = x1[i - 1] - '0'; b[i] = x2[i - 1] - '0'; cola.insert(a[i]); colb.insert(b[i]); } for(int i=1;i<=n;i++) adj[i].clear(); for(int i=1;i<n;i++){ int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } if(cola.size() == 1){ for(int x : colb){ if(cola.count(x) == 0){ cout << "NIE" << "\n"; return; } } cout << "TAK" << "\n"; return; } bool ex = false; for(int i=1;i<=n;i++){ if(adj[i].size() > 2) ex = true; } if(ex){ int df = 0; for(int i=1;i<=n;i++){ df += (a[i] != b[i]); } bool jest = false; jest = !bip(); /*or(int i=1;i<=n;i++){ if(adj[i].size() > 2){ int c = b[i]; int ile = 0; for(int x : adj[i]){ if(b[x] == c) ile += 1; } if(ile) jest = true; //if(adj[i].size() < n - 1) jest = true; } }*/ if(df == 0 || jest){ cout << "TAK" << "\n"; } else cout << "NIE" << "\n"; return; } int go = -1; for(int i=1;i<=n;i++){ if(adj[i].size() == 1) go = i; } vector<bool> vis(n + 1, false); int lasta = -1; int lastb = -1; int c1 = 0; int c2 = 0; int g = go; while(1){ if(a[go] != lasta) c1 += 1; if(b[go] != lastb) c2 += 1; lasta = a[go]; lastb = b[go]; vis[go] = true; int nxt = -1; for(int x : adj[go]){ if(vis[x] == false) nxt = x; } if(nxt == -1) break; go = nxt; } if(c2 < c1 || (c2 == c1 && a[go] == b[go])){ cout << "TAK" << "\n"; } else cout << "NIE" << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; cin >> tt; while(tt--) solve(); 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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long using namespace std; const int nax = 1e5 + 5; int a[nax]; int b[nax]; vector<int> adj[nax]; int n; bool ok = true; bool vis[nax]; int col[nax]; void dfs(int v){ vis[v] = true; for(int x : adj[v]){ if(vis[x] && b[x] == b[v]) ok = false; if(!vis[x]) dfs(x); } } bool bip(){ ok = true; for(int i=1;i<=n;i++) vis[i] = false; dfs(1); return ok; } void solve(){ cin >> n; string x1, x2; cin >> x1 >> x2; set<int> cola, colb; for(int i=1;i<=n;i++){ a[i] = x1[i - 1] - '0'; b[i] = x2[i - 1] - '0'; cola.insert(a[i]); colb.insert(b[i]); } for(int i=1;i<=n;i++) adj[i].clear(); for(int i=1;i<n;i++){ int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } if(cola.size() == 1){ for(int x : colb){ if(cola.count(x) == 0){ cout << "NIE" << "\n"; return; } } cout << "TAK" << "\n"; return; } bool ex = false; for(int i=1;i<=n;i++){ if(adj[i].size() > 2) ex = true; } if(ex){ int df = 0; for(int i=1;i<=n;i++){ df += (a[i] != b[i]); } bool jest = false; jest = !bip(); /*or(int i=1;i<=n;i++){ if(adj[i].size() > 2){ int c = b[i]; int ile = 0; for(int x : adj[i]){ if(b[x] == c) ile += 1; } if(ile) jest = true; //if(adj[i].size() < n - 1) jest = true; } }*/ if(df == 0 || jest){ cout << "TAK" << "\n"; } else cout << "NIE" << "\n"; return; } int go = -1; for(int i=1;i<=n;i++){ if(adj[i].size() == 1) go = i; } vector<bool> vis(n + 1, false); int lasta = -1; int lastb = -1; int c1 = 0; int c2 = 0; int g = go; while(1){ if(a[go] != lasta) c1 += 1; if(b[go] != lastb) c2 += 1; lasta = a[go]; lastb = b[go]; vis[go] = true; int nxt = -1; for(int x : adj[go]){ if(vis[x] == false) nxt = x; } if(nxt == -1) break; go = nxt; } if(c2 < c1 || (c2 == c1 && a[go] == b[go])){ cout << "TAK" << "\n"; } else cout << "NIE" << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; cin >> tt; while(tt--) solve(); return 0; } |