#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; const l INF = 1000000000 + 9; const ll MOD = 123456789; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second l num_tests, n; string colors_before, colors_after; vector<l> graph[1000015]; l is_path() { l cnt = 0, last = -1; FORE(1, n, i) { if(graph[i].size() == 1) { cnt++; last = i; } } if(cnt > 2) return -1; else return last; } bool check_simple(string bef, string aft) { string compressed_before = "", compressed_after = ""; compressed_before.PB(bef[0]); compressed_after.PB(aft[0]); FOR(1, n, i) { if(bef[i] != compressed_before.back()) compressed_before.PB(bef[i]); if(aft[i] != compressed_after.back()) compressed_after.PB(aft[i]); } if(compressed_before == compressed_after) return true; else return compressed_before.length() > compressed_after.length(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> num_tests; while(num_tests--) { cin >> n >> colors_before >> colors_after; bool is_before[2] = {false, false}, is_after[2] = {false, false}; bool same_color = false; REP(n, i) { if(colors_before[i] == '0') is_before[0] = true; else is_before[1] = true; if(colors_after[i] == '0') is_after[0] = true; else is_after[1] = true; graph[i+1].clear(); } REP(n-1, i) { l a, b; cin >> a >> b; graph[a].PB(b); graph[b].PB(a); if(colors_after[a-1] == colors_after[b-1]) same_color = true; } if(colors_before == colors_after) { cout << "TAK\n"; continue; } if(is_before[0] + is_before[1] == 1 && is_after[0] + is_after[1] == 1) { if(is_before[0] != is_after[0]) cout << "NIE\n"; else cout << "TAK\n"; continue; } else if(is_before[0] + is_before[1] != is_after[0] + is_after[1]) { if(is_before[0] + is_before[1] < is_after[0] + is_after[1]) cout << "NIE\n"; else cout << "TAK\n"; continue; } l start = is_path(); if(start == -1) { cout << (same_color ? "TAK\n" : "NIE\n"); continue; } string new_colors_before = "", new_colors_after = ""; l pos = start, last = -1; while(true) { new_colors_before.PB(colors_before[pos-1]); new_colors_after.PB(colors_after[pos-1]); if(pos != start && graph[pos].size() == 1) break; if(graph[pos][0] != last) { last = pos; pos = graph[last][0]; } else { last = pos; pos = graph[last][1]; } } if(check_simple(new_colors_before, new_colors_after)) cout << "TAK\n"; else cout << "NIE\n"; } }
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; const l INF = 1000000000 + 9; const ll MOD = 123456789; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second l num_tests, n; string colors_before, colors_after; vector<l> graph[1000015]; l is_path() { l cnt = 0, last = -1; FORE(1, n, i) { if(graph[i].size() == 1) { cnt++; last = i; } } if(cnt > 2) return -1; else return last; } bool check_simple(string bef, string aft) { string compressed_before = "", compressed_after = ""; compressed_before.PB(bef[0]); compressed_after.PB(aft[0]); FOR(1, n, i) { if(bef[i] != compressed_before.back()) compressed_before.PB(bef[i]); if(aft[i] != compressed_after.back()) compressed_after.PB(aft[i]); } if(compressed_before == compressed_after) return true; else return compressed_before.length() > compressed_after.length(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> num_tests; while(num_tests--) { cin >> n >> colors_before >> colors_after; bool is_before[2] = {false, false}, is_after[2] = {false, false}; bool same_color = false; REP(n, i) { if(colors_before[i] == '0') is_before[0] = true; else is_before[1] = true; if(colors_after[i] == '0') is_after[0] = true; else is_after[1] = true; graph[i+1].clear(); } REP(n-1, i) { l a, b; cin >> a >> b; graph[a].PB(b); graph[b].PB(a); if(colors_after[a-1] == colors_after[b-1]) same_color = true; } if(colors_before == colors_after) { cout << "TAK\n"; continue; } if(is_before[0] + is_before[1] == 1 && is_after[0] + is_after[1] == 1) { if(is_before[0] != is_after[0]) cout << "NIE\n"; else cout << "TAK\n"; continue; } else if(is_before[0] + is_before[1] != is_after[0] + is_after[1]) { if(is_before[0] + is_before[1] < is_after[0] + is_after[1]) cout << "NIE\n"; else cout << "TAK\n"; continue; } l start = is_path(); if(start == -1) { cout << (same_color ? "TAK\n" : "NIE\n"); continue; } string new_colors_before = "", new_colors_after = ""; l pos = start, last = -1; while(true) { new_colors_before.PB(colors_before[pos-1]); new_colors_after.PB(colors_after[pos-1]); if(pos != start && graph[pos].size() == 1) break; if(graph[pos][0] != last) { last = pos; pos = graph[last][0]; } else { last = pos; pos = graph[last][1]; } } if(check_simple(new_colors_before, new_colors_after)) cout << "TAK\n"; else cout << "NIE\n"; } } |