#include <cstdio>
#include <vector>
#define MAKS 100010
using namespace std;
char start[MAKS];
char cel[MAKS];
vector<int> sas[MAKS];
bool bylo[MAKS];
bool f[MAKS][2];
void licz(int v)
{
bylo[v]=true;
int start_v=start[v]-'0';
bool porazka=false;
int potrzeba[2]={0, 0};
int mamy[2]={0, 0};
int mamyplus[2]={0, 0};
for(int i=0;i<sas[v].size();i++)
{
int u=sas[v][i];
int cel_u=cel[u]-'0';
if(bylo[u])continue;
licz(u);
if((!f[u][0]) && (!f[u][1]))
{
porazka=true;
continue;
}
if(f[u][0] && (!f[u][1]) && cel_u==0)mamy[0]++;
if(f[u][1] && (!f[u][0]) && cel_u==1)mamy[1]++;
if(f[u][0] && f[u][1] && cel_u==0)mamyplus[0]++;
if(f[u][0] && f[u][1] && cel_u==1)mamyplus[1]++;
if(f[u][0] && (!f[u][1]) && cel_u==1)potrzeba[1]++;
if(f[u][1] && (!f[u][0]) && cel_u==0)potrzeba[0]++;
}
f[v][0]=false;
f[v][1]=false;
if(porazka)return;
// AAAAAAAAAAAAAAA
if( (potrzeba[1-start_v]==0) ||
(mamy[1-start_v]>0 && (potrzeba[start_v]>0 || mamy[start_v]>0 || mamyplus[start_v]>0)) ||
(potrzeba[start_v]==1 && (mamy[start_v]>0 || mamyplus[start_v]>0)) ||
(potrzeba[start_v]>=2) ||
(mamyplus[start_v]==1 && (potrzeba[start_v]>0 || mamy[start_v]>0)) ||
(mamyplus[start_v]>=2) )
f[v][start_v] = true;
if(mamy[1-start_v]>0 || mamyplus[1-start_v]>0) f[v][1-start_v]=true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d %s %s",&n,start,cel);
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d %d",&a,&b);
a--; b--;
sas[a].push_back(b);
sas[b].push_back(a);
}
licz(0);
if(f[0][cel[0]-'0'])puts("TAK");
else puts("NIE");
// --------
for(int i=0;i<n;i++)sas[i].clear();
for(int i=0;i<n;i++)bylo[i]=false;
}
}
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 | #include <cstdio> #include <vector> #define MAKS 100010 using namespace std; char start[MAKS]; char cel[MAKS]; vector<int> sas[MAKS]; bool bylo[MAKS]; bool f[MAKS][2]; void licz(int v) { bylo[v]=true; int start_v=start[v]-'0'; bool porazka=false; int potrzeba[2]={0, 0}; int mamy[2]={0, 0}; int mamyplus[2]={0, 0}; for(int i=0;i<sas[v].size();i++) { int u=sas[v][i]; int cel_u=cel[u]-'0'; if(bylo[u])continue; licz(u); if((!f[u][0]) && (!f[u][1])) { porazka=true; continue; } if(f[u][0] && (!f[u][1]) && cel_u==0)mamy[0]++; if(f[u][1] && (!f[u][0]) && cel_u==1)mamy[1]++; if(f[u][0] && f[u][1] && cel_u==0)mamyplus[0]++; if(f[u][0] && f[u][1] && cel_u==1)mamyplus[1]++; if(f[u][0] && (!f[u][1]) && cel_u==1)potrzeba[1]++; if(f[u][1] && (!f[u][0]) && cel_u==0)potrzeba[0]++; } f[v][0]=false; f[v][1]=false; if(porazka)return; // AAAAAAAAAAAAAAA if( (potrzeba[1-start_v]==0) || (mamy[1-start_v]>0 && (potrzeba[start_v]>0 || mamy[start_v]>0 || mamyplus[start_v]>0)) || (potrzeba[start_v]==1 && (mamy[start_v]>0 || mamyplus[start_v]>0)) || (potrzeba[start_v]>=2) || (mamyplus[start_v]==1 && (potrzeba[start_v]>0 || mamy[start_v]>0)) || (mamyplus[start_v]>=2) ) f[v][start_v] = true; if(mamy[1-start_v]>0 || mamyplus[1-start_v]>0) f[v][1-start_v]=true; } int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d %s %s",&n,start,cel); for(int i=0;i<n-1;i++) { int a,b; scanf("%d %d",&a,&b); a--; b--; sas[a].push_back(b); sas[b].push_back(a); } licz(0); if(f[0][cel[0]-'0'])puts("TAK"); else puts("NIE"); // -------- for(int i=0;i<n;i++)sas[i].clear(); for(int i=0;i<n;i++)bylo[i]=false; } } |
English