#include <iostream> #include <list> #include <vector> #include <map> #include <queue> #include <string> using namespace std; typedef struct node{ vector<int> neighbours; int cnt=0; char have=-1; char want=-1; }node; int main(){ int t; cin >> t; while(t--){ int n; cin >> n; vector<node> graph; bool is_0 = false; bool is_1 = false; bool want_0 = false; bool want_1 = false; bool want_other = false; bool is_bambus = true; bool is_generator = false; bool is_all_different = true; int leaf = -1; for(int i=0; i<n; i++){ char x; cin >> x; node new_n; new_n.have = x; if(x=='0'){ is_0 = true; }else{ is_1 = true; } graph.push_back(new_n); } for(int i=0; i<n; i++){ char x; cin >> x; graph[i].want = x; if(x=='0'){ want_0 = true; }else{ want_1 = true; } if(graph[i].want != graph[i].have){ want_other = true; } } for(int i=0; i<n-1; i++){ int l, r; cin >> l >> r; graph[l-1].neighbours.push_back(r-1); graph[r-1].neighbours.push_back(l-1); graph[l-1].cnt++; graph[r-1].cnt++; } for(int i=0; i<n; i++){ if(graph[i].cnt>=3){ is_bambus=false; bool w_0 = false; bool w_1 = false; for(int j=0; j<graph[i].cnt; j++){ if(graph[graph[i].neighbours[j]].want == '0'){ w_0 = true; } if(graph[graph[i].neighbours[j]].want == '1'){ w_1 = true; } } if(w_0 && w_1){ is_generator = true; } } if(graph[i].cnt<=1){ leaf = i; } } for(int i=0; i<n; i++){ for(int j=0; j<graph[i].cnt; j++){ if(graph[graph[i].neighbours[j]].want == graph[i].want){ is_all_different = false; } } } if(!is_bambus){ if(!want_other){ cout << "TAK\n"; }else{ bool ended = false; if(is_0 && !is_1){ if(want_1){ ended = true; } }else if(is_1 && !is_0){ if(want_0){ ended = true; } } if(ended){ cout << "NIE\n"; }else if(is_generator){ cout << "TAK\n"; }else if(!is_all_different){ cout << "TAK\n"; }else{ cout << "NIE\n"; } } }else{ node cur_node = graph[leaf]; int prev_id = -1; int cur_id = leaf; string str_have; str_have.push_back(cur_node.have); int have_cnt = 0; string str_want; str_want.push_back(cur_node.want); int want_cnt = 0; for(int i=1; i<n; i++){ if (prev_id != cur_node.neighbours.back()){ prev_id = cur_id; cur_id = cur_node.neighbours.back(); cur_node = graph[cur_id]; }else{ prev_id = cur_id; cur_id = cur_node.neighbours.front(); cur_node = graph[cur_id]; } if(cur_node.have != str_have[have_cnt]){ have_cnt++; str_have += cur_node.have; } if(cur_node.want != str_want[want_cnt]){ want_cnt++; str_want += cur_node.want; } } //cout << str_have << " " << str_want << "\n"; if(str_have.find(str_want) != string::npos){ 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | #include <iostream> #include <list> #include <vector> #include <map> #include <queue> #include <string> using namespace std; typedef struct node{ vector<int> neighbours; int cnt=0; char have=-1; char want=-1; }node; int main(){ int t; cin >> t; while(t--){ int n; cin >> n; vector<node> graph; bool is_0 = false; bool is_1 = false; bool want_0 = false; bool want_1 = false; bool want_other = false; bool is_bambus = true; bool is_generator = false; bool is_all_different = true; int leaf = -1; for(int i=0; i<n; i++){ char x; cin >> x; node new_n; new_n.have = x; if(x=='0'){ is_0 = true; }else{ is_1 = true; } graph.push_back(new_n); } for(int i=0; i<n; i++){ char x; cin >> x; graph[i].want = x; if(x=='0'){ want_0 = true; }else{ want_1 = true; } if(graph[i].want != graph[i].have){ want_other = true; } } for(int i=0; i<n-1; i++){ int l, r; cin >> l >> r; graph[l-1].neighbours.push_back(r-1); graph[r-1].neighbours.push_back(l-1); graph[l-1].cnt++; graph[r-1].cnt++; } for(int i=0; i<n; i++){ if(graph[i].cnt>=3){ is_bambus=false; bool w_0 = false; bool w_1 = false; for(int j=0; j<graph[i].cnt; j++){ if(graph[graph[i].neighbours[j]].want == '0'){ w_0 = true; } if(graph[graph[i].neighbours[j]].want == '1'){ w_1 = true; } } if(w_0 && w_1){ is_generator = true; } } if(graph[i].cnt<=1){ leaf = i; } } for(int i=0; i<n; i++){ for(int j=0; j<graph[i].cnt; j++){ if(graph[graph[i].neighbours[j]].want == graph[i].want){ is_all_different = false; } } } if(!is_bambus){ if(!want_other){ cout << "TAK\n"; }else{ bool ended = false; if(is_0 && !is_1){ if(want_1){ ended = true; } }else if(is_1 && !is_0){ if(want_0){ ended = true; } } if(ended){ cout << "NIE\n"; }else if(is_generator){ cout << "TAK\n"; }else if(!is_all_different){ cout << "TAK\n"; }else{ cout << "NIE\n"; } } }else{ node cur_node = graph[leaf]; int prev_id = -1; int cur_id = leaf; string str_have; str_have.push_back(cur_node.have); int have_cnt = 0; string str_want; str_want.push_back(cur_node.want); int want_cnt = 0; for(int i=1; i<n; i++){ if (prev_id != cur_node.neighbours.back()){ prev_id = cur_id; cur_id = cur_node.neighbours.back(); cur_node = graph[cur_id]; }else{ prev_id = cur_id; cur_id = cur_node.neighbours.front(); cur_node = graph[cur_id]; } if(cur_node.have != str_have[have_cnt]){ have_cnt++; str_have += cur_node.have; } if(cur_node.want != str_want[want_cnt]){ want_cnt++; str_want += cur_node.want; } } //cout << str_have << " " << str_want << "\n"; if(str_have.find(str_want) != string::npos){ cout << "TAK\n"; }else{ cout << "NIE\n"; } } } } |