#include<iostream> #include<string> #include<vector> #include<algorithm> #include<list> using namespace std; void dfs(vector<vector<int>>& adj, string& str, string& colors, int u, int parent) { char c = colors[u]; if (str.empty() || str[str.size() - 1] != c) { str.push_back(c); } for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v != parent) { dfs(adj, str, colors, v, u); } } } bool cantwo(vector<vector<int>>& adj, string& colors) { bool two = false; for (int u = 0; u < adj.size(); u++) { for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j]; if (colors[u] == colors[v]) { return true; } } } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, iter = 0; cin >> t; for (; t > 0; t--) { iter += 1; int n; cin >> n; string source, target; cin >> source >> target; int maxdeg = 1; int onedeg = -1; vector<vector<int>> adj(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u -= 1; v -= 1; adj[u].push_back(v); adj[v].push_back(u); maxdeg = max(maxdeg, (int)adj[u].size()); maxdeg = max(maxdeg, (int)adj[v].size()); if (adj[u].size() == 1) onedeg = u; if (adj[v].size() == 1) onedeg = v; } for (int i = 0; i < n; i++) { if (adj[i].size() == 1) onedeg = i; } if (source == target) { cout << "TAK\n"; continue; } if (n == 1) { if (source[0] == target[0]) { cout << "TAK\n"; } else { cout << "NIE\n"; } continue; } bool black_s = false, red_s = false; for (int i = 0; i < source.size(); i++) { if (source[i] == '1') { black_s = true; } else { red_s = true; } } bool black_t = false, red_t = false; for (int i = 0; i < target.size(); i++) { if (target[i] == '1') { black_t = true; } else { red_t = true; } } if (red_t && !red_s) { cout << "NIE\n"; continue; } if (black_t && !black_s) { cout << "NIE\n"; continue; } if (red_t && !red_s) { cout << "TAK\n"; continue; } if (black_t && !black_s) { cout << "TAK\n"; continue; } if (maxdeg >= 3) { bool two = cantwo(adj, target); if (two) { cout << "TAK\n"; } else { cout << "NIE\n"; } } else { string s; dfs(adj, s, source, onedeg, -1); string t; dfs(adj, t, target, onedeg, -1); size_t found = s.find(t); if (found != string::npos) { cout << "TAK\n"; } else { cout << "NIE\n"; } } } 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 133 134 135 136 137 | #include<iostream> #include<string> #include<vector> #include<algorithm> #include<list> using namespace std; void dfs(vector<vector<int>>& adj, string& str, string& colors, int u, int parent) { char c = colors[u]; if (str.empty() || str[str.size() - 1] != c) { str.push_back(c); } for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v != parent) { dfs(adj, str, colors, v, u); } } } bool cantwo(vector<vector<int>>& adj, string& colors) { bool two = false; for (int u = 0; u < adj.size(); u++) { for (int j = 0; j < adj[u].size(); j++) { int v = adj[u][j]; if (colors[u] == colors[v]) { return true; } } } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, iter = 0; cin >> t; for (; t > 0; t--) { iter += 1; int n; cin >> n; string source, target; cin >> source >> target; int maxdeg = 1; int onedeg = -1; vector<vector<int>> adj(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u -= 1; v -= 1; adj[u].push_back(v); adj[v].push_back(u); maxdeg = max(maxdeg, (int)adj[u].size()); maxdeg = max(maxdeg, (int)adj[v].size()); if (adj[u].size() == 1) onedeg = u; if (adj[v].size() == 1) onedeg = v; } for (int i = 0; i < n; i++) { if (adj[i].size() == 1) onedeg = i; } if (source == target) { cout << "TAK\n"; continue; } if (n == 1) { if (source[0] == target[0]) { cout << "TAK\n"; } else { cout << "NIE\n"; } continue; } bool black_s = false, red_s = false; for (int i = 0; i < source.size(); i++) { if (source[i] == '1') { black_s = true; } else { red_s = true; } } bool black_t = false, red_t = false; for (int i = 0; i < target.size(); i++) { if (target[i] == '1') { black_t = true; } else { red_t = true; } } if (red_t && !red_s) { cout << "NIE\n"; continue; } if (black_t && !black_s) { cout << "NIE\n"; continue; } if (red_t && !red_s) { cout << "TAK\n"; continue; } if (black_t && !black_s) { cout << "TAK\n"; continue; } if (maxdeg >= 3) { bool two = cantwo(adj, target); if (two) { cout << "TAK\n"; } else { cout << "NIE\n"; } } else { string s; dfs(adj, s, source, onedeg, -1); string t; dfs(adj, t, target, onedeg, -1); size_t found = s.find(t); if (found != string::npos) { cout << "TAK\n"; } else { cout << "NIE\n"; } } } return 0; } |