/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define FOR(x, b, e) for(long x = b; x <= (e); x++) #define FORD(x, b, e) for(long x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) #define LL long long using namespace std; long n; class Node; class Node { public: long num; long neiC; vector<long> nei; long col[2]; // col[0]-poczatkowy, col[1]-koncowy // 0-czerwone, 1-czarne void reset() { neiC=0; nei.clear(); } void addNeighbour(long num) { neiC++; nei.push_back(num);} bool isFunny(); }; Node nodes[510000]; long maxDegree() { long md=0; FOR(i,1,n) if (nodes[i].neiC>md) md=nodes[i].neiC; return md; } /// mozliwe odpowiedzi: /// 0 - nie zachodzi oczywisty przypadek /// 1 - przypadek oczywisty, odpowiedz TAK /// 2 - przypadek oczywisty, odpowiedz NIE long obviousCase() { long initially0=0; long initially1=0; long finally0=0; long finally1=0; FOR(i,1,n) if (nodes[i].col[0]==0) initially0++; else initially1++; FOR(i,1,n) if (nodes[i].col[1]==0) finally0++; else finally1++; if (initially0==n) { // wszystkie czerwone na poczatku if (finally0==n) return 1; // jest OK else return 2; // nie da sie } else if (initially1==n) { // wszystkie czarne na poczatku if (finally1==n) return 1; // jest OK else return 2; // nie da sie } return 0; // przypadek nie jest oczywisty } long anyLeaf() { FOR(i,1,n) if (nodes[i].neiC==1) return i; } long countChanges(long leaf, long which) { //cout << "Counting changes in " << which << " from leaf " << leaf << "\n"; long cur=nodes[leaf].nei[0]; long pop=leaf; long changes=0; if (nodes[leaf].col[which]!=nodes[cur].col[which]) changes=1; long next; while (nodes[cur].neiC>1) { next=nodes[cur].nei[0]; if (next==pop) next=nodes[cur].nei[1]; pop=cur; cur=next; if (nodes[pop].col[which]!=nodes[cur].col[which]) changes++; } //cout << " changes=" << changes << "\n"; return changes; } bool okInSimpleTree() { // maxDegree is 2 long leaf=anyLeaf(); long ch1=countChanges(leaf,0); long ch2=countChanges(leaf,1); if (ch1<ch2) return false; else if (ch1>ch2) return true; else return (nodes[leaf].col[0]==nodes[leaf].col[1]); } bool isBiparted() { FOR(i,1,n) FOREACH (p,nodes[i].nei) if (nodes[i].col[1]==nodes[*p].col[1]) return false; return true; } bool areTheSame() { FOR(i,1,n) if (nodes[i].col[0]!=nodes[i].col[1]) return false; return true; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); long t; cin >>t; string s1, s2; long a,b; bool ans; FOR(pt,1,t) { cin >> n; cin >> s1; cin >> s2; //cout << "s1=" << s1 << "#\n"; //cout << "s2=" << s2 << "#\n"; //cout << "Tree number " << t << " n=" << n << "\n"; FOR(i,1,n) { nodes[i].col[0]=(s1[i-1]=='1'); nodes[i].col[1]=(s2[i-1]=='1'); nodes[i].reset(); nodes[i].num=i; } FOR(i,1,n-1) { cin >>a; cin >>b; nodes[a].addNeighbour(b); nodes[b].addNeighbour(a); } long oc=obviousCase(); //cout << "oc=" << oc << "\n"; if (oc==1) ans=true; else if (oc==2) ans=false; else { long md=maxDegree(); //cout << "maxDegree=" << md << "\n"; if (md<=2) ans=okInSimpleTree(); else { bool isBi=isBiparted(); if (isBi) { ans=areTheSame(); } //cout << "areTheSame=" << ans << "\n"; } else ans=true; } } if (ans) 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 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 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define FOR(x, b, e) for(long x = b; x <= (e); x++) #define FORD(x, b, e) for(long x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) #define LL long long using namespace std; long n; class Node; class Node { public: long num; long neiC; vector<long> nei; long col[2]; // col[0]-poczatkowy, col[1]-koncowy // 0-czerwone, 1-czarne void reset() { neiC=0; nei.clear(); } void addNeighbour(long num) { neiC++; nei.push_back(num);} bool isFunny(); }; Node nodes[510000]; long maxDegree() { long md=0; FOR(i,1,n) if (nodes[i].neiC>md) md=nodes[i].neiC; return md; } /// mozliwe odpowiedzi: /// 0 - nie zachodzi oczywisty przypadek /// 1 - przypadek oczywisty, odpowiedz TAK /// 2 - przypadek oczywisty, odpowiedz NIE long obviousCase() { long initially0=0; long initially1=0; long finally0=0; long finally1=0; FOR(i,1,n) if (nodes[i].col[0]==0) initially0++; else initially1++; FOR(i,1,n) if (nodes[i].col[1]==0) finally0++; else finally1++; if (initially0==n) { // wszystkie czerwone na poczatku if (finally0==n) return 1; // jest OK else return 2; // nie da sie } else if (initially1==n) { // wszystkie czarne na poczatku if (finally1==n) return 1; // jest OK else return 2; // nie da sie } return 0; // przypadek nie jest oczywisty } long anyLeaf() { FOR(i,1,n) if (nodes[i].neiC==1) return i; } long countChanges(long leaf, long which) { //cout << "Counting changes in " << which << " from leaf " << leaf << "\n"; long cur=nodes[leaf].nei[0]; long pop=leaf; long changes=0; if (nodes[leaf].col[which]!=nodes[cur].col[which]) changes=1; long next; while (nodes[cur].neiC>1) { next=nodes[cur].nei[0]; if (next==pop) next=nodes[cur].nei[1]; pop=cur; cur=next; if (nodes[pop].col[which]!=nodes[cur].col[which]) changes++; } //cout << " changes=" << changes << "\n"; return changes; } bool okInSimpleTree() { // maxDegree is 2 long leaf=anyLeaf(); long ch1=countChanges(leaf,0); long ch2=countChanges(leaf,1); if (ch1<ch2) return false; else if (ch1>ch2) return true; else return (nodes[leaf].col[0]==nodes[leaf].col[1]); } bool isBiparted() { FOR(i,1,n) FOREACH (p,nodes[i].nei) if (nodes[i].col[1]==nodes[*p].col[1]) return false; return true; } bool areTheSame() { FOR(i,1,n) if (nodes[i].col[0]!=nodes[i].col[1]) return false; return true; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); long t; cin >>t; string s1, s2; long a,b; bool ans; FOR(pt,1,t) { cin >> n; cin >> s1; cin >> s2; //cout << "s1=" << s1 << "#\n"; //cout << "s2=" << s2 << "#\n"; //cout << "Tree number " << t << " n=" << n << "\n"; FOR(i,1,n) { nodes[i].col[0]=(s1[i-1]=='1'); nodes[i].col[1]=(s2[i-1]=='1'); nodes[i].reset(); nodes[i].num=i; } FOR(i,1,n-1) { cin >>a; cin >>b; nodes[a].addNeighbour(b); nodes[b].addNeighbour(a); } long oc=obviousCase(); //cout << "oc=" << oc << "\n"; if (oc==1) ans=true; else if (oc==2) ans=false; else { long md=maxDegree(); //cout << "maxDegree=" << md << "\n"; if (md<=2) ans=okInSimpleTree(); else { bool isBi=isBiparted(); if (isBi) { ans=areTheSame(); } //cout << "areTheSame=" << ans << "\n"; } else ans=true; } } if (ans) cout << "TAK\n"; else cout << "NIE\n"; } return 0; }; |