#include <iostream>
#include <fstream>
#include <map>
#include <vector>
using namespace std;
const int nmax=500005;
map<int,int> ap[nmax];
vector<int> v[nmax];
int l[nmax],r[nmax],p[nmax],closed[nmax],w[nmax],sons[nmax],rele[nmax],tot[nmax],col[nmax],c[nmax],tt[nmax];
int nr,n,i,paths,k,x,y,frz;
void baga(int path,int cc)
{
ap[path][cc]++;
if(ap[path][cc]==1) rele[path]++;
if(ap[path][cc]==tot[cc]) rele[path]--;
}
void dfs(int x)
{
int big=0;
w[x]=1;
++nr;l[x]=nr;col[nr]=c[x];
for(int i=0;i<v[x].size();i++)
if(!w[v[x][i]])
{
tt[v[x][i]]=x;
dfs(v[x][i]);
if(w[v[x][i]]>w[big])
big=v[x][i];
w[x]+=w[v[x][i]];
}
if(big&&(!closed[big])) p[x]=p[big];
else
{
p[x]=++paths;
if(big) sons[paths]++;
}
baga(p[x],c[x]);
for(int i=0;i<v[x].size();i++)
if(v[x][i]!=big&&v[x][i]!=tt[x])
{
if(closed[v[x][i]]) sons[p[x]]++;
else
{
for(int j=l[v[x][i]];j<=r[v[x][i]];j++)
baga(p[x],col[j]);
}
}
if(rele[p[x]]==0)
{
closed[x]=1;
if(sons[p[x]]==0) frz++;
}
r[x]=nr;
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>k;
for(i=1;i<=n-1;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(i=1;i<=n;i++)
{
cin>>c[i];
tot[c[i]]++;
}
dfs(1);
if(sons[p[1]]==1) frz++;
cout<<(frz+1)/2;
return 0;
}
Compilation message
mergers.cpp: In function 'void dfs(int)':
mergers.cpp:22:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
mergers.cpp:38:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35704 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
34 ms |
35704 KB |
Output is correct |
4 |
Correct |
34 ms |
35580 KB |
Output is correct |
5 |
Incorrect |
34 ms |
35576 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35704 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
34 ms |
35704 KB |
Output is correct |
4 |
Correct |
34 ms |
35580 KB |
Output is correct |
5 |
Incorrect |
34 ms |
35576 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35704 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
34 ms |
35704 KB |
Output is correct |
4 |
Correct |
34 ms |
35580 KB |
Output is correct |
5 |
Incorrect |
34 ms |
35576 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
138 ms |
46984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35704 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
34 ms |
35704 KB |
Output is correct |
4 |
Correct |
34 ms |
35580 KB |
Output is correct |
5 |
Incorrect |
34 ms |
35576 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |