#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool przypadek() {
int n;
cin >> n;
string przed,po;
cin >> przed;
cin >> po;
int x,y;
vector<int> krawedzi(n, 0);
int zmiany_przed = 0, zmiany_po = 0;
for(int i = 0; i < n-1; i++) {
cin >> x >> y;
krawedzi[x-1]++;
krawedzi[y-1]++;
if( przed[x-1] != przed[y-1] )
zmiany_przed++;
if( po[x-1] != po[y-1] )
zmiany_po++;
}
if(przed == po)
return true;
if(zmiany_po == n-1)
return false;
bool kolory_przed[2], kolory_po[2];
for(int i = 0; i < 2; i++)
kolory_przed[i] = kolory_po[i] = false;
for(int i = 0; i < przed.length(); i++)
kolory_przed[ przed[i]-'0' ] = true;
for(int i = 0; i < po.length(); i++)
kolory_po[ po[i]-'0' ] = true;
if(kolory_po[0] && !kolory_przed[0])
return false;
if(kolory_po[1] && !kolory_przed[1])
return false;
if(!kolory_po[0] || !kolory_po[1])
return true;
for(int i = 0 ; i < n; i++)
if(krawedzi[i] > 2)
return true;
if(zmiany_po < zmiany_przed)
return true;
if(zmiany_po > zmiany_przed)
return false;
for(int i = 0; i < n; i++)
if(krawedzi[i] == 1)
return przed[i]==po[i];
}
int main() {
int t;
cin >> t;
for(int it = 0; it < t; it++)
if(przypadek())
cout << "TAK" << endl;
else
cout << "NIE" << endl;
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; bool przypadek() { int n; cin >> n; string przed,po; cin >> przed; cin >> po; int x,y; vector<int> krawedzi(n, 0); int zmiany_przed = 0, zmiany_po = 0; for(int i = 0; i < n-1; i++) { cin >> x >> y; krawedzi[x-1]++; krawedzi[y-1]++; if( przed[x-1] != przed[y-1] ) zmiany_przed++; if( po[x-1] != po[y-1] ) zmiany_po++; } if(przed == po) return true; if(zmiany_po == n-1) return false; bool kolory_przed[2], kolory_po[2]; for(int i = 0; i < 2; i++) kolory_przed[i] = kolory_po[i] = false; for(int i = 0; i < przed.length(); i++) kolory_przed[ przed[i]-'0' ] = true; for(int i = 0; i < po.length(); i++) kolory_po[ po[i]-'0' ] = true; if(kolory_po[0] && !kolory_przed[0]) return false; if(kolory_po[1] && !kolory_przed[1]) return false; if(!kolory_po[0] || !kolory_po[1]) return true; for(int i = 0 ; i < n; i++) if(krawedzi[i] > 2) return true; if(zmiany_po < zmiany_przed) return true; if(zmiany_po > zmiany_przed) return false; for(int i = 0; i < n; i++) if(krawedzi[i] == 1) return przed[i]==po[i]; } int main() { int t; cin >> t; for(int it = 0; it < t; it++) if(przypadek()) cout << "TAK" << endl; else cout << "NIE" << endl; return 0; } |
English