#include <stdio.h> #include <vector> int Current[200000]; int Target[200000]; struct Node { std::vector<int> nodes; }; Node Tree[200000]; int Branch[200000]; int N; int GetLeaf() { for(int i=1; i<=N; i++) { if(Tree[i].nodes.size() <= 1) return i; } return -1; } int GetNext(int node, int prevNode) { auto it = Tree[node].nodes.begin(); if(it == Tree[node].nodes.end()) return 0; if(*it != prevNode) return *it; ++it; if(it == Tree[node].nodes.end()) return 0; return *it; } void ProcessLinear() { int starter = GetLeaf(); int changes1 = 0; int changes2 = 0; int prev1 = Current[starter]; int prev2 = Target[starter]; int node = GetNext(starter, -1); int prevNode = starter; while(node > 0) { if(prev1 != Current[node]) changes1++; if(prev2 != Target[node]) changes2++; prev1 = Current[node]; prev2 = Target[node]; int next = GetNext(node, prevNode); prevNode = node; node = next; } if(changes1 > changes2) { printf("TAK\n"); return; } if(changes1 == changes2 && Current[starter] == Target[starter]) { printf("TAK\n"); return; } printf("NIE\n"); } bool IsBranch(int node) { return Tree[node].nodes.size() >= 3; } bool DFS(int node, int prevNode) { if(Target[node] == Target[prevNode]) return true; //go straight while(IsBranch(node) == false) { int next = GetNext(node, prevNode); if(next == 0) return false; prevNode = node; node = next; if(Target[node] == Target[prevNode]) return true; } //go DFS for(auto next : Tree[node].nodes) { if(next == prevNode) continue; if(DFS(next, node)) return true; } return false; } void ProcessTree() { int node = GetLeaf(); int next = GetNext(node, -1); if(DFS(next, node)) printf("TAK\n"); else printf("NIE\n"); } int main() { int tc; scanf("%d ", &tc); for(int i=0; i<tc; i++) { scanf("%d ", &N); bool currentAnyZero = false; bool currentAnyOne = false; for(int i = 1; i <= N; i++) { scanf("%1d ", &Current[i]); currentAnyZero = Current[i] == 0 ? true : currentAnyZero; currentAnyOne = Current[i] == 1 ? true : currentAnyOne; Tree[i].nodes.clear(); Branch[i] = 0; } bool targetAnyZero = false; bool targetAnyOne = false; for(int i = 1; i <= N; i++) { scanf("%1d ", &Target[i]); targetAnyZero = Target[i] == 0 ? true : targetAnyZero; targetAnyOne = Target[i] == 1 ? true : targetAnyOne; } bool anyBranch = false; for(int i=1; i<N; i++) { int from, to; scanf("%d %d ", &from, &to); Tree[from].nodes.push_back(to); Tree[to].nodes.push_back(from); Branch[from] = Tree[from].nodes.size() >= 3 ? 1 : 0; Branch[to] = Tree[to].nodes.size() >= 3 ? 1 : 0; anyBranch |= Branch[from] | Branch[to]; } bool equal = true; for(int i = 1; i <= N; i++) if(Current[i] != Target[i]) equal = false; if(equal) printf("TAK\n"); else if(currentAnyOne == false && targetAnyOne == true) printf("NIE\n"); else if(currentAnyZero == false && targetAnyZero == true) printf("NIE\n"); else if(anyBranch == false) ProcessLinear(); else ProcessTree(); } 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 138 139 140 141 142 143 144 145 146 147 148 | #include <stdio.h> #include <vector> int Current[200000]; int Target[200000]; struct Node { std::vector<int> nodes; }; Node Tree[200000]; int Branch[200000]; int N; int GetLeaf() { for(int i=1; i<=N; i++) { if(Tree[i].nodes.size() <= 1) return i; } return -1; } int GetNext(int node, int prevNode) { auto it = Tree[node].nodes.begin(); if(it == Tree[node].nodes.end()) return 0; if(*it != prevNode) return *it; ++it; if(it == Tree[node].nodes.end()) return 0; return *it; } void ProcessLinear() { int starter = GetLeaf(); int changes1 = 0; int changes2 = 0; int prev1 = Current[starter]; int prev2 = Target[starter]; int node = GetNext(starter, -1); int prevNode = starter; while(node > 0) { if(prev1 != Current[node]) changes1++; if(prev2 != Target[node]) changes2++; prev1 = Current[node]; prev2 = Target[node]; int next = GetNext(node, prevNode); prevNode = node; node = next; } if(changes1 > changes2) { printf("TAK\n"); return; } if(changes1 == changes2 && Current[starter] == Target[starter]) { printf("TAK\n"); return; } printf("NIE\n"); } bool IsBranch(int node) { return Tree[node].nodes.size() >= 3; } bool DFS(int node, int prevNode) { if(Target[node] == Target[prevNode]) return true; //go straight while(IsBranch(node) == false) { int next = GetNext(node, prevNode); if(next == 0) return false; prevNode = node; node = next; if(Target[node] == Target[prevNode]) return true; } //go DFS for(auto next : Tree[node].nodes) { if(next == prevNode) continue; if(DFS(next, node)) return true; } return false; } void ProcessTree() { int node = GetLeaf(); int next = GetNext(node, -1); if(DFS(next, node)) printf("TAK\n"); else printf("NIE\n"); } int main() { int tc; scanf("%d ", &tc); for(int i=0; i<tc; i++) { scanf("%d ", &N); bool currentAnyZero = false; bool currentAnyOne = false; for(int i = 1; i <= N; i++) { scanf("%1d ", &Current[i]); currentAnyZero = Current[i] == 0 ? true : currentAnyZero; currentAnyOne = Current[i] == 1 ? true : currentAnyOne; Tree[i].nodes.clear(); Branch[i] = 0; } bool targetAnyZero = false; bool targetAnyOne = false; for(int i = 1; i <= N; i++) { scanf("%1d ", &Target[i]); targetAnyZero = Target[i] == 0 ? true : targetAnyZero; targetAnyOne = Target[i] == 1 ? true : targetAnyOne; } bool anyBranch = false; for(int i=1; i<N; i++) { int from, to; scanf("%d %d ", &from, &to); Tree[from].nodes.push_back(to); Tree[to].nodes.push_back(from); Branch[from] = Tree[from].nodes.size() >= 3 ? 1 : 0; Branch[to] = Tree[to].nodes.size() >= 3 ? 1 : 0; anyBranch |= Branch[from] | Branch[to]; } bool equal = true; for(int i = 1; i <= N; i++) if(Current[i] != Target[i]) equal = false; if(equal) printf("TAK\n"); else if(currentAnyOne == false && targetAnyOne == true) printf("NIE\n"); else if(currentAnyZero == false && targetAnyZero == true) printf("NIE\n"); else if(anyBranch == false) ProcessLinear(); else ProcessTree(); } return 0; } |