This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <vector>
using namespace std;
set < int > a,b;
set < pair < int , int > > road;
int now=0;
vector < int > Next[500005];
int color[500005];
int deg[500005]={0};
int tt[500005]={0};
int Father[500005]={0};
vector < int > con[500005];
vector < int > use[500005];
int sum[500005]={0};
int M;
void F(int here,int fa,int x)
{
int i,j;
con[here][color[here]]++;
sum[color[here]]++;
for(auto i:Next[here])
{
if(i==fa) continue;
F(i,here,x);
for(j=1;j<=M;j++) con[here][j]+=con[i][j];
}
}
void F3(int here,int fa,int x)
{
int ok=1,i,j;
for(auto i:Next[here])
{
ok=1;
if(i==fa) continue;
F3(i,here,x);
for(j=1;j<=M;j++)
{
if(con[i][j]>0&&con[i][j]<sum[j])
{
//printf("%d %d %d %d %d\n",here,i,j,con[i][j],sum[j]);
ok=0;
}
}
if(ok)
{
//printf("%d %d\n",here,i);
road.insert(make_pair(here,i));
road.insert(make_pair(i,here));
}
}
}
void F2(int here,int fa,int xx)
{
for(auto i:Next[here])
{
if(i==fa) continue;
if(road.find(make_pair(here,i))!=road.end())
{
now++;
deg[xx]++;
deg[now]++;
//printf("%d %d %d %d\n",here,i,deg[xx],deg[now]);
F2(i,here,now);
}
else F2(i,here,xx);
}
}
void F4(int here,int fa,int deg)
{
tt[here]=deg;
Father[here]=fa;
for(auto i:Next[here])
{
if(i!=fa)
{
road.insert(make_pair(here,i));
F4(i,here,deg+1);
}
}
}
int LCA(int a,int b)
{
if(a==b) return a;
int x,y;
if(tt[a]==tt[b])
{
x=Father[a];
y=Father[b];
//printf("aa %d %d\n",x,a);
//printf("bb %d %d\n",y,b);
if(road.find(make_pair(x,a))!=road.end()) road.erase(make_pair(x,a));
if(road.find(make_pair(y,b))!=road.end()) road.erase(make_pair(y,b));
return LCA(x,y);
}
else
{
if(tt[a]<tt[b]) swap(a,b);
x=Father[a];
//printf("cc %d %d\n",x,a);
if(road.find(make_pair(x,a))!=road.end()) road.erase(make_pair(x,a));
return LCA(x,b);
}
}
int main()
{
int N,K,i,j,x,y,xx=0,ok=1;
scanf("%d %d",&N,&M);
for(i=1;i<N;i++)
{
scanf("%d %d",&x,&y);
Next[x].push_back(y);
Next[y].push_back(x);
}
for(i=1;i<=N;i++)
{
scanf("%d",&color[i]);
use[color[i]].push_back(i);
}
F4(1,-1,0);
for(i=1;i<=M;i++)
{
K=use[i].size();
if(K>=2)
{
x=use[i][0];
for(j=1;j<K;j++) x=LCA(x,use[i][j]);
}
}
/*if(N<=3000||M<=50)
{
for(i=1;i<=N;i++)
{
//scanf("%d",&color[i]);
for(j=0;j<=M;j++) con[i].push_back(0);
}
F(1,-1,0);
F3(1,-1,0);
}
else*/
now=0;
F2(1,-1,0);
for(i=0;i<=now;i++) if(deg[i]==1) xx++;
if(xx==1) printf("0\n");
else printf("%d\n",(xx+1)/2);
return 0;
}
Compilation message (stderr)
mergers.cpp: In function 'void F(int, int, int)':
mergers.cpp:22:9: warning: unused variable 'i' [-Wunused-variable]
22 | int i,j;
| ^
mergers.cpp: In function 'void F3(int, int, int)':
mergers.cpp:34:14: warning: unused variable 'i' [-Wunused-variable]
34 | int ok=1,i,j;
| ^
mergers.cpp: In function 'int main()':
mergers.cpp:112:26: warning: unused variable 'ok' [-Wunused-variable]
112 | int N,K,i,j,x,y,xx=0,ok=1;
| ^~
mergers.cpp:113:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
113 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
116 | scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
123 | scanf("%d",&color[i]);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |