-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmorosoph.cpp
More file actions
74 lines (70 loc) · 1.39 KB
/
Copy pathmorosoph.cpp
File metadata and controls
74 lines (70 loc) · 1.39 KB
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
#include "bits/stdc++.h"
#define l long
#define ll long long
#define vl vector<long>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pb(a) push_back(a)
#define fri(a,b) for(l i=a;i<b;i++)
#define frj(a,b) for(l j=a;j<b;j++)
#define frii(a,b) for(l i=a;i<=b;i++)
#define frjj(a,b) for(l j=a;j<=b;j++)
#define max 500005
using namespace std;
vector<int>vec[max];
vector<ll>value(max);
vector<l>xormin(max);
vector<l>xormax(max);
vector<bool>isleaf(max,false);
void dfs(int node,int par,l xmax,l xmin)
{
xormax[node]=xmax^(1<<value[node]);
xormin[node]=xmin^(1<<(value[node]-1));
for(auto child:vec[node])
{
if(child!=par)
dfs(child,node,xormax[node],xormin[node]);
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
l n;
ll k;
cin>>n>>k;
fri(0,n-1)
{
int u,v;
cin>>u>>v;
vec[u].pb(v);
vec[v].pb(u);
}
fri(1,n+1)
cin>>value[i];
dfs(1,-1,0,0);
fri(2,n+1)
{
if(vec[i].size()==1)
isleaf[i]=true;
}
bool flag=false;
fri(1,n+1)
{
if(isleaf[i])
{
if(k>=xormin[i] && k<=xormax[i])
{ //cout<<"possible for leaf:"<<i<<endl;
flag=true;break;}
}
}
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
/*fri(1,n+1)
cout<<"i:"<<i<<" xormax:"<<xormax[i]<<" xormin:"<<xormin[i]<<endl;*/
}
return 0;
}