#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=5e5+50;
const ll mod=1e9+7;
using namespace std;
int x,y,i,n,up[nmax][20],tin[nmax],tout[nmax],tt,lc[nmax],sz[nmax],pr[nmax],deg[nmax],rs,j,lvl[nmax],c,k;
vector<int>a[nmax],vc[nmax];
void dfs(int x,int p)
{
tin[x]=++tt;
up[x][0]=p;
lvl[x]=lvl[p]+1;
for(int i=1;i<20;i++)up[x][i]=up[up[x][i-1]][i-1];
for(int i=0;i<(int)a[x].size();i++)if(a[x][i]!=p)dfs(a[x][i],x);
tout[x]=++tt;
}
int anc(int x,int y)
{
return tin[x]<=tin[y] && tout[x]>=tout[y];
}
int lca(int x,int y)
{
if(anc(x,y))return x;
if(anc(y,x))return y;
for(int i=19;i>=0;i--)if(!anc(up[x][i],y))x=up[x][i];
return up[x][0];
}
int fnd(int x)
{
if(pr[x]==x)return x;
return pr[x]=fnd(pr[x]);
}
void uni(int x,int y)
{
x=fnd(x),y=fnd(y);
if(x==y)return;
if(sz[x]<sz[y])swap(x,y);
pr[y]=x;
sz[x]+=sz[y];
}
void bld(int x,int p)
{
for(int i=0;i<(int)a[x].size();i++)
{
int y=a[x][i];
if(y==p)continue;
bld(y,x);
}
lc[p]=max(lc[p],lc[x]-1);
if(lc[x])uni(p,x);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>k;
for(i=1;i<n;i++)
{
cin>>x>>y;
a[x].pb(y);
a[y].pb(x);
}
dfs(1,1);
for(i=1;i<=n;i++)
{
cin>>c;
pr[i]=i;
sz[i]=1;
vc[c].pb(i);
}
for(i=1;i<=k;i++)
{
if(vc[i].empty())continue;
x=vc[i][0];
for(j=1;j<(int)vc[i].size();j++)x=lca(x,vc[i][j]);
for(j=0;j<(int)vc[i].size();j++)lc[vc[i][j]]=lvl[vc[i][j]]-lvl[x];
}
bld(1,1);
for(i=2;i<=n;i++)
{
if(lc[i])continue;
deg[fnd(i)]++;
deg[fnd(up[i][0])]++;
}
for(i=1;i<=n;i++)if(deg[i]==1)rs++;
cout<<(rs+1)/2<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23856 KB |
Output is correct |
3 |
Correct |
19 ms |
23928 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
19 ms |
23936 KB |
Output is correct |
6 |
Correct |
19 ms |
23936 KB |
Output is correct |
7 |
Correct |
19 ms |
23936 KB |
Output is correct |
8 |
Correct |
19 ms |
23936 KB |
Output is correct |
9 |
Correct |
19 ms |
23936 KB |
Output is correct |
10 |
Correct |
20 ms |
23936 KB |
Output is correct |
11 |
Correct |
19 ms |
23936 KB |
Output is correct |
12 |
Correct |
19 ms |
23936 KB |
Output is correct |
13 |
Correct |
19 ms |
23936 KB |
Output is correct |
14 |
Correct |
20 ms |
23936 KB |
Output is correct |
15 |
Correct |
19 ms |
23936 KB |
Output is correct |
16 |
Correct |
20 ms |
23936 KB |
Output is correct |
17 |
Correct |
19 ms |
23936 KB |
Output is correct |
18 |
Correct |
19 ms |
23928 KB |
Output is correct |
19 |
Correct |
20 ms |
23936 KB |
Output is correct |
20 |
Correct |
19 ms |
23936 KB |
Output is correct |
21 |
Correct |
20 ms |
23936 KB |
Output is correct |
22 |
Correct |
19 ms |
23936 KB |
Output is correct |
23 |
Correct |
19 ms |
23936 KB |
Output is correct |
24 |
Correct |
20 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23856 KB |
Output is correct |
3 |
Correct |
19 ms |
23928 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
19 ms |
23936 KB |
Output is correct |
6 |
Correct |
19 ms |
23936 KB |
Output is correct |
7 |
Correct |
19 ms |
23936 KB |
Output is correct |
8 |
Correct |
19 ms |
23936 KB |
Output is correct |
9 |
Correct |
19 ms |
23936 KB |
Output is correct |
10 |
Correct |
20 ms |
23936 KB |
Output is correct |
11 |
Correct |
19 ms |
23936 KB |
Output is correct |
12 |
Correct |
19 ms |
23936 KB |
Output is correct |
13 |
Correct |
19 ms |
23936 KB |
Output is correct |
14 |
Correct |
20 ms |
23936 KB |
Output is correct |
15 |
Correct |
19 ms |
23936 KB |
Output is correct |
16 |
Correct |
20 ms |
23936 KB |
Output is correct |
17 |
Correct |
19 ms |
23936 KB |
Output is correct |
18 |
Correct |
19 ms |
23928 KB |
Output is correct |
19 |
Correct |
20 ms |
23936 KB |
Output is correct |
20 |
Correct |
19 ms |
23936 KB |
Output is correct |
21 |
Correct |
20 ms |
23936 KB |
Output is correct |
22 |
Correct |
19 ms |
23936 KB |
Output is correct |
23 |
Correct |
19 ms |
23936 KB |
Output is correct |
24 |
Correct |
20 ms |
23936 KB |
Output is correct |
25 |
Correct |
19 ms |
23936 KB |
Output is correct |
26 |
Correct |
21 ms |
24448 KB |
Output is correct |
27 |
Correct |
22 ms |
24320 KB |
Output is correct |
28 |
Correct |
21 ms |
24448 KB |
Output is correct |
29 |
Correct |
21 ms |
24448 KB |
Output is correct |
30 |
Correct |
21 ms |
24320 KB |
Output is correct |
31 |
Correct |
20 ms |
23936 KB |
Output is correct |
32 |
Correct |
21 ms |
24448 KB |
Output is correct |
33 |
Correct |
21 ms |
23936 KB |
Output is correct |
34 |
Correct |
21 ms |
24320 KB |
Output is correct |
35 |
Correct |
22 ms |
24448 KB |
Output is correct |
36 |
Correct |
21 ms |
24448 KB |
Output is correct |
37 |
Correct |
21 ms |
24320 KB |
Output is correct |
38 |
Correct |
19 ms |
23936 KB |
Output is correct |
39 |
Correct |
22 ms |
24320 KB |
Output is correct |
40 |
Correct |
21 ms |
24320 KB |
Output is correct |
41 |
Correct |
21 ms |
24320 KB |
Output is correct |
42 |
Correct |
21 ms |
24448 KB |
Output is correct |
43 |
Correct |
21 ms |
24448 KB |
Output is correct |
44 |
Correct |
19 ms |
23936 KB |
Output is correct |
45 |
Correct |
21 ms |
24448 KB |
Output is correct |
46 |
Correct |
22 ms |
24448 KB |
Output is correct |
47 |
Correct |
19 ms |
23936 KB |
Output is correct |
48 |
Correct |
21 ms |
24320 KB |
Output is correct |
49 |
Correct |
21 ms |
24448 KB |
Output is correct |
50 |
Correct |
22 ms |
24440 KB |
Output is correct |
51 |
Correct |
21 ms |
24320 KB |
Output is correct |
52 |
Correct |
21 ms |
24320 KB |
Output is correct |
53 |
Correct |
21 ms |
24448 KB |
Output is correct |
54 |
Correct |
20 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23856 KB |
Output is correct |
3 |
Correct |
19 ms |
23928 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
19 ms |
23936 KB |
Output is correct |
6 |
Correct |
19 ms |
23936 KB |
Output is correct |
7 |
Correct |
19 ms |
23936 KB |
Output is correct |
8 |
Correct |
19 ms |
23936 KB |
Output is correct |
9 |
Correct |
19 ms |
23936 KB |
Output is correct |
10 |
Correct |
20 ms |
23936 KB |
Output is correct |
11 |
Correct |
19 ms |
23936 KB |
Output is correct |
12 |
Correct |
19 ms |
23936 KB |
Output is correct |
13 |
Correct |
19 ms |
23936 KB |
Output is correct |
14 |
Correct |
20 ms |
23936 KB |
Output is correct |
15 |
Correct |
19 ms |
23936 KB |
Output is correct |
16 |
Correct |
20 ms |
23936 KB |
Output is correct |
17 |
Correct |
19 ms |
23936 KB |
Output is correct |
18 |
Correct |
19 ms |
23928 KB |
Output is correct |
19 |
Correct |
20 ms |
23936 KB |
Output is correct |
20 |
Correct |
19 ms |
23936 KB |
Output is correct |
21 |
Correct |
20 ms |
23936 KB |
Output is correct |
22 |
Correct |
19 ms |
23936 KB |
Output is correct |
23 |
Correct |
19 ms |
23936 KB |
Output is correct |
24 |
Correct |
20 ms |
23936 KB |
Output is correct |
25 |
Correct |
19 ms |
23936 KB |
Output is correct |
26 |
Correct |
78 ms |
39536 KB |
Output is correct |
27 |
Correct |
104 ms |
39160 KB |
Output is correct |
28 |
Correct |
21 ms |
24320 KB |
Output is correct |
29 |
Correct |
20 ms |
23936 KB |
Output is correct |
30 |
Correct |
19 ms |
23936 KB |
Output is correct |
31 |
Correct |
118 ms |
39288 KB |
Output is correct |
32 |
Correct |
22 ms |
24320 KB |
Output is correct |
33 |
Correct |
147 ms |
42232 KB |
Output is correct |
34 |
Correct |
115 ms |
39292 KB |
Output is correct |
35 |
Correct |
21 ms |
24320 KB |
Output is correct |
36 |
Correct |
142 ms |
39544 KB |
Output is correct |
37 |
Correct |
21 ms |
24320 KB |
Output is correct |
38 |
Correct |
22 ms |
24320 KB |
Output is correct |
39 |
Correct |
85 ms |
39536 KB |
Output is correct |
40 |
Correct |
22 ms |
24444 KB |
Output is correct |
41 |
Correct |
107 ms |
39280 KB |
Output is correct |
42 |
Correct |
138 ms |
40184 KB |
Output is correct |
43 |
Correct |
20 ms |
23936 KB |
Output is correct |
44 |
Correct |
130 ms |
42352 KB |
Output is correct |
45 |
Correct |
149 ms |
40824 KB |
Output is correct |
46 |
Correct |
21 ms |
24448 KB |
Output is correct |
47 |
Correct |
21 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
39664 KB |
Output is correct |
2 |
Correct |
97 ms |
42928 KB |
Output is correct |
3 |
Correct |
22 ms |
24448 KB |
Output is correct |
4 |
Correct |
24 ms |
24320 KB |
Output is correct |
5 |
Correct |
19 ms |
23936 KB |
Output is correct |
6 |
Correct |
20 ms |
23936 KB |
Output is correct |
7 |
Correct |
20 ms |
24320 KB |
Output is correct |
8 |
Correct |
137 ms |
40952 KB |
Output is correct |
9 |
Correct |
21 ms |
24320 KB |
Output is correct |
10 |
Correct |
115 ms |
39800 KB |
Output is correct |
11 |
Correct |
20 ms |
23808 KB |
Output is correct |
12 |
Correct |
139 ms |
39744 KB |
Output is correct |
13 |
Correct |
141 ms |
40952 KB |
Output is correct |
14 |
Correct |
129 ms |
42492 KB |
Output is correct |
15 |
Correct |
81 ms |
39620 KB |
Output is correct |
16 |
Correct |
21 ms |
24440 KB |
Output is correct |
17 |
Correct |
19 ms |
23936 KB |
Output is correct |
18 |
Correct |
100 ms |
42480 KB |
Output is correct |
19 |
Correct |
160 ms |
44152 KB |
Output is correct |
20 |
Correct |
21 ms |
24448 KB |
Output is correct |
21 |
Correct |
20 ms |
23808 KB |
Output is correct |
22 |
Correct |
111 ms |
41200 KB |
Output is correct |
23 |
Correct |
22 ms |
24320 KB |
Output is correct |
24 |
Correct |
146 ms |
40184 KB |
Output is correct |
25 |
Correct |
156 ms |
43640 KB |
Output is correct |
26 |
Correct |
22 ms |
24448 KB |
Output is correct |
27 |
Correct |
22 ms |
24448 KB |
Output is correct |
28 |
Correct |
21 ms |
24312 KB |
Output is correct |
29 |
Correct |
21 ms |
24320 KB |
Output is correct |
30 |
Correct |
21 ms |
24448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23856 KB |
Output is correct |
3 |
Correct |
19 ms |
23928 KB |
Output is correct |
4 |
Correct |
19 ms |
23936 KB |
Output is correct |
5 |
Correct |
19 ms |
23936 KB |
Output is correct |
6 |
Correct |
19 ms |
23936 KB |
Output is correct |
7 |
Correct |
19 ms |
23936 KB |
Output is correct |
8 |
Correct |
19 ms |
23936 KB |
Output is correct |
9 |
Correct |
19 ms |
23936 KB |
Output is correct |
10 |
Correct |
20 ms |
23936 KB |
Output is correct |
11 |
Correct |
19 ms |
23936 KB |
Output is correct |
12 |
Correct |
19 ms |
23936 KB |
Output is correct |
13 |
Correct |
19 ms |
23936 KB |
Output is correct |
14 |
Correct |
20 ms |
23936 KB |
Output is correct |
15 |
Correct |
19 ms |
23936 KB |
Output is correct |
16 |
Correct |
20 ms |
23936 KB |
Output is correct |
17 |
Correct |
19 ms |
23936 KB |
Output is correct |
18 |
Correct |
19 ms |
23928 KB |
Output is correct |
19 |
Correct |
20 ms |
23936 KB |
Output is correct |
20 |
Correct |
19 ms |
23936 KB |
Output is correct |
21 |
Correct |
20 ms |
23936 KB |
Output is correct |
22 |
Correct |
19 ms |
23936 KB |
Output is correct |
23 |
Correct |
19 ms |
23936 KB |
Output is correct |
24 |
Correct |
20 ms |
23936 KB |
Output is correct |
25 |
Correct |
19 ms |
23936 KB |
Output is correct |
26 |
Correct |
21 ms |
24448 KB |
Output is correct |
27 |
Correct |
22 ms |
24320 KB |
Output is correct |
28 |
Correct |
21 ms |
24448 KB |
Output is correct |
29 |
Correct |
21 ms |
24448 KB |
Output is correct |
30 |
Correct |
21 ms |
24320 KB |
Output is correct |
31 |
Correct |
20 ms |
23936 KB |
Output is correct |
32 |
Correct |
21 ms |
24448 KB |
Output is correct |
33 |
Correct |
21 ms |
23936 KB |
Output is correct |
34 |
Correct |
21 ms |
24320 KB |
Output is correct |
35 |
Correct |
22 ms |
24448 KB |
Output is correct |
36 |
Correct |
21 ms |
24448 KB |
Output is correct |
37 |
Correct |
21 ms |
24320 KB |
Output is correct |
38 |
Correct |
19 ms |
23936 KB |
Output is correct |
39 |
Correct |
22 ms |
24320 KB |
Output is correct |
40 |
Correct |
21 ms |
24320 KB |
Output is correct |
41 |
Correct |
21 ms |
24320 KB |
Output is correct |
42 |
Correct |
21 ms |
24448 KB |
Output is correct |
43 |
Correct |
21 ms |
24448 KB |
Output is correct |
44 |
Correct |
19 ms |
23936 KB |
Output is correct |
45 |
Correct |
21 ms |
24448 KB |
Output is correct |
46 |
Correct |
22 ms |
24448 KB |
Output is correct |
47 |
Correct |
19 ms |
23936 KB |
Output is correct |
48 |
Correct |
21 ms |
24320 KB |
Output is correct |
49 |
Correct |
21 ms |
24448 KB |
Output is correct |
50 |
Correct |
22 ms |
24440 KB |
Output is correct |
51 |
Correct |
21 ms |
24320 KB |
Output is correct |
52 |
Correct |
21 ms |
24320 KB |
Output is correct |
53 |
Correct |
21 ms |
24448 KB |
Output is correct |
54 |
Correct |
20 ms |
24320 KB |
Output is correct |
55 |
Correct |
19 ms |
23936 KB |
Output is correct |
56 |
Correct |
78 ms |
39536 KB |
Output is correct |
57 |
Correct |
104 ms |
39160 KB |
Output is correct |
58 |
Correct |
21 ms |
24320 KB |
Output is correct |
59 |
Correct |
20 ms |
23936 KB |
Output is correct |
60 |
Correct |
19 ms |
23936 KB |
Output is correct |
61 |
Correct |
118 ms |
39288 KB |
Output is correct |
62 |
Correct |
22 ms |
24320 KB |
Output is correct |
63 |
Correct |
147 ms |
42232 KB |
Output is correct |
64 |
Correct |
115 ms |
39292 KB |
Output is correct |
65 |
Correct |
21 ms |
24320 KB |
Output is correct |
66 |
Correct |
142 ms |
39544 KB |
Output is correct |
67 |
Correct |
21 ms |
24320 KB |
Output is correct |
68 |
Correct |
22 ms |
24320 KB |
Output is correct |
69 |
Correct |
85 ms |
39536 KB |
Output is correct |
70 |
Correct |
22 ms |
24444 KB |
Output is correct |
71 |
Correct |
107 ms |
39280 KB |
Output is correct |
72 |
Correct |
138 ms |
40184 KB |
Output is correct |
73 |
Correct |
20 ms |
23936 KB |
Output is correct |
74 |
Correct |
130 ms |
42352 KB |
Output is correct |
75 |
Correct |
149 ms |
40824 KB |
Output is correct |
76 |
Correct |
21 ms |
24448 KB |
Output is correct |
77 |
Correct |
21 ms |
24320 KB |
Output is correct |
78 |
Correct |
90 ms |
39664 KB |
Output is correct |
79 |
Correct |
97 ms |
42928 KB |
Output is correct |
80 |
Correct |
22 ms |
24448 KB |
Output is correct |
81 |
Correct |
24 ms |
24320 KB |
Output is correct |
82 |
Correct |
19 ms |
23936 KB |
Output is correct |
83 |
Correct |
20 ms |
23936 KB |
Output is correct |
84 |
Correct |
20 ms |
24320 KB |
Output is correct |
85 |
Correct |
137 ms |
40952 KB |
Output is correct |
86 |
Correct |
21 ms |
24320 KB |
Output is correct |
87 |
Correct |
115 ms |
39800 KB |
Output is correct |
88 |
Correct |
20 ms |
23808 KB |
Output is correct |
89 |
Correct |
139 ms |
39744 KB |
Output is correct |
90 |
Correct |
141 ms |
40952 KB |
Output is correct |
91 |
Correct |
129 ms |
42492 KB |
Output is correct |
92 |
Correct |
81 ms |
39620 KB |
Output is correct |
93 |
Correct |
21 ms |
24440 KB |
Output is correct |
94 |
Correct |
19 ms |
23936 KB |
Output is correct |
95 |
Correct |
100 ms |
42480 KB |
Output is correct |
96 |
Correct |
160 ms |
44152 KB |
Output is correct |
97 |
Correct |
21 ms |
24448 KB |
Output is correct |
98 |
Correct |
20 ms |
23808 KB |
Output is correct |
99 |
Correct |
111 ms |
41200 KB |
Output is correct |
100 |
Correct |
22 ms |
24320 KB |
Output is correct |
101 |
Correct |
146 ms |
40184 KB |
Output is correct |
102 |
Correct |
156 ms |
43640 KB |
Output is correct |
103 |
Correct |
22 ms |
24448 KB |
Output is correct |
104 |
Correct |
22 ms |
24448 KB |
Output is correct |
105 |
Correct |
21 ms |
24312 KB |
Output is correct |
106 |
Correct |
21 ms |
24320 KB |
Output is correct |
107 |
Correct |
21 ms |
24448 KB |
Output is correct |
108 |
Correct |
646 ms |
110644 KB |
Output is correct |
109 |
Correct |
885 ms |
108508 KB |
Output is correct |
110 |
Correct |
896 ms |
113252 KB |
Output is correct |
111 |
Correct |
1091 ms |
127680 KB |
Output is correct |
112 |
Correct |
1103 ms |
123256 KB |
Output is correct |
113 |
Correct |
640 ms |
117464 KB |
Output is correct |
114 |
Correct |
694 ms |
100692 KB |
Output is correct |
115 |
Correct |
709 ms |
100712 KB |
Output is correct |
116 |
Correct |
899 ms |
107000 KB |
Output is correct |
117 |
Correct |
904 ms |
117352 KB |
Output is correct |
118 |
Correct |
724 ms |
102560 KB |
Output is correct |
119 |
Correct |
891 ms |
117316 KB |
Output is correct |
120 |
Correct |
1101 ms |
122740 KB |
Output is correct |
121 |
Correct |
955 ms |
117112 KB |
Output is correct |
122 |
Correct |
940 ms |
111096 KB |
Output is correct |
123 |
Correct |
628 ms |
118244 KB |
Output is correct |
124 |
Correct |
1000 ms |
115452 KB |
Output is correct |