#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;
int n,k,all,lwsep;
vector<int> adj[N];
int res[N],sz[N],am[N],now[N];
bool big[N],sep[N],warn;
int allt,fult;
int sepsub[N];
void pre(int u,int p){ sz[u] = 1; for(int v : adj[u]) if(v!=p) pre(v,u),sz[u]+=sz[v]; }
void add(int u,int p,int x)
{
if(now[res[u]]==am[res[u]]) fult--;
if(now[res[u]]==0) allt++;
now[res[u]]+=x;
if(now[res[u]]==am[res[u]]) fult++;
if(now[res[u]]==0) allt--;
for(int v : adj[u]) if(v!=p and !big[v]) add(v,u,x);
}
void dfs(int u,int p,bool k)
{
int mx = 0,bc = -1;
for(int v : adj[u]) if(v!=p) if(sz[v]>mx) mx = sz[v],bc = v;
for(int v : adj[u]) if(v!=p) if(v!=bc) dfs(v,u,0);
if(bc!=-1) dfs(bc,u,1),big[bc] = 1;
add(u,p,1);
sep[u] = (allt==fult);
if(sep[u] and u>1) all++;
if(bc!=-1) big[bc] = 0;
if(!k) add(u,p,-1);
}
void pres(int u,int p)
{
sepsub[u] = sep[u];
for(int v : adj[u]) if(v!=p) pres(v,u),sepsub[u]+=sepsub[v];
if(sepsub[u]==all and sep[u]) warn = true;
if(sepsub[u]==1 and sep[u] and u!=1) lwsep++;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for(int i = 1;i < n;i++)
{
int a,b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i = 1;i <= n;i++) cin >> res[i],am[res[i]]++;
pre(1,0);
dfs(1,0,1);
pres(1,0);
if(!warn) cout << (lwsep+1)/2;
else if(lwsep%2==1) cout << (lwsep+1)/2;
else cout << lwsep/2+1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
12 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12160 KB |
Output is correct |
5 |
Correct |
13 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
15 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
12 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
12 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
15 ms |
12160 KB |
Output is correct |
16 |
Correct |
15 ms |
12160 KB |
Output is correct |
17 |
Correct |
12 ms |
12160 KB |
Output is correct |
18 |
Correct |
12 ms |
12160 KB |
Output is correct |
19 |
Correct |
12 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
12 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
12 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12160 KB |
Output is correct |
5 |
Correct |
13 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
15 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
12 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
12 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
15 ms |
12160 KB |
Output is correct |
16 |
Correct |
15 ms |
12160 KB |
Output is correct |
17 |
Correct |
12 ms |
12160 KB |
Output is correct |
18 |
Correct |
12 ms |
12160 KB |
Output is correct |
19 |
Correct |
12 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
12 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
14 ms |
12288 KB |
Output is correct |
27 |
Correct |
14 ms |
12288 KB |
Output is correct |
28 |
Correct |
14 ms |
12416 KB |
Output is correct |
29 |
Correct |
14 ms |
12288 KB |
Output is correct |
30 |
Correct |
14 ms |
12288 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
13 ms |
12544 KB |
Output is correct |
33 |
Correct |
13 ms |
12160 KB |
Output is correct |
34 |
Correct |
13 ms |
12288 KB |
Output is correct |
35 |
Correct |
14 ms |
12288 KB |
Output is correct |
36 |
Correct |
14 ms |
12288 KB |
Output is correct |
37 |
Correct |
14 ms |
12288 KB |
Output is correct |
38 |
Correct |
12 ms |
12160 KB |
Output is correct |
39 |
Correct |
14 ms |
12288 KB |
Output is correct |
40 |
Correct |
14 ms |
12288 KB |
Output is correct |
41 |
Correct |
14 ms |
12288 KB |
Output is correct |
42 |
Correct |
13 ms |
12288 KB |
Output is correct |
43 |
Correct |
13 ms |
12544 KB |
Output is correct |
44 |
Correct |
12 ms |
12160 KB |
Output is correct |
45 |
Correct |
13 ms |
12416 KB |
Output is correct |
46 |
Correct |
14 ms |
12288 KB |
Output is correct |
47 |
Correct |
12 ms |
12160 KB |
Output is correct |
48 |
Correct |
13 ms |
12288 KB |
Output is correct |
49 |
Correct |
13 ms |
12288 KB |
Output is correct |
50 |
Correct |
14 ms |
12416 KB |
Output is correct |
51 |
Correct |
14 ms |
12288 KB |
Output is correct |
52 |
Correct |
13 ms |
12288 KB |
Output is correct |
53 |
Correct |
13 ms |
12288 KB |
Output is correct |
54 |
Correct |
14 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
12 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12160 KB |
Output is correct |
5 |
Correct |
13 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
15 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
12 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
12 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
15 ms |
12160 KB |
Output is correct |
16 |
Correct |
15 ms |
12160 KB |
Output is correct |
17 |
Correct |
12 ms |
12160 KB |
Output is correct |
18 |
Correct |
12 ms |
12160 KB |
Output is correct |
19 |
Correct |
12 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
12 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
25 |
Correct |
14 ms |
12160 KB |
Output is correct |
26 |
Correct |
66 ms |
18412 KB |
Output is correct |
27 |
Correct |
123 ms |
18172 KB |
Output is correct |
28 |
Correct |
13 ms |
12296 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
12 ms |
12160 KB |
Output is correct |
31 |
Correct |
134 ms |
18168 KB |
Output is correct |
32 |
Correct |
13 ms |
12288 KB |
Output is correct |
33 |
Correct |
128 ms |
25336 KB |
Output is correct |
34 |
Correct |
125 ms |
18172 KB |
Output is correct |
35 |
Correct |
13 ms |
12288 KB |
Output is correct |
36 |
Correct |
129 ms |
18940 KB |
Output is correct |
37 |
Correct |
13 ms |
12288 KB |
Output is correct |
38 |
Correct |
14 ms |
12288 KB |
Output is correct |
39 |
Correct |
63 ms |
18288 KB |
Output is correct |
40 |
Correct |
13 ms |
12544 KB |
Output is correct |
41 |
Correct |
125 ms |
18040 KB |
Output is correct |
42 |
Correct |
134 ms |
20472 KB |
Output is correct |
43 |
Correct |
13 ms |
12160 KB |
Output is correct |
44 |
Correct |
126 ms |
25592 KB |
Output is correct |
45 |
Correct |
104 ms |
21880 KB |
Output is correct |
46 |
Correct |
15 ms |
12288 KB |
Output is correct |
47 |
Correct |
14 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
18288 KB |
Output is correct |
2 |
Correct |
70 ms |
19416 KB |
Output is correct |
3 |
Correct |
15 ms |
12288 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
13 ms |
12288 KB |
Output is correct |
8 |
Correct |
139 ms |
18872 KB |
Output is correct |
9 |
Correct |
13 ms |
12288 KB |
Output is correct |
10 |
Correct |
126 ms |
18424 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
139 ms |
18168 KB |
Output is correct |
13 |
Correct |
146 ms |
18808 KB |
Output is correct |
14 |
Correct |
139 ms |
19248 KB |
Output is correct |
15 |
Correct |
62 ms |
18304 KB |
Output is correct |
16 |
Correct |
13 ms |
12160 KB |
Output is correct |
17 |
Correct |
12 ms |
12160 KB |
Output is correct |
18 |
Correct |
74 ms |
19308 KB |
Output is correct |
19 |
Correct |
170 ms |
23160 KB |
Output is correct |
20 |
Correct |
15 ms |
12264 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
68 ms |
18928 KB |
Output is correct |
23 |
Correct |
14 ms |
12288 KB |
Output is correct |
24 |
Correct |
144 ms |
18296 KB |
Output is correct |
25 |
Correct |
138 ms |
22008 KB |
Output is correct |
26 |
Correct |
13 ms |
12288 KB |
Output is correct |
27 |
Correct |
14 ms |
12416 KB |
Output is correct |
28 |
Correct |
14 ms |
12288 KB |
Output is correct |
29 |
Correct |
13 ms |
12288 KB |
Output is correct |
30 |
Correct |
13 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
12 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12160 KB |
Output is correct |
5 |
Correct |
13 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
15 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
12 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
12 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
15 ms |
12160 KB |
Output is correct |
16 |
Correct |
15 ms |
12160 KB |
Output is correct |
17 |
Correct |
12 ms |
12160 KB |
Output is correct |
18 |
Correct |
12 ms |
12160 KB |
Output is correct |
19 |
Correct |
12 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
12 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
14 ms |
12288 KB |
Output is correct |
27 |
Correct |
14 ms |
12288 KB |
Output is correct |
28 |
Correct |
14 ms |
12416 KB |
Output is correct |
29 |
Correct |
14 ms |
12288 KB |
Output is correct |
30 |
Correct |
14 ms |
12288 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
13 ms |
12544 KB |
Output is correct |
33 |
Correct |
13 ms |
12160 KB |
Output is correct |
34 |
Correct |
13 ms |
12288 KB |
Output is correct |
35 |
Correct |
14 ms |
12288 KB |
Output is correct |
36 |
Correct |
14 ms |
12288 KB |
Output is correct |
37 |
Correct |
14 ms |
12288 KB |
Output is correct |
38 |
Correct |
12 ms |
12160 KB |
Output is correct |
39 |
Correct |
14 ms |
12288 KB |
Output is correct |
40 |
Correct |
14 ms |
12288 KB |
Output is correct |
41 |
Correct |
14 ms |
12288 KB |
Output is correct |
42 |
Correct |
13 ms |
12288 KB |
Output is correct |
43 |
Correct |
13 ms |
12544 KB |
Output is correct |
44 |
Correct |
12 ms |
12160 KB |
Output is correct |
45 |
Correct |
13 ms |
12416 KB |
Output is correct |
46 |
Correct |
14 ms |
12288 KB |
Output is correct |
47 |
Correct |
12 ms |
12160 KB |
Output is correct |
48 |
Correct |
13 ms |
12288 KB |
Output is correct |
49 |
Correct |
13 ms |
12288 KB |
Output is correct |
50 |
Correct |
14 ms |
12416 KB |
Output is correct |
51 |
Correct |
14 ms |
12288 KB |
Output is correct |
52 |
Correct |
13 ms |
12288 KB |
Output is correct |
53 |
Correct |
13 ms |
12288 KB |
Output is correct |
54 |
Correct |
14 ms |
12288 KB |
Output is correct |
55 |
Correct |
14 ms |
12160 KB |
Output is correct |
56 |
Correct |
66 ms |
18412 KB |
Output is correct |
57 |
Correct |
123 ms |
18172 KB |
Output is correct |
58 |
Correct |
13 ms |
12296 KB |
Output is correct |
59 |
Correct |
11 ms |
12160 KB |
Output is correct |
60 |
Correct |
12 ms |
12160 KB |
Output is correct |
61 |
Correct |
134 ms |
18168 KB |
Output is correct |
62 |
Correct |
13 ms |
12288 KB |
Output is correct |
63 |
Correct |
128 ms |
25336 KB |
Output is correct |
64 |
Correct |
125 ms |
18172 KB |
Output is correct |
65 |
Correct |
13 ms |
12288 KB |
Output is correct |
66 |
Correct |
129 ms |
18940 KB |
Output is correct |
67 |
Correct |
13 ms |
12288 KB |
Output is correct |
68 |
Correct |
14 ms |
12288 KB |
Output is correct |
69 |
Correct |
63 ms |
18288 KB |
Output is correct |
70 |
Correct |
13 ms |
12544 KB |
Output is correct |
71 |
Correct |
125 ms |
18040 KB |
Output is correct |
72 |
Correct |
134 ms |
20472 KB |
Output is correct |
73 |
Correct |
13 ms |
12160 KB |
Output is correct |
74 |
Correct |
126 ms |
25592 KB |
Output is correct |
75 |
Correct |
104 ms |
21880 KB |
Output is correct |
76 |
Correct |
15 ms |
12288 KB |
Output is correct |
77 |
Correct |
14 ms |
12288 KB |
Output is correct |
78 |
Correct |
63 ms |
18288 KB |
Output is correct |
79 |
Correct |
70 ms |
19416 KB |
Output is correct |
80 |
Correct |
15 ms |
12288 KB |
Output is correct |
81 |
Correct |
13 ms |
12288 KB |
Output is correct |
82 |
Correct |
12 ms |
12160 KB |
Output is correct |
83 |
Correct |
12 ms |
12160 KB |
Output is correct |
84 |
Correct |
13 ms |
12288 KB |
Output is correct |
85 |
Correct |
139 ms |
18872 KB |
Output is correct |
86 |
Correct |
13 ms |
12288 KB |
Output is correct |
87 |
Correct |
126 ms |
18424 KB |
Output is correct |
88 |
Correct |
12 ms |
12160 KB |
Output is correct |
89 |
Correct |
139 ms |
18168 KB |
Output is correct |
90 |
Correct |
146 ms |
18808 KB |
Output is correct |
91 |
Correct |
139 ms |
19248 KB |
Output is correct |
92 |
Correct |
62 ms |
18304 KB |
Output is correct |
93 |
Correct |
13 ms |
12160 KB |
Output is correct |
94 |
Correct |
12 ms |
12160 KB |
Output is correct |
95 |
Correct |
74 ms |
19308 KB |
Output is correct |
96 |
Correct |
170 ms |
23160 KB |
Output is correct |
97 |
Correct |
15 ms |
12264 KB |
Output is correct |
98 |
Correct |
12 ms |
12160 KB |
Output is correct |
99 |
Correct |
68 ms |
18928 KB |
Output is correct |
100 |
Correct |
14 ms |
12288 KB |
Output is correct |
101 |
Correct |
144 ms |
18296 KB |
Output is correct |
102 |
Correct |
138 ms |
22008 KB |
Output is correct |
103 |
Correct |
13 ms |
12288 KB |
Output is correct |
104 |
Correct |
14 ms |
12416 KB |
Output is correct |
105 |
Correct |
14 ms |
12288 KB |
Output is correct |
106 |
Correct |
13 ms |
12288 KB |
Output is correct |
107 |
Correct |
13 ms |
12288 KB |
Output is correct |
108 |
Correct |
465 ms |
47692 KB |
Output is correct |
109 |
Correct |
1157 ms |
60924 KB |
Output is correct |
110 |
Correct |
993 ms |
71928 KB |
Output is correct |
111 |
Correct |
1183 ms |
72312 KB |
Output is correct |
112 |
Correct |
969 ms |
62204 KB |
Output is correct |
113 |
Correct |
494 ms |
49628 KB |
Output is correct |
114 |
Correct |
1070 ms |
42872 KB |
Output is correct |
115 |
Correct |
1070 ms |
42868 KB |
Output is correct |
116 |
Correct |
1192 ms |
45400 KB |
Output is correct |
117 |
Correct |
1283 ms |
49144 KB |
Output is correct |
118 |
Correct |
1078 ms |
43004 KB |
Output is correct |
119 |
Correct |
1206 ms |
49104 KB |
Output is correct |
120 |
Correct |
992 ms |
63096 KB |
Output is correct |
121 |
Correct |
1172 ms |
48996 KB |
Output is correct |
122 |
Correct |
1193 ms |
47480 KB |
Output is correct |
123 |
Correct |
482 ms |
50020 KB |
Output is correct |
124 |
Correct |
1464 ms |
48120 KB |
Output is correct |