#include <iostream> #include <cstdint> #include <vector> #include <algorithm> #include <iterator> #include <string> #include <numeric> #include <set> #include <tuple> using namespace std; vector <vector<int> > E; string in, out; int n; bool ma_oba(string s){ bool ma_1 = false; bool ma_0 = false; for (auto c:s){ if (c=='0') ma_0=true; else ma_1 = true; } return ma_0 && ma_1; } bool linia(int v){ int old_v = v; vector <char> in_s, out_s; in_s.push_back(in[v]); out_s.push_back(out[v]); do{ if (E[v][0]!=old_v){ old_v = v; v = E[v][0]; }else{ old_v = v; v = E[v][1]; } in_s.push_back(in[v]); out_s.push_back(out[v]); }while(E[v].size()>=2); in_s.erase(unique(begin(in_s),end(in_s)),end(in_s)); out_s.erase(unique(begin(out_s),end(out_s)),end(out_s)); return ( in_s.size() > out_s.size() || ( in_s.size() == out_s.size() && in_s[0]==out_s[0] ) ); } bool wierzcholki(){ bool wierzcholek3p=false; bool para = false; int lisc; for (int i=0; i<E.size(); i++){ if (E[i].size() > 2) wierzcholek3p = true; if (E[i].size() == 1) lisc=i; for (int j=0; j<E[i].size(); j++){ if (out[i] == out[E[i][j]] ) para = true; } } if (!para) return false; else if ( wierzcholek3p) return true; else //para && ! wierzcholek return linia(lisc); } int main() { int t; cin>>t; for (int test=0; test<t; test++){ cin>>n; cin>>in>>out; //E.erase(); //E.resize(n); E = vector <vector<int> >(n); for (int i=0; i<n-1; i++){ int a,b; cin>>a>>b; a--; b--; E[a].push_back(b); E[b].push_back(a); } if (in==out){ //identyczne drzewa, nie ma co robić; cout<<"TAK"<<endl; continue; } if (! ma_oba(out)){ if (ma_oba(in)) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; continue; }else{ if (!ma_oba(in)) cout<<"NIE"<<endl; else{ if (wierzcholki()) 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 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 | #include <iostream> #include <cstdint> #include <vector> #include <algorithm> #include <iterator> #include <string> #include <numeric> #include <set> #include <tuple> using namespace std; vector <vector<int> > E; string in, out; int n; bool ma_oba(string s){ bool ma_1 = false; bool ma_0 = false; for (auto c:s){ if (c=='0') ma_0=true; else ma_1 = true; } return ma_0 && ma_1; } bool linia(int v){ int old_v = v; vector <char> in_s, out_s; in_s.push_back(in[v]); out_s.push_back(out[v]); do{ if (E[v][0]!=old_v){ old_v = v; v = E[v][0]; }else{ old_v = v; v = E[v][1]; } in_s.push_back(in[v]); out_s.push_back(out[v]); }while(E[v].size()>=2); in_s.erase(unique(begin(in_s),end(in_s)),end(in_s)); out_s.erase(unique(begin(out_s),end(out_s)),end(out_s)); return ( in_s.size() > out_s.size() || ( in_s.size() == out_s.size() && in_s[0]==out_s[0] ) ); } bool wierzcholki(){ bool wierzcholek3p=false; bool para = false; int lisc; for (int i=0; i<E.size(); i++){ if (E[i].size() > 2) wierzcholek3p = true; if (E[i].size() == 1) lisc=i; for (int j=0; j<E[i].size(); j++){ if (out[i] == out[E[i][j]] ) para = true; } } if (!para) return false; else if ( wierzcholek3p) return true; else //para && ! wierzcholek return linia(lisc); } int main() { int t; cin>>t; for (int test=0; test<t; test++){ cin>>n; cin>>in>>out; //E.erase(); //E.resize(n); E = vector <vector<int> >(n); for (int i=0; i<n-1; i++){ int a,b; cin>>a>>b; a--; b--; E[a].push_back(b); E[b].push_back(a); } if (in==out){ //identyczne drzewa, nie ma co robić; cout<<"TAK"<<endl; continue; } if (! ma_oba(out)){ if (ma_oba(in)) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; continue; }else{ if (!ma_oba(in)) cout<<"NIE"<<endl; else{ if (wierzcholki()) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } } } return 0; } |