#include<iostream> #include<vector> #include<algorithm> using namespace std; int search(const vector<char> &colors,const vector<char> &desired,const vector<vector<int>> &graph, int curr, int prev, int count, int desCount){ if(prev!=-1){ if(desired[prev]!=desired[curr]){ desCount++; } count++; } if(count>desCount){ return count-desCount; } if(graph[curr].size()==1 && prev!=-1){ return count-desCount; } int ret = 0; for(auto j: graph[curr]){ if(j!=prev){ ret = max(ret, search(colors, desired, graph,j,curr,count,desCount)); if(ret > 0){ return ret; } } } return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--){ int n; cin >> n; vector<char> colors(n); vector<char> desired(n); bool allBlack = true; bool allRed = true; bool allBlack1 = true; bool allRed1 = true; for(int i=0;i<n;i++){ cin >> colors[i]; if(colors[i] == '1'){ allRed = false; }else{ allBlack = false; } } for(int i=0;i<n;i++){ cin >> desired[i]; if(desired[i] == '1'){ allRed1 = false; }else{ allBlack1 = false; } } vector<vector<int>> graph(n); for(int i=0;i<n-1;i++){ int a, b; cin >> a >> b; graph[a-1].push_back(b-1); graph[b-1].push_back(a-1); } bool three = false; vector<int> threes; for(int i=0;i<n;i++){ if(graph[i].size() >= 3){ threes.push_back(i); } } if(!threes.empty()){ if((allBlack&&!allBlack1)||(!allRed1&&allRed)){ cout << "NIE\n"; continue; }else{ bool isGood = false; for(auto three: threes){ for(auto neighbour: graph[three]){ if(desired[neighbour]==desired[three]){ isGood = true; break; } } if(isGood){ break; } } if(isGood){ cout << "TAK\n"; continue; } bool allEq = true; for(int i=0;i<n;i++){ if(colors[i]!=desired[i]){ allEq=false; break; } } if(allEq){ cout << "TAK\n"; continue; } int ret=0; for(int i=0;i<n;i++){ if(graph[i].size()==1){ ret = search(colors,desired,graph,i,-1,0,0); break; } } if(isGood || allEq || ret> 0){ cout << "TAK\n"; }else { cout << "NIE\n"; } continue; } }else if(graph[0].empty()){ cout << (colors[0]==desired[0] ? "TAK\n" : "NIE\n"); continue; }else{ int curr; char currColor = '.'; char desColor = '.'; int prev = -1; for(int i=0;i<n;i++){ if(graph[i].size() == 1){ curr = i; break; } } int count = 0; int desCount = 0; while(true){ if(currColor!=colors[curr]){ count++; } if(desColor!=desired[curr]){ desCount++; } currColor = colors[curr]; desColor = desired[curr]; bool toBreak = graph[curr].size() == 1 && prev != -1; for(int i: graph[curr]){ if(i!=prev){ prev = curr; curr = i; break; } } if(toBreak){ break; } } if(desCount<count){ cout << "TAK\n"; } else if(desCount == count){ cout << (colors[curr]==desired[curr]? "TAK\n": "NIE\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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int search(const vector<char> &colors,const vector<char> &desired,const vector<vector<int>> &graph, int curr, int prev, int count, int desCount){ if(prev!=-1){ if(desired[prev]!=desired[curr]){ desCount++; } count++; } if(count>desCount){ return count-desCount; } if(graph[curr].size()==1 && prev!=-1){ return count-desCount; } int ret = 0; for(auto j: graph[curr]){ if(j!=prev){ ret = max(ret, search(colors, desired, graph,j,curr,count,desCount)); if(ret > 0){ return ret; } } } return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--){ int n; cin >> n; vector<char> colors(n); vector<char> desired(n); bool allBlack = true; bool allRed = true; bool allBlack1 = true; bool allRed1 = true; for(int i=0;i<n;i++){ cin >> colors[i]; if(colors[i] == '1'){ allRed = false; }else{ allBlack = false; } } for(int i=0;i<n;i++){ cin >> desired[i]; if(desired[i] == '1'){ allRed1 = false; }else{ allBlack1 = false; } } vector<vector<int>> graph(n); for(int i=0;i<n-1;i++){ int a, b; cin >> a >> b; graph[a-1].push_back(b-1); graph[b-1].push_back(a-1); } bool three = false; vector<int> threes; for(int i=0;i<n;i++){ if(graph[i].size() >= 3){ threes.push_back(i); } } if(!threes.empty()){ if((allBlack&&!allBlack1)||(!allRed1&&allRed)){ cout << "NIE\n"; continue; }else{ bool isGood = false; for(auto three: threes){ for(auto neighbour: graph[three]){ if(desired[neighbour]==desired[three]){ isGood = true; break; } } if(isGood){ break; } } if(isGood){ cout << "TAK\n"; continue; } bool allEq = true; for(int i=0;i<n;i++){ if(colors[i]!=desired[i]){ allEq=false; break; } } if(allEq){ cout << "TAK\n"; continue; } int ret=0; for(int i=0;i<n;i++){ if(graph[i].size()==1){ ret = search(colors,desired,graph,i,-1,0,0); break; } } if(isGood || allEq || ret> 0){ cout << "TAK\n"; }else { cout << "NIE\n"; } continue; } }else if(graph[0].empty()){ cout << (colors[0]==desired[0] ? "TAK\n" : "NIE\n"); continue; }else{ int curr; char currColor = '.'; char desColor = '.'; int prev = -1; for(int i=0;i<n;i++){ if(graph[i].size() == 1){ curr = i; break; } } int count = 0; int desCount = 0; while(true){ if(currColor!=colors[curr]){ count++; } if(desColor!=desired[curr]){ desCount++; } currColor = colors[curr]; desColor = desired[curr]; bool toBreak = graph[curr].size() == 1 && prev != -1; for(int i: graph[curr]){ if(i!=prev){ prev = curr; curr = i; break; } } if(toBreak){ break; } } if(desCount<count){ cout << "TAK\n"; } else if(desCount == count){ cout << (colors[curr]==desired[curr]? "TAK\n": "NIE\n"); } else{ cout << "NIE\n"; } } } } |