#include <bits/stdc++.h>
using namespace std;
int n = 0, a, b;
const int MAX = 100005;
vector<int> tab[MAX];
string sp, sk;
bool czy = false;
queue<string> kol;
set<string> bylo;
void wyczysc()
{
for (int i = 0; i <= n; i++)
tab[i].clear();
while (!kol.empty())
kol.pop();
czy = false;
bylo.clear();
}
void dodaj(string x)
{
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < tab[i].size(); j++)
{
string g = x;
g[tab[i][j]] = x[i];
if (bylo.find(g) == bylo.end())
{
bylo.insert(g);
kol.push(g);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int l = 0; l < t; l++)
{
wyczysc();
cin >> n;
cin >> sp >> sk;
sp = " " + sp;
sk = " " + sk;
for (int i = 0; i < n - 1; i++)
{
cin >> a >> b;
tab[a].push_back(b);
tab[b].push_back(a);
}
if (sp == sk)
{
cout << "TAK\n";
continue;
}
bylo.insert(sp);
dodaj(sp);
while (!kol.empty())
{
string s = kol.front();
kol.pop();
if (s == sk)
{
czy = true;
break;
}
dodaj(s);
}
if (czy)
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 | #include <bits/stdc++.h> using namespace std; int n = 0, a, b; const int MAX = 100005; vector<int> tab[MAX]; string sp, sk; bool czy = false; queue<string> kol; set<string> bylo; void wyczysc() { for (int i = 0; i <= n; i++) tab[i].clear(); while (!kol.empty()) kol.pop(); czy = false; bylo.clear(); } void dodaj(string x) { for (int i = 1; i <= n; i++) { for (int j = 0; j < tab[i].size(); j++) { string g = x; g[tab[i][j]] = x[i]; if (bylo.find(g) == bylo.end()) { bylo.insert(g); kol.push(g); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int l = 0; l < t; l++) { wyczysc(); cin >> n; cin >> sp >> sk; sp = " " + sp; sk = " " + sk; for (int i = 0; i < n - 1; i++) { cin >> a >> b; tab[a].push_back(b); tab[b].push_back(a); } if (sp == sk) { cout << "TAK\n"; continue; } bylo.insert(sp); dodaj(sp); while (!kol.empty()) { string s = kol.front(); kol.pop(); if (s == sk) { czy = true; break; } dodaj(s); } if (czy) cout << "TAK\n"; else cout << "NIE\n"; } return 0; } |
English