#include <bits/stdc++.h>
using namespace std;
const int N = 500005, K = 500005;
int n, k, u, v, ans = 0, a[N], sub[N], f[N];
int cur = 0, sum[K], cnt[K];
vector<int> adj[N];
void add(int u, int p, int val, int av = 0)
{
cur -= (cnt[a[u]] == sum[a[u]] || cnt[a[u]] == 0);
cnt[a[u]] += val;
cur += (cnt[a[u]] == sum[a[u]] || cnt[a[u]] == 0);
for (int &v : adj[u])
if (v != p && v != av)
add(v, u, val, av);
}
int DFS_1(int u, int p = 0)
{
sub[u] = 1;
for (int &v : adj[u])
if (v != p)
sub[u] += DFS_1(v, u);
return sub[u];
}
bool DFS_2(int u, int p, bool big)
{
int mc = 0;
for (int &v : adj[u])
if (v != p && sub[v] > sub[mc])
mc = v;
for (int &v : adj[u])
if (v != p && v != mc)
f[u] += (DFS_2(v, u, false) ? 1 : f[v]);
if (mc != 0)
f[u] += (DFS_2(mc, u, true) ? 1 : f[mc]);
add(u, p, 1, mc);
bool chk = (cur == 0);
if (chk)
ans += (f[u] == (u == 1));
if (!big)
add(u, p, -1);
return chk;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i < n; i++)
{
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum[a[i]]++;
}
DFS_1(1);
DFS_2(1, 0, true);
cout << (ans + 1) / 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12148 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Correct |
12 ms |
12068 KB |
Output is correct |
8 |
Correct |
13 ms |
12076 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12148 KB |
Output is correct |
14 |
Correct |
16 ms |
12168 KB |
Output is correct |
15 |
Correct |
12 ms |
12152 KB |
Output is correct |
16 |
Correct |
16 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12152 KB |
Output is correct |
20 |
Correct |
12 ms |
12152 KB |
Output is correct |
21 |
Correct |
12 ms |
12080 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
13 ms |
12132 KB |
Output is correct |
24 |
Correct |
12 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12148 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Correct |
12 ms |
12068 KB |
Output is correct |
8 |
Correct |
13 ms |
12076 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12148 KB |
Output is correct |
14 |
Correct |
16 ms |
12168 KB |
Output is correct |
15 |
Correct |
12 ms |
12152 KB |
Output is correct |
16 |
Correct |
16 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12152 KB |
Output is correct |
20 |
Correct |
12 ms |
12152 KB |
Output is correct |
21 |
Correct |
12 ms |
12080 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
13 ms |
12132 KB |
Output is correct |
24 |
Correct |
12 ms |
12152 KB |
Output is correct |
25 |
Correct |
12 ms |
12152 KB |
Output is correct |
26 |
Correct |
14 ms |
12280 KB |
Output is correct |
27 |
Correct |
14 ms |
12280 KB |
Output is correct |
28 |
Correct |
14 ms |
12408 KB |
Output is correct |
29 |
Correct |
14 ms |
12252 KB |
Output is correct |
30 |
Correct |
15 ms |
12280 KB |
Output is correct |
31 |
Correct |
12 ms |
12152 KB |
Output is correct |
32 |
Correct |
15 ms |
12408 KB |
Output is correct |
33 |
Correct |
12 ms |
12152 KB |
Output is correct |
34 |
Correct |
14 ms |
12280 KB |
Output is correct |
35 |
Correct |
15 ms |
12364 KB |
Output is correct |
36 |
Correct |
15 ms |
12280 KB |
Output is correct |
37 |
Correct |
15 ms |
12280 KB |
Output is correct |
38 |
Correct |
13 ms |
12132 KB |
Output is correct |
39 |
Correct |
15 ms |
12252 KB |
Output is correct |
40 |
Correct |
15 ms |
12280 KB |
Output is correct |
41 |
Correct |
14 ms |
12280 KB |
Output is correct |
42 |
Correct |
14 ms |
12280 KB |
Output is correct |
43 |
Correct |
14 ms |
12536 KB |
Output is correct |
44 |
Correct |
13 ms |
12152 KB |
Output is correct |
45 |
Correct |
14 ms |
12408 KB |
Output is correct |
46 |
Correct |
15 ms |
12280 KB |
Output is correct |
47 |
Correct |
14 ms |
12144 KB |
Output is correct |
48 |
Correct |
15 ms |
12252 KB |
Output is correct |
49 |
Correct |
14 ms |
12384 KB |
Output is correct |
50 |
Correct |
15 ms |
12408 KB |
Output is correct |
51 |
Correct |
15 ms |
12280 KB |
Output is correct |
52 |
Correct |
14 ms |
12280 KB |
Output is correct |
53 |
Correct |
14 ms |
12280 KB |
Output is correct |
54 |
Correct |
15 ms |
12224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12148 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Correct |
12 ms |
12068 KB |
Output is correct |
8 |
Correct |
13 ms |
12076 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12148 KB |
Output is correct |
14 |
Correct |
16 ms |
12168 KB |
Output is correct |
15 |
Correct |
12 ms |
12152 KB |
Output is correct |
16 |
Correct |
16 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12152 KB |
Output is correct |
20 |
Correct |
12 ms |
12152 KB |
Output is correct |
21 |
Correct |
12 ms |
12080 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
13 ms |
12132 KB |
Output is correct |
24 |
Correct |
12 ms |
12152 KB |
Output is correct |
25 |
Correct |
12 ms |
12152 KB |
Output is correct |
26 |
Correct |
69 ms |
17052 KB |
Output is correct |
27 |
Correct |
132 ms |
17188 KB |
Output is correct |
28 |
Correct |
14 ms |
12280 KB |
Output is correct |
29 |
Correct |
12 ms |
12128 KB |
Output is correct |
30 |
Correct |
13 ms |
12152 KB |
Output is correct |
31 |
Correct |
136 ms |
17144 KB |
Output is correct |
32 |
Correct |
14 ms |
12276 KB |
Output is correct |
33 |
Correct |
125 ms |
24312 KB |
Output is correct |
34 |
Correct |
141 ms |
17128 KB |
Output is correct |
35 |
Correct |
17 ms |
12280 KB |
Output is correct |
36 |
Correct |
157 ms |
17804 KB |
Output is correct |
37 |
Correct |
14 ms |
12284 KB |
Output is correct |
38 |
Correct |
14 ms |
12280 KB |
Output is correct |
39 |
Correct |
72 ms |
16996 KB |
Output is correct |
40 |
Correct |
14 ms |
12536 KB |
Output is correct |
41 |
Correct |
144 ms |
17192 KB |
Output is correct |
42 |
Correct |
133 ms |
19448 KB |
Output is correct |
43 |
Correct |
12 ms |
12128 KB |
Output is correct |
44 |
Correct |
163 ms |
24636 KB |
Output is correct |
45 |
Correct |
142 ms |
21016 KB |
Output is correct |
46 |
Correct |
14 ms |
12280 KB |
Output is correct |
47 |
Correct |
15 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
17108 KB |
Output is correct |
2 |
Correct |
82 ms |
17920 KB |
Output is correct |
3 |
Correct |
14 ms |
12280 KB |
Output is correct |
4 |
Correct |
15 ms |
12336 KB |
Output is correct |
5 |
Correct |
12 ms |
12156 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12280 KB |
Output is correct |
8 |
Correct |
149 ms |
17768 KB |
Output is correct |
9 |
Correct |
15 ms |
12252 KB |
Output is correct |
10 |
Correct |
138 ms |
17144 KB |
Output is correct |
11 |
Correct |
13 ms |
12084 KB |
Output is correct |
12 |
Correct |
175 ms |
17312 KB |
Output is correct |
13 |
Correct |
160 ms |
17580 KB |
Output is correct |
14 |
Correct |
149 ms |
18032 KB |
Output is correct |
15 |
Correct |
73 ms |
17056 KB |
Output is correct |
16 |
Correct |
14 ms |
12280 KB |
Output is correct |
17 |
Correct |
12 ms |
12140 KB |
Output is correct |
18 |
Correct |
88 ms |
17760 KB |
Output is correct |
19 |
Correct |
173 ms |
21956 KB |
Output is correct |
20 |
Correct |
15 ms |
12276 KB |
Output is correct |
21 |
Correct |
13 ms |
12160 KB |
Output is correct |
22 |
Correct |
81 ms |
17516 KB |
Output is correct |
23 |
Correct |
15 ms |
12292 KB |
Output is correct |
24 |
Correct |
162 ms |
17236 KB |
Output is correct |
25 |
Correct |
116 ms |
20568 KB |
Output is correct |
26 |
Correct |
14 ms |
12280 KB |
Output is correct |
27 |
Correct |
15 ms |
12416 KB |
Output is correct |
28 |
Correct |
15 ms |
12280 KB |
Output is correct |
29 |
Correct |
14 ms |
12252 KB |
Output is correct |
30 |
Correct |
14 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
12 ms |
12148 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
12 ms |
12152 KB |
Output is correct |
7 |
Correct |
12 ms |
12068 KB |
Output is correct |
8 |
Correct |
13 ms |
12076 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12148 KB |
Output is correct |
14 |
Correct |
16 ms |
12168 KB |
Output is correct |
15 |
Correct |
12 ms |
12152 KB |
Output is correct |
16 |
Correct |
16 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12152 KB |
Output is correct |
20 |
Correct |
12 ms |
12152 KB |
Output is correct |
21 |
Correct |
12 ms |
12080 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
13 ms |
12132 KB |
Output is correct |
24 |
Correct |
12 ms |
12152 KB |
Output is correct |
25 |
Correct |
12 ms |
12152 KB |
Output is correct |
26 |
Correct |
14 ms |
12280 KB |
Output is correct |
27 |
Correct |
14 ms |
12280 KB |
Output is correct |
28 |
Correct |
14 ms |
12408 KB |
Output is correct |
29 |
Correct |
14 ms |
12252 KB |
Output is correct |
30 |
Correct |
15 ms |
12280 KB |
Output is correct |
31 |
Correct |
12 ms |
12152 KB |
Output is correct |
32 |
Correct |
15 ms |
12408 KB |
Output is correct |
33 |
Correct |
12 ms |
12152 KB |
Output is correct |
34 |
Correct |
14 ms |
12280 KB |
Output is correct |
35 |
Correct |
15 ms |
12364 KB |
Output is correct |
36 |
Correct |
15 ms |
12280 KB |
Output is correct |
37 |
Correct |
15 ms |
12280 KB |
Output is correct |
38 |
Correct |
13 ms |
12132 KB |
Output is correct |
39 |
Correct |
15 ms |
12252 KB |
Output is correct |
40 |
Correct |
15 ms |
12280 KB |
Output is correct |
41 |
Correct |
14 ms |
12280 KB |
Output is correct |
42 |
Correct |
14 ms |
12280 KB |
Output is correct |
43 |
Correct |
14 ms |
12536 KB |
Output is correct |
44 |
Correct |
13 ms |
12152 KB |
Output is correct |
45 |
Correct |
14 ms |
12408 KB |
Output is correct |
46 |
Correct |
15 ms |
12280 KB |
Output is correct |
47 |
Correct |
14 ms |
12144 KB |
Output is correct |
48 |
Correct |
15 ms |
12252 KB |
Output is correct |
49 |
Correct |
14 ms |
12384 KB |
Output is correct |
50 |
Correct |
15 ms |
12408 KB |
Output is correct |
51 |
Correct |
15 ms |
12280 KB |
Output is correct |
52 |
Correct |
14 ms |
12280 KB |
Output is correct |
53 |
Correct |
14 ms |
12280 KB |
Output is correct |
54 |
Correct |
15 ms |
12224 KB |
Output is correct |
55 |
Correct |
12 ms |
12152 KB |
Output is correct |
56 |
Correct |
69 ms |
17052 KB |
Output is correct |
57 |
Correct |
132 ms |
17188 KB |
Output is correct |
58 |
Correct |
14 ms |
12280 KB |
Output is correct |
59 |
Correct |
12 ms |
12128 KB |
Output is correct |
60 |
Correct |
13 ms |
12152 KB |
Output is correct |
61 |
Correct |
136 ms |
17144 KB |
Output is correct |
62 |
Correct |
14 ms |
12276 KB |
Output is correct |
63 |
Correct |
125 ms |
24312 KB |
Output is correct |
64 |
Correct |
141 ms |
17128 KB |
Output is correct |
65 |
Correct |
17 ms |
12280 KB |
Output is correct |
66 |
Correct |
157 ms |
17804 KB |
Output is correct |
67 |
Correct |
14 ms |
12284 KB |
Output is correct |
68 |
Correct |
14 ms |
12280 KB |
Output is correct |
69 |
Correct |
72 ms |
16996 KB |
Output is correct |
70 |
Correct |
14 ms |
12536 KB |
Output is correct |
71 |
Correct |
144 ms |
17192 KB |
Output is correct |
72 |
Correct |
133 ms |
19448 KB |
Output is correct |
73 |
Correct |
12 ms |
12128 KB |
Output is correct |
74 |
Correct |
163 ms |
24636 KB |
Output is correct |
75 |
Correct |
142 ms |
21016 KB |
Output is correct |
76 |
Correct |
14 ms |
12280 KB |
Output is correct |
77 |
Correct |
15 ms |
12280 KB |
Output is correct |
78 |
Correct |
73 ms |
17108 KB |
Output is correct |
79 |
Correct |
82 ms |
17920 KB |
Output is correct |
80 |
Correct |
14 ms |
12280 KB |
Output is correct |
81 |
Correct |
15 ms |
12336 KB |
Output is correct |
82 |
Correct |
12 ms |
12156 KB |
Output is correct |
83 |
Correct |
12 ms |
12152 KB |
Output is correct |
84 |
Correct |
13 ms |
12280 KB |
Output is correct |
85 |
Correct |
149 ms |
17768 KB |
Output is correct |
86 |
Correct |
15 ms |
12252 KB |
Output is correct |
87 |
Correct |
138 ms |
17144 KB |
Output is correct |
88 |
Correct |
13 ms |
12084 KB |
Output is correct |
89 |
Correct |
175 ms |
17312 KB |
Output is correct |
90 |
Correct |
160 ms |
17580 KB |
Output is correct |
91 |
Correct |
149 ms |
18032 KB |
Output is correct |
92 |
Correct |
73 ms |
17056 KB |
Output is correct |
93 |
Correct |
14 ms |
12280 KB |
Output is correct |
94 |
Correct |
12 ms |
12140 KB |
Output is correct |
95 |
Correct |
88 ms |
17760 KB |
Output is correct |
96 |
Correct |
173 ms |
21956 KB |
Output is correct |
97 |
Correct |
15 ms |
12276 KB |
Output is correct |
98 |
Correct |
13 ms |
12160 KB |
Output is correct |
99 |
Correct |
81 ms |
17516 KB |
Output is correct |
100 |
Correct |
15 ms |
12292 KB |
Output is correct |
101 |
Correct |
162 ms |
17236 KB |
Output is correct |
102 |
Correct |
116 ms |
20568 KB |
Output is correct |
103 |
Correct |
14 ms |
12280 KB |
Output is correct |
104 |
Correct |
15 ms |
12416 KB |
Output is correct |
105 |
Correct |
15 ms |
12280 KB |
Output is correct |
106 |
Correct |
14 ms |
12252 KB |
Output is correct |
107 |
Correct |
14 ms |
12280 KB |
Output is correct |
108 |
Correct |
496 ms |
35872 KB |
Output is correct |
109 |
Correct |
1035 ms |
52840 KB |
Output is correct |
110 |
Correct |
948 ms |
64072 KB |
Output is correct |
111 |
Correct |
1118 ms |
61812 KB |
Output is correct |
112 |
Correct |
1000 ms |
52040 KB |
Output is correct |
113 |
Correct |
545 ms |
37748 KB |
Output is correct |
114 |
Correct |
1077 ms |
34864 KB |
Output is correct |
115 |
Correct |
1102 ms |
34860 KB |
Output is correct |
116 |
Correct |
1231 ms |
35732 KB |
Output is correct |
117 |
Correct |
1320 ms |
38760 KB |
Output is correct |
118 |
Correct |
1061 ms |
34448 KB |
Output is correct |
119 |
Correct |
1252 ms |
38752 KB |
Output is correct |
120 |
Correct |
953 ms |
52704 KB |
Output is correct |
121 |
Correct |
1219 ms |
38660 KB |
Output is correct |
122 |
Correct |
1222 ms |
37104 KB |
Output is correct |
123 |
Correct |
516 ms |
38228 KB |
Output is correct |
124 |
Correct |
1566 ms |
38048 KB |
Output is correct |