#include <iostream> #include <vector> using namespace std; int t,n; string before,after; vector<int> graph[100009]; int main() { cin >> t; while(t --> 0) { cin >> n; for(int i=1; i<=n; i++) { vector<int>().swap(graph[i]); } cin >> before >> after; int a,b; for(int i=0; i<n-1; i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } bool flag = false; bool rb=false, ra=false, bb=false, ba=false; for(int i=1; i<=n; i++) { if(graph[i].size() > 2) flag = true; if(before[i-1] == '0') rb = true; else bb = true; if(after[i-1] == '0') ra = true; else ba = true; } if(flag) { if((rb && bb) || (rb && !ba) || (bb && !ra)) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } } else { // ścieżka string b,a; char cb,ca; int v, prev; for(int i=1; i<=n; i++) { if(graph[i].size() <= 1) { v = i; prev = -1; break; } } while(true) { cb = before[v-1]; ca = after[v-1]; if(b.empty() || cb != b.back()) b.push_back(cb); if(a.empty() || ca != a.back()) a.push_back(ca); if(graph[v].size() == 0) break; int next = graph[v][0]; if(next == prev) { if(graph[v].size() < 2) break; else next = graph[v][1]; } prev = v; v = next; } // cout << b << ", " << a << endl; if((b.front() == a.front() && b.length() >= a.length()) || (b.front() != a.front() && b.length() > a.length())) { cout << "TAK" << endl; } else { cout << "NIE" << 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 | #include <iostream> #include <vector> using namespace std; int t,n; string before,after; vector<int> graph[100009]; int main() { cin >> t; while(t --> 0) { cin >> n; for(int i=1; i<=n; i++) { vector<int>().swap(graph[i]); } cin >> before >> after; int a,b; for(int i=0; i<n-1; i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } bool flag = false; bool rb=false, ra=false, bb=false, ba=false; for(int i=1; i<=n; i++) { if(graph[i].size() > 2) flag = true; if(before[i-1] == '0') rb = true; else bb = true; if(after[i-1] == '0') ra = true; else ba = true; } if(flag) { if((rb && bb) || (rb && !ba) || (bb && !ra)) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } } else { // ścieżka string b,a; char cb,ca; int v, prev; for(int i=1; i<=n; i++) { if(graph[i].size() <= 1) { v = i; prev = -1; break; } } while(true) { cb = before[v-1]; ca = after[v-1]; if(b.empty() || cb != b.back()) b.push_back(cb); if(a.empty() || ca != a.back()) a.push_back(ca); if(graph[v].size() == 0) break; int next = graph[v][0]; if(next == prev) { if(graph[v].size() < 2) break; else next = graph[v][1]; } prev = v; v = next; } // cout << b << ", " << a << endl; if((b.front() == a.front() && b.length() >= a.length()) || (b.front() != a.front() && b.length() > a.length())) { cout << "TAK" << endl; } else { cout << "NIE" << endl; } } } } |