#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=502000;
const int mxM=104;
const int mxK=1000000;
const ll MOD=1000000007;
const ll INF=100000000000001;
int N, K;
vector <int> v[mxN];
vector <pii> E;
vector <int> grp[mxN];
int par[mxN][20], dep[mxN];
int S[mxN];
int root[mxN]; ///lca of all node
int uf[mxN];
int deg[mxN];
int ans;
void dfs(int now, int pre)
{
for(int nxt : v[now])
{
if(nxt==pre) continue;
par[nxt][0]=now;
dep[nxt]=dep[now]+1;
dfs(nxt, now);
}
}
int findpar(int a)
{
if(uf[a]==a) return a;
return uf[a]=findpar(uf[a]);
}
int lca(int a, int b)
{
if(dep[a]<dep[b]) return lca(b, a);
for(int i=19;i>=0;i--)
{
if(dep[a]>=dep[b]+(1<<i))
{
a=par[a][i];
}
}
if(a==b) return a;
for(int i=19;i>=0;i--)
{
if(par[a][i]!=par[b][i]) a=par[a][i], b=par[b][i];
}
return par[a][0];
}
int main()
{
gibon
cin >> N >> K;
for(int i=1;i<=N;i++) uf[i]=i;
for(int i=1;i<N;i++)
{
int a, b; cin >> a >> b;
v[a].push_back(b); v[b].push_back(a);
E.push_back({a, b});
}
dfs(1, -1);
for(int i=1;i<=19;i++) for(int j=1;j<=N;j++) par[j][i]=par[par[j][i-1]][i-1];
for(int i=1;i<=N;i++) cin >> S[i];
for(int i=1;i<=N;i++) grp[S[i]].push_back(i);
for(int i=1;i<=K;i++)
{
for(int ele : grp[i])
{
if(!root[i])
{
root[i]=ele; continue;
}
root[i]=lca(root[i], ele);
}
}
for(int i=1;i<=K;i++)
{
for(int ele : grp[i])
{
int now=ele;
while(findpar(now)!=findpar(root[i]))
{
now=findpar(now);
uf[now]=par[now][0];
now=par[now][0];
}
}
}
for(pii ele : E)
{
int a=findpar(ele.fir), b=findpar(ele.sec);
if(a==b) continue;
deg[a]++; deg[b]++;
}
for(int i=1;i<=N;i++)
{
if(deg[i]==1) ans++;
}
cout << (ans+1)/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23896 KB |
Output is correct |
2 |
Correct |
13 ms |
23880 KB |
Output is correct |
3 |
Correct |
14 ms |
23884 KB |
Output is correct |
4 |
Correct |
13 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
23956 KB |
Output is correct |
7 |
Correct |
13 ms |
23920 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23904 KB |
Output is correct |
11 |
Correct |
13 ms |
23904 KB |
Output is correct |
12 |
Correct |
14 ms |
23884 KB |
Output is correct |
13 |
Correct |
14 ms |
23884 KB |
Output is correct |
14 |
Correct |
15 ms |
23848 KB |
Output is correct |
15 |
Correct |
13 ms |
23928 KB |
Output is correct |
16 |
Correct |
13 ms |
23884 KB |
Output is correct |
17 |
Correct |
14 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
23932 KB |
Output is correct |
19 |
Correct |
14 ms |
23852 KB |
Output is correct |
20 |
Correct |
13 ms |
23904 KB |
Output is correct |
21 |
Correct |
13 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23884 KB |
Output is correct |
23 |
Correct |
13 ms |
23908 KB |
Output is correct |
24 |
Correct |
13 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23896 KB |
Output is correct |
2 |
Correct |
13 ms |
23880 KB |
Output is correct |
3 |
Correct |
14 ms |
23884 KB |
Output is correct |
4 |
Correct |
13 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
23956 KB |
Output is correct |
7 |
Correct |
13 ms |
23920 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23904 KB |
Output is correct |
11 |
Correct |
13 ms |
23904 KB |
Output is correct |
12 |
Correct |
14 ms |
23884 KB |
Output is correct |
13 |
Correct |
14 ms |
23884 KB |
Output is correct |
14 |
Correct |
15 ms |
23848 KB |
Output is correct |
15 |
Correct |
13 ms |
23928 KB |
Output is correct |
16 |
Correct |
13 ms |
23884 KB |
Output is correct |
17 |
Correct |
14 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
23932 KB |
Output is correct |
19 |
Correct |
14 ms |
23852 KB |
Output is correct |
20 |
Correct |
13 ms |
23904 KB |
Output is correct |
21 |
Correct |
13 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23884 KB |
Output is correct |
23 |
Correct |
13 ms |
23908 KB |
Output is correct |
24 |
Correct |
13 ms |
23840 KB |
Output is correct |
25 |
Correct |
13 ms |
23884 KB |
Output is correct |
26 |
Correct |
15 ms |
24372 KB |
Output is correct |
27 |
Correct |
15 ms |
24364 KB |
Output is correct |
28 |
Correct |
16 ms |
24560 KB |
Output is correct |
29 |
Correct |
16 ms |
24416 KB |
Output is correct |
30 |
Correct |
17 ms |
24296 KB |
Output is correct |
31 |
Correct |
14 ms |
23876 KB |
Output is correct |
32 |
Correct |
15 ms |
24576 KB |
Output is correct |
33 |
Correct |
14 ms |
23904 KB |
Output is correct |
34 |
Correct |
15 ms |
24300 KB |
Output is correct |
35 |
Correct |
15 ms |
24408 KB |
Output is correct |
36 |
Correct |
15 ms |
24396 KB |
Output is correct |
37 |
Correct |
16 ms |
24396 KB |
Output is correct |
38 |
Correct |
14 ms |
23884 KB |
Output is correct |
39 |
Correct |
15 ms |
24428 KB |
Output is correct |
40 |
Correct |
15 ms |
24396 KB |
Output is correct |
41 |
Correct |
15 ms |
24408 KB |
Output is correct |
42 |
Correct |
17 ms |
24388 KB |
Output is correct |
43 |
Correct |
15 ms |
24524 KB |
Output is correct |
44 |
Correct |
15 ms |
23896 KB |
Output is correct |
45 |
Correct |
15 ms |
24432 KB |
Output is correct |
46 |
Correct |
16 ms |
24372 KB |
Output is correct |
47 |
Correct |
14 ms |
23884 KB |
Output is correct |
48 |
Correct |
16 ms |
24396 KB |
Output is correct |
49 |
Correct |
16 ms |
24388 KB |
Output is correct |
50 |
Correct |
16 ms |
24448 KB |
Output is correct |
51 |
Correct |
15 ms |
24396 KB |
Output is correct |
52 |
Correct |
15 ms |
24384 KB |
Output is correct |
53 |
Correct |
16 ms |
24436 KB |
Output is correct |
54 |
Correct |
16 ms |
24408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23896 KB |
Output is correct |
2 |
Correct |
13 ms |
23880 KB |
Output is correct |
3 |
Correct |
14 ms |
23884 KB |
Output is correct |
4 |
Correct |
13 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
23956 KB |
Output is correct |
7 |
Correct |
13 ms |
23920 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23904 KB |
Output is correct |
11 |
Correct |
13 ms |
23904 KB |
Output is correct |
12 |
Correct |
14 ms |
23884 KB |
Output is correct |
13 |
Correct |
14 ms |
23884 KB |
Output is correct |
14 |
Correct |
15 ms |
23848 KB |
Output is correct |
15 |
Correct |
13 ms |
23928 KB |
Output is correct |
16 |
Correct |
13 ms |
23884 KB |
Output is correct |
17 |
Correct |
14 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
23932 KB |
Output is correct |
19 |
Correct |
14 ms |
23852 KB |
Output is correct |
20 |
Correct |
13 ms |
23904 KB |
Output is correct |
21 |
Correct |
13 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23884 KB |
Output is correct |
23 |
Correct |
13 ms |
23908 KB |
Output is correct |
24 |
Correct |
13 ms |
23840 KB |
Output is correct |
25 |
Correct |
13 ms |
23884 KB |
Output is correct |
26 |
Correct |
85 ms |
39164 KB |
Output is correct |
27 |
Correct |
103 ms |
38832 KB |
Output is correct |
28 |
Correct |
17 ms |
24396 KB |
Output is correct |
29 |
Correct |
13 ms |
23956 KB |
Output is correct |
30 |
Correct |
14 ms |
23884 KB |
Output is correct |
31 |
Correct |
102 ms |
38964 KB |
Output is correct |
32 |
Correct |
16 ms |
24396 KB |
Output is correct |
33 |
Correct |
132 ms |
46012 KB |
Output is correct |
34 |
Correct |
103 ms |
38876 KB |
Output is correct |
35 |
Correct |
16 ms |
24416 KB |
Output is correct |
36 |
Correct |
113 ms |
39736 KB |
Output is correct |
37 |
Correct |
16 ms |
24308 KB |
Output is correct |
38 |
Correct |
15 ms |
24436 KB |
Output is correct |
39 |
Correct |
95 ms |
39188 KB |
Output is correct |
40 |
Correct |
15 ms |
24524 KB |
Output is correct |
41 |
Correct |
91 ms |
39096 KB |
Output is correct |
42 |
Correct |
132 ms |
41216 KB |
Output is correct |
43 |
Correct |
14 ms |
23884 KB |
Output is correct |
44 |
Correct |
125 ms |
46644 KB |
Output is correct |
45 |
Correct |
127 ms |
42604 KB |
Output is correct |
46 |
Correct |
15 ms |
24396 KB |
Output is correct |
47 |
Correct |
16 ms |
24304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
39184 KB |
Output is correct |
2 |
Correct |
83 ms |
42800 KB |
Output is correct |
3 |
Correct |
15 ms |
24396 KB |
Output is correct |
4 |
Correct |
17 ms |
24396 KB |
Output is correct |
5 |
Correct |
14 ms |
23904 KB |
Output is correct |
6 |
Correct |
13 ms |
23944 KB |
Output is correct |
7 |
Correct |
16 ms |
24396 KB |
Output is correct |
8 |
Correct |
126 ms |
40916 KB |
Output is correct |
9 |
Correct |
15 ms |
24396 KB |
Output is correct |
10 |
Correct |
102 ms |
39420 KB |
Output is correct |
11 |
Correct |
14 ms |
23884 KB |
Output is correct |
12 |
Correct |
115 ms |
39352 KB |
Output is correct |
13 |
Correct |
113 ms |
40700 KB |
Output is correct |
14 |
Correct |
98 ms |
42556 KB |
Output is correct |
15 |
Correct |
86 ms |
39080 KB |
Output is correct |
16 |
Correct |
15 ms |
24396 KB |
Output is correct |
17 |
Correct |
14 ms |
23952 KB |
Output is correct |
18 |
Correct |
96 ms |
42664 KB |
Output is correct |
19 |
Correct |
112 ms |
46520 KB |
Output is correct |
20 |
Correct |
15 ms |
24356 KB |
Output is correct |
21 |
Correct |
14 ms |
23920 KB |
Output is correct |
22 |
Correct |
93 ms |
41032 KB |
Output is correct |
23 |
Correct |
16 ms |
24384 KB |
Output is correct |
24 |
Correct |
111 ms |
39840 KB |
Output is correct |
25 |
Correct |
109 ms |
45236 KB |
Output is correct |
26 |
Correct |
15 ms |
24432 KB |
Output is correct |
27 |
Correct |
16 ms |
24556 KB |
Output is correct |
28 |
Correct |
16 ms |
24348 KB |
Output is correct |
29 |
Correct |
16 ms |
24396 KB |
Output is correct |
30 |
Correct |
16 ms |
24416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23896 KB |
Output is correct |
2 |
Correct |
13 ms |
23880 KB |
Output is correct |
3 |
Correct |
14 ms |
23884 KB |
Output is correct |
4 |
Correct |
13 ms |
23848 KB |
Output is correct |
5 |
Correct |
13 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
23956 KB |
Output is correct |
7 |
Correct |
13 ms |
23920 KB |
Output is correct |
8 |
Correct |
13 ms |
23884 KB |
Output is correct |
9 |
Correct |
14 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23904 KB |
Output is correct |
11 |
Correct |
13 ms |
23904 KB |
Output is correct |
12 |
Correct |
14 ms |
23884 KB |
Output is correct |
13 |
Correct |
14 ms |
23884 KB |
Output is correct |
14 |
Correct |
15 ms |
23848 KB |
Output is correct |
15 |
Correct |
13 ms |
23928 KB |
Output is correct |
16 |
Correct |
13 ms |
23884 KB |
Output is correct |
17 |
Correct |
14 ms |
23900 KB |
Output is correct |
18 |
Correct |
13 ms |
23932 KB |
Output is correct |
19 |
Correct |
14 ms |
23852 KB |
Output is correct |
20 |
Correct |
13 ms |
23904 KB |
Output is correct |
21 |
Correct |
13 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23884 KB |
Output is correct |
23 |
Correct |
13 ms |
23908 KB |
Output is correct |
24 |
Correct |
13 ms |
23840 KB |
Output is correct |
25 |
Correct |
13 ms |
23884 KB |
Output is correct |
26 |
Correct |
15 ms |
24372 KB |
Output is correct |
27 |
Correct |
15 ms |
24364 KB |
Output is correct |
28 |
Correct |
16 ms |
24560 KB |
Output is correct |
29 |
Correct |
16 ms |
24416 KB |
Output is correct |
30 |
Correct |
17 ms |
24296 KB |
Output is correct |
31 |
Correct |
14 ms |
23876 KB |
Output is correct |
32 |
Correct |
15 ms |
24576 KB |
Output is correct |
33 |
Correct |
14 ms |
23904 KB |
Output is correct |
34 |
Correct |
15 ms |
24300 KB |
Output is correct |
35 |
Correct |
15 ms |
24408 KB |
Output is correct |
36 |
Correct |
15 ms |
24396 KB |
Output is correct |
37 |
Correct |
16 ms |
24396 KB |
Output is correct |
38 |
Correct |
14 ms |
23884 KB |
Output is correct |
39 |
Correct |
15 ms |
24428 KB |
Output is correct |
40 |
Correct |
15 ms |
24396 KB |
Output is correct |
41 |
Correct |
15 ms |
24408 KB |
Output is correct |
42 |
Correct |
17 ms |
24388 KB |
Output is correct |
43 |
Correct |
15 ms |
24524 KB |
Output is correct |
44 |
Correct |
15 ms |
23896 KB |
Output is correct |
45 |
Correct |
15 ms |
24432 KB |
Output is correct |
46 |
Correct |
16 ms |
24372 KB |
Output is correct |
47 |
Correct |
14 ms |
23884 KB |
Output is correct |
48 |
Correct |
16 ms |
24396 KB |
Output is correct |
49 |
Correct |
16 ms |
24388 KB |
Output is correct |
50 |
Correct |
16 ms |
24448 KB |
Output is correct |
51 |
Correct |
15 ms |
24396 KB |
Output is correct |
52 |
Correct |
15 ms |
24384 KB |
Output is correct |
53 |
Correct |
16 ms |
24436 KB |
Output is correct |
54 |
Correct |
16 ms |
24408 KB |
Output is correct |
55 |
Correct |
13 ms |
23884 KB |
Output is correct |
56 |
Correct |
85 ms |
39164 KB |
Output is correct |
57 |
Correct |
103 ms |
38832 KB |
Output is correct |
58 |
Correct |
17 ms |
24396 KB |
Output is correct |
59 |
Correct |
13 ms |
23956 KB |
Output is correct |
60 |
Correct |
14 ms |
23884 KB |
Output is correct |
61 |
Correct |
102 ms |
38964 KB |
Output is correct |
62 |
Correct |
16 ms |
24396 KB |
Output is correct |
63 |
Correct |
132 ms |
46012 KB |
Output is correct |
64 |
Correct |
103 ms |
38876 KB |
Output is correct |
65 |
Correct |
16 ms |
24416 KB |
Output is correct |
66 |
Correct |
113 ms |
39736 KB |
Output is correct |
67 |
Correct |
16 ms |
24308 KB |
Output is correct |
68 |
Correct |
15 ms |
24436 KB |
Output is correct |
69 |
Correct |
95 ms |
39188 KB |
Output is correct |
70 |
Correct |
15 ms |
24524 KB |
Output is correct |
71 |
Correct |
91 ms |
39096 KB |
Output is correct |
72 |
Correct |
132 ms |
41216 KB |
Output is correct |
73 |
Correct |
14 ms |
23884 KB |
Output is correct |
74 |
Correct |
125 ms |
46644 KB |
Output is correct |
75 |
Correct |
127 ms |
42604 KB |
Output is correct |
76 |
Correct |
15 ms |
24396 KB |
Output is correct |
77 |
Correct |
16 ms |
24304 KB |
Output is correct |
78 |
Correct |
83 ms |
39184 KB |
Output is correct |
79 |
Correct |
83 ms |
42800 KB |
Output is correct |
80 |
Correct |
15 ms |
24396 KB |
Output is correct |
81 |
Correct |
17 ms |
24396 KB |
Output is correct |
82 |
Correct |
14 ms |
23904 KB |
Output is correct |
83 |
Correct |
13 ms |
23944 KB |
Output is correct |
84 |
Correct |
16 ms |
24396 KB |
Output is correct |
85 |
Correct |
126 ms |
40916 KB |
Output is correct |
86 |
Correct |
15 ms |
24396 KB |
Output is correct |
87 |
Correct |
102 ms |
39420 KB |
Output is correct |
88 |
Correct |
14 ms |
23884 KB |
Output is correct |
89 |
Correct |
115 ms |
39352 KB |
Output is correct |
90 |
Correct |
113 ms |
40700 KB |
Output is correct |
91 |
Correct |
98 ms |
42556 KB |
Output is correct |
92 |
Correct |
86 ms |
39080 KB |
Output is correct |
93 |
Correct |
15 ms |
24396 KB |
Output is correct |
94 |
Correct |
14 ms |
23952 KB |
Output is correct |
95 |
Correct |
96 ms |
42664 KB |
Output is correct |
96 |
Correct |
112 ms |
46520 KB |
Output is correct |
97 |
Correct |
15 ms |
24356 KB |
Output is correct |
98 |
Correct |
14 ms |
23920 KB |
Output is correct |
99 |
Correct |
93 ms |
41032 KB |
Output is correct |
100 |
Correct |
16 ms |
24384 KB |
Output is correct |
101 |
Correct |
111 ms |
39840 KB |
Output is correct |
102 |
Correct |
109 ms |
45236 KB |
Output is correct |
103 |
Correct |
15 ms |
24432 KB |
Output is correct |
104 |
Correct |
16 ms |
24556 KB |
Output is correct |
105 |
Correct |
16 ms |
24348 KB |
Output is correct |
106 |
Correct |
16 ms |
24396 KB |
Output is correct |
107 |
Correct |
16 ms |
24416 KB |
Output is correct |
108 |
Correct |
664 ms |
109536 KB |
Output is correct |
109 |
Correct |
981 ms |
118312 KB |
Output is correct |
110 |
Correct |
1008 ms |
130088 KB |
Output is correct |
111 |
Correct |
895 ms |
141484 KB |
Output is correct |
112 |
Correct |
897 ms |
132020 KB |
Output is correct |
113 |
Correct |
684 ms |
118308 KB |
Output is correct |
114 |
Correct |
672 ms |
99960 KB |
Output is correct |
115 |
Correct |
656 ms |
100824 KB |
Output is correct |
116 |
Correct |
874 ms |
105460 KB |
Output is correct |
117 |
Correct |
742 ms |
118568 KB |
Output is correct |
118 |
Correct |
747 ms |
100684 KB |
Output is correct |
119 |
Correct |
740 ms |
118508 KB |
Output is correct |
120 |
Correct |
850 ms |
132520 KB |
Output is correct |
121 |
Correct |
772 ms |
118580 KB |
Output is correct |
122 |
Correct |
909 ms |
111444 KB |
Output is correct |
123 |
Correct |
569 ms |
120048 KB |
Output is correct |
124 |
Correct |
856 ms |
116596 KB |
Output is correct |