#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+6;
vector<int>g[MAXN];
int n,k;
int c[MAXN],cnt[MAXN];
bool can0=1;
int subtree[MAXN];
void dfs1(int u,int p)
{
subtree[u]=1;
for(auto xd:g[u])
{
if(xd==p)continue;
dfs1(xd,u);
subtree[u]+=subtree[xd];
}
}
void dfs(int u,int p,map<int,int> &mp)
{
int maxst=0,bch=-1;
for(auto xd:g[u])
{
if(xd==p)continue;
if(subtree[xd]>maxst)
{
maxst=subtree[xd];
bch=xd;
}
}
if(bch!=-1)
{
dfs(bch,u,mp);
for(auto xd:g[u])
{
if(xd==p)continue;
if(xd==bch)continue;
map<int,int>tmp;
dfs(xd,u,tmp);
for(auto dx:tmp)
{
mp[dx.first]+=dx.second;
if(mp[dx.first]==cnt[dx.first])mp.erase(dx.first);
}
}
}
mp[c[u]]++;
if(mp[c[u]]==cnt[c[u]])mp.erase(c[u]);
if(mp.size()==0&&p!=0)
{
//cout<<"** "<<u<<endl;
can0=0;
}
}
int main()
{
cin>>n>>k;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
cin>>c[i];
cnt[c[i]]++;
}
map<int,int>mp;
dfs1(1,0);
dfs(1,0,mp);
if(can0)cout<<1<<endl;
else cout<<0<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
121 ms |
9212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |