import sys
from collections import defaultdict
N, K = map(int, input().split())
langs = list(map(int, input().split()))
childs = defaultdict(list)
for i in range(1, N):
parent = int(input())
childs[parent].append(i)
def dfs(n, dep, seen_langs):
lang = langs[n]
candidate = seen_langs is not None and lang not in seen_langs
if candidate:
seen_langs.add(lang)
local_count = defaultdict(int)
local_count_lang = defaultdict(lambda: defaultdict(int))
local_count[dep] += 1
local_count_lang[lang][dep] += 1
best_productivity = 0
best_swaps = 0
for child in childs[n]:
inc_local_count, inc_local_count_lang, productivity, swaps = dfs(child, dep+1, seen_langs)
if len(inc_local_count) > len(local_count):
inc_local_count, local_count = local_count, inc_local_count
inc_local_count_lang, local_count_lang = local_count_lang, inc_local_count_lang
for d, count in inc_local_count.items():
local_count[d] += count
for c, count_dep in inc_local_count_lang.items():
for d, count in count_dep.items():
local_count_lang[c][d] += count
if productivity > best_productivity:
best_productivity, best_swaps = productivity, swaps
if productivity == best_productivity and swaps < best_swaps:
best_swaps = swaps
if candidate:
seen_langs.remove(lang)
non_swaps = productivity = 0
deps = total_count_lang[lang].keys() if len(total_count_lang[lang]) < len(local_count) else local_count.keys()
for d in deps:
non_swaps += local_count_lang[lang][d]
productivity += min(local_count[d], total_count_lang[lang][d])
swaps = productivity - non_swaps
if productivity > best_productivity:
best_productivity, best_swaps = productivity, swaps
if productivity == best_productivity and swaps < best_swaps:
best_swaps = swaps
return local_count, local_count_lang, best_productivity, best_swaps
sys.setrecursionlimit(N+10)
total_count, total_count_lang, _, _ = dfs(0, 0, None)
_, _, productivity, swaps = dfs(0, 0, set())
print(productivity, swaps)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18736 KB |
Output is correct |
2 |
Correct |
32 ms |
18728 KB |
Output is correct |
3 |
Correct |
34 ms |
18696 KB |
Output is correct |
4 |
Correct |
27 ms |
18740 KB |
Output is correct |
5 |
Correct |
29 ms |
18792 KB |
Output is correct |
6 |
Correct |
30 ms |
18692 KB |
Output is correct |
7 |
Correct |
32 ms |
19212 KB |
Output is correct |
8 |
Correct |
107 ms |
31400 KB |
Output is correct |
9 |
Correct |
2387 ms |
373324 KB |
Output is correct |
10 |
Correct |
32 ms |
18964 KB |
Output is correct |
11 |
Correct |
132 ms |
31296 KB |
Output is correct |
12 |
Correct |
2025 ms |
398692 KB |
Output is correct |
13 |
Correct |
33 ms |
18980 KB |
Output is correct |
14 |
Correct |
127 ms |
32172 KB |
Output is correct |
15 |
Correct |
2548 ms |
376644 KB |
Output is correct |
16 |
Correct |
30 ms |
18740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18704 KB |
Output is correct |
2 |
Correct |
34 ms |
18740 KB |
Output is correct |
3 |
Correct |
34 ms |
18996 KB |
Output is correct |
4 |
Correct |
108 ms |
31412 KB |
Output is correct |
5 |
Correct |
2000 ms |
399244 KB |
Output is correct |
6 |
Correct |
34 ms |
18768 KB |
Output is correct |
7 |
Correct |
131 ms |
27148 KB |
Output is correct |
8 |
Correct |
289 ms |
42200 KB |
Output is correct |
9 |
Correct |
37 ms |
18988 KB |
Output is correct |
10 |
Correct |
163 ms |
32328 KB |
Output is correct |
11 |
Correct |
2073 ms |
249424 KB |
Output is correct |
12 |
Correct |
254 ms |
36372 KB |
Output is correct |
13 |
Correct |
32 ms |
18740 KB |
Output is correct |
14 |
Correct |
45 ms |
18888 KB |
Output is correct |
15 |
Correct |
557 ms |
48080 KB |
Output is correct |
16 |
Correct |
469 ms |
46216 KB |
Output is correct |
17 |
Correct |
37 ms |
18748 KB |
Output is correct |
18 |
Correct |
167 ms |
27072 KB |
Output is correct |
19 |
Correct |
452 ms |
46156 KB |
Output is correct |
20 |
Correct |
508 ms |
47344 KB |
Output is correct |
21 |
Correct |
398 ms |
44724 KB |
Output is correct |
22 |
Correct |
32 ms |
18768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
18592 KB |
Output is correct |
2 |
Correct |
37 ms |
18636 KB |
Output is correct |
3 |
Correct |
29 ms |
18740 KB |
Output is correct |
4 |
Correct |
30 ms |
18936 KB |
Output is correct |
5 |
Correct |
30 ms |
18576 KB |
Output is correct |
6 |
Correct |
30 ms |
18688 KB |
Output is correct |
7 |
Correct |
37 ms |
19076 KB |
Output is correct |
8 |
Correct |
2356 ms |
373928 KB |
Output is correct |
9 |
Correct |
31 ms |
19004 KB |
Output is correct |
10 |
Correct |
123 ms |
32188 KB |
Output is correct |
11 |
Correct |
2337 ms |
375240 KB |
Output is correct |
12 |
Correct |
30 ms |
18748 KB |
Output is correct |
13 |
Correct |
27 ms |
18792 KB |
Output is correct |
14 |
Correct |
28 ms |
18748 KB |
Output is correct |
15 |
Correct |
188 ms |
27936 KB |
Output is correct |
16 |
Correct |
172 ms |
28196 KB |
Output is correct |
17 |
Correct |
172 ms |
27812 KB |
Output is correct |
18 |
Correct |
963 ms |
135272 KB |
Output is correct |
19 |
Correct |
1101 ms |
135832 KB |
Output is correct |
20 |
Correct |
1937 ms |
187528 KB |
Output is correct |
21 |
Correct |
818 ms |
72032 KB |
Output is correct |
22 |
Correct |
807 ms |
116004 KB |
Output is correct |
23 |
Correct |
2364 ms |
326424 KB |
Output is correct |
24 |
Correct |
1785 ms |
212528 KB |
Output is correct |
25 |
Correct |
132 ms |
29220 KB |
Output is correct |
26 |
Correct |
144 ms |
30240 KB |
Output is correct |
27 |
Correct |
191 ms |
27680 KB |
Output is correct |
28 |
Correct |
202 ms |
28452 KB |
Output is correct |
29 |
Correct |
307 ms |
35544 KB |
Output is correct |
30 |
Correct |
1000 ms |
133036 KB |
Output is correct |
31 |
Correct |
962 ms |
152184 KB |
Output is correct |
32 |
Correct |
1307 ms |
199744 KB |
Output is correct |
33 |
Correct |
861 ms |
108932 KB |
Output is correct |
34 |
Correct |
29 ms |
18748 KB |
Output is correct |
35 |
Correct |
29 ms |
18748 KB |
Output is correct |
36 |
Correct |
27 ms |
18740 KB |
Output is correct |
37 |
Correct |
28 ms |
18740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
18740 KB |
Output is correct |
2 |
Correct |
27 ms |
18524 KB |
Output is correct |
3 |
Correct |
27 ms |
18740 KB |
Output is correct |
4 |
Correct |
27 ms |
18744 KB |
Output is correct |
5 |
Correct |
38 ms |
18720 KB |
Output is correct |
6 |
Correct |
31 ms |
18740 KB |
Output is correct |
7 |
Correct |
46 ms |
19044 KB |
Output is correct |
8 |
Correct |
104 ms |
31272 KB |
Output is correct |
9 |
Correct |
33 ms |
18992 KB |
Output is correct |
10 |
Correct |
103 ms |
31268 KB |
Output is correct |
11 |
Correct |
32 ms |
18996 KB |
Output is correct |
12 |
Correct |
128 ms |
32036 KB |
Output is correct |
13 |
Correct |
31 ms |
18740 KB |
Output is correct |
14 |
Correct |
127 ms |
27172 KB |
Output is correct |
15 |
Correct |
31 ms |
18848 KB |
Output is correct |
16 |
Correct |
145 ms |
32452 KB |
Output is correct |
17 |
Correct |
32 ms |
18592 KB |
Output is correct |
18 |
Correct |
37 ms |
18832 KB |
Output is correct |
19 |
Correct |
32 ms |
18740 KB |
Output is correct |
20 |
Correct |
137 ms |
27352 KB |
Output is correct |
21 |
Correct |
32 ms |
18748 KB |
Output is correct |
22 |
Correct |
28 ms |
18740 KB |
Output is correct |
23 |
Correct |
185 ms |
27772 KB |
Output is correct |
24 |
Correct |
184 ms |
28040 KB |
Output is correct |
25 |
Correct |
212 ms |
27980 KB |
Output is correct |
26 |
Correct |
117 ms |
29220 KB |
Output is correct |
27 |
Correct |
177 ms |
30496 KB |
Output is correct |
28 |
Correct |
170 ms |
28016 KB |
Output is correct |
29 |
Correct |
178 ms |
28484 KB |
Output is correct |
30 |
Correct |
141 ms |
29240 KB |
Output is correct |
31 |
Correct |
140 ms |
27164 KB |
Output is correct |
32 |
Correct |
143 ms |
26916 KB |
Output is correct |
33 |
Correct |
149 ms |
35752 KB |
Output is correct |
34 |
Correct |
168 ms |
27168 KB |
Output is correct |
35 |
Correct |
117 ms |
26912 KB |
Output is correct |
36 |
Correct |
155 ms |
27020 KB |
Output is correct |
37 |
Correct |
139 ms |
27172 KB |
Output is correct |
38 |
Correct |
55 ms |
18740 KB |
Output is correct |
39 |
Correct |
27 ms |
18740 KB |
Output is correct |
40 |
Correct |
28 ms |
18792 KB |
Output is correct |
41 |
Correct |
28 ms |
18604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18736 KB |
Output is correct |
2 |
Correct |
32 ms |
18728 KB |
Output is correct |
3 |
Correct |
34 ms |
18696 KB |
Output is correct |
4 |
Correct |
27 ms |
18740 KB |
Output is correct |
5 |
Correct |
29 ms |
18792 KB |
Output is correct |
6 |
Correct |
30 ms |
18692 KB |
Output is correct |
7 |
Correct |
32 ms |
19212 KB |
Output is correct |
8 |
Correct |
107 ms |
31400 KB |
Output is correct |
9 |
Correct |
2387 ms |
373324 KB |
Output is correct |
10 |
Correct |
32 ms |
18964 KB |
Output is correct |
11 |
Correct |
132 ms |
31296 KB |
Output is correct |
12 |
Correct |
2025 ms |
398692 KB |
Output is correct |
13 |
Correct |
33 ms |
18980 KB |
Output is correct |
14 |
Correct |
127 ms |
32172 KB |
Output is correct |
15 |
Correct |
2548 ms |
376644 KB |
Output is correct |
16 |
Correct |
30 ms |
18740 KB |
Output is correct |
17 |
Correct |
29 ms |
18704 KB |
Output is correct |
18 |
Correct |
34 ms |
18740 KB |
Output is correct |
19 |
Correct |
34 ms |
18996 KB |
Output is correct |
20 |
Correct |
108 ms |
31412 KB |
Output is correct |
21 |
Correct |
2000 ms |
399244 KB |
Output is correct |
22 |
Correct |
34 ms |
18768 KB |
Output is correct |
23 |
Correct |
131 ms |
27148 KB |
Output is correct |
24 |
Correct |
289 ms |
42200 KB |
Output is correct |
25 |
Correct |
37 ms |
18988 KB |
Output is correct |
26 |
Correct |
163 ms |
32328 KB |
Output is correct |
27 |
Correct |
2073 ms |
249424 KB |
Output is correct |
28 |
Correct |
254 ms |
36372 KB |
Output is correct |
29 |
Correct |
32 ms |
18740 KB |
Output is correct |
30 |
Correct |
45 ms |
18888 KB |
Output is correct |
31 |
Correct |
557 ms |
48080 KB |
Output is correct |
32 |
Correct |
469 ms |
46216 KB |
Output is correct |
33 |
Correct |
37 ms |
18748 KB |
Output is correct |
34 |
Correct |
167 ms |
27072 KB |
Output is correct |
35 |
Correct |
452 ms |
46156 KB |
Output is correct |
36 |
Correct |
508 ms |
47344 KB |
Output is correct |
37 |
Correct |
398 ms |
44724 KB |
Output is correct |
38 |
Correct |
32 ms |
18768 KB |
Output is correct |
39 |
Correct |
33 ms |
18592 KB |
Output is correct |
40 |
Correct |
37 ms |
18636 KB |
Output is correct |
41 |
Correct |
29 ms |
18740 KB |
Output is correct |
42 |
Correct |
30 ms |
18936 KB |
Output is correct |
43 |
Correct |
30 ms |
18576 KB |
Output is correct |
44 |
Correct |
30 ms |
18688 KB |
Output is correct |
45 |
Correct |
37 ms |
19076 KB |
Output is correct |
46 |
Correct |
2356 ms |
373928 KB |
Output is correct |
47 |
Correct |
31 ms |
19004 KB |
Output is correct |
48 |
Correct |
123 ms |
32188 KB |
Output is correct |
49 |
Correct |
2337 ms |
375240 KB |
Output is correct |
50 |
Correct |
30 ms |
18748 KB |
Output is correct |
51 |
Correct |
27 ms |
18792 KB |
Output is correct |
52 |
Correct |
28 ms |
18748 KB |
Output is correct |
53 |
Correct |
188 ms |
27936 KB |
Output is correct |
54 |
Correct |
172 ms |
28196 KB |
Output is correct |
55 |
Correct |
172 ms |
27812 KB |
Output is correct |
56 |
Correct |
963 ms |
135272 KB |
Output is correct |
57 |
Correct |
1101 ms |
135832 KB |
Output is correct |
58 |
Correct |
1937 ms |
187528 KB |
Output is correct |
59 |
Correct |
818 ms |
72032 KB |
Output is correct |
60 |
Correct |
807 ms |
116004 KB |
Output is correct |
61 |
Correct |
2364 ms |
326424 KB |
Output is correct |
62 |
Correct |
1785 ms |
212528 KB |
Output is correct |
63 |
Correct |
132 ms |
29220 KB |
Output is correct |
64 |
Correct |
144 ms |
30240 KB |
Output is correct |
65 |
Correct |
191 ms |
27680 KB |
Output is correct |
66 |
Correct |
202 ms |
28452 KB |
Output is correct |
67 |
Correct |
307 ms |
35544 KB |
Output is correct |
68 |
Correct |
1000 ms |
133036 KB |
Output is correct |
69 |
Correct |
962 ms |
152184 KB |
Output is correct |
70 |
Correct |
1307 ms |
199744 KB |
Output is correct |
71 |
Correct |
861 ms |
108932 KB |
Output is correct |
72 |
Correct |
29 ms |
18748 KB |
Output is correct |
73 |
Correct |
29 ms |
18748 KB |
Output is correct |
74 |
Correct |
27 ms |
18740 KB |
Output is correct |
75 |
Correct |
28 ms |
18740 KB |
Output is correct |
76 |
Correct |
28 ms |
18740 KB |
Output is correct |
77 |
Correct |
27 ms |
18524 KB |
Output is correct |
78 |
Correct |
27 ms |
18740 KB |
Output is correct |
79 |
Correct |
27 ms |
18744 KB |
Output is correct |
80 |
Correct |
38 ms |
18720 KB |
Output is correct |
81 |
Correct |
31 ms |
18740 KB |
Output is correct |
82 |
Correct |
46 ms |
19044 KB |
Output is correct |
83 |
Correct |
104 ms |
31272 KB |
Output is correct |
84 |
Correct |
33 ms |
18992 KB |
Output is correct |
85 |
Correct |
103 ms |
31268 KB |
Output is correct |
86 |
Correct |
32 ms |
18996 KB |
Output is correct |
87 |
Correct |
128 ms |
32036 KB |
Output is correct |
88 |
Correct |
31 ms |
18740 KB |
Output is correct |
89 |
Correct |
127 ms |
27172 KB |
Output is correct |
90 |
Correct |
31 ms |
18848 KB |
Output is correct |
91 |
Correct |
145 ms |
32452 KB |
Output is correct |
92 |
Correct |
32 ms |
18592 KB |
Output is correct |
93 |
Correct |
37 ms |
18832 KB |
Output is correct |
94 |
Correct |
32 ms |
18740 KB |
Output is correct |
95 |
Correct |
137 ms |
27352 KB |
Output is correct |
96 |
Correct |
32 ms |
18748 KB |
Output is correct |
97 |
Correct |
28 ms |
18740 KB |
Output is correct |
98 |
Correct |
185 ms |
27772 KB |
Output is correct |
99 |
Correct |
184 ms |
28040 KB |
Output is correct |
100 |
Correct |
212 ms |
27980 KB |
Output is correct |
101 |
Correct |
117 ms |
29220 KB |
Output is correct |
102 |
Correct |
177 ms |
30496 KB |
Output is correct |
103 |
Correct |
170 ms |
28016 KB |
Output is correct |
104 |
Correct |
178 ms |
28484 KB |
Output is correct |
105 |
Correct |
141 ms |
29240 KB |
Output is correct |
106 |
Correct |
140 ms |
27164 KB |
Output is correct |
107 |
Correct |
143 ms |
26916 KB |
Output is correct |
108 |
Correct |
149 ms |
35752 KB |
Output is correct |
109 |
Correct |
168 ms |
27168 KB |
Output is correct |
110 |
Correct |
117 ms |
26912 KB |
Output is correct |
111 |
Correct |
155 ms |
27020 KB |
Output is correct |
112 |
Correct |
139 ms |
27172 KB |
Output is correct |
113 |
Correct |
55 ms |
18740 KB |
Output is correct |
114 |
Correct |
27 ms |
18740 KB |
Output is correct |
115 |
Correct |
28 ms |
18792 KB |
Output is correct |
116 |
Correct |
28 ms |
18604 KB |
Output is correct |
117 |
Correct |
27 ms |
18740 KB |
Output is correct |
118 |
Correct |
50 ms |
18740 KB |
Output is correct |
119 |
Correct |
28 ms |
18740 KB |
Output is correct |
120 |
Correct |
29 ms |
18748 KB |
Output is correct |
121 |
Correct |
26 ms |
18800 KB |
Output is correct |
122 |
Correct |
29 ms |
18612 KB |
Output is correct |
123 |
Correct |
31 ms |
19200 KB |
Output is correct |
124 |
Correct |
119 ms |
32036 KB |
Output is correct |
125 |
Correct |
2299 ms |
374604 KB |
Output is correct |
126 |
Correct |
33 ms |
19016 KB |
Output is correct |
127 |
Correct |
104 ms |
31332 KB |
Output is correct |
128 |
Correct |
2031 ms |
399352 KB |
Output is correct |
129 |
Correct |
37 ms |
18988 KB |
Output is correct |
130 |
Correct |
124 ms |
32132 KB |
Output is correct |
131 |
Correct |
2319 ms |
376920 KB |
Output is correct |
132 |
Correct |
36 ms |
18796 KB |
Output is correct |
133 |
Correct |
130 ms |
27060 KB |
Output is correct |
134 |
Correct |
320 ms |
42824 KB |
Output is correct |
135 |
Correct |
33 ms |
18992 KB |
Output is correct |
136 |
Correct |
141 ms |
32392 KB |
Output is correct |
137 |
Correct |
1987 ms |
249192 KB |
Output is correct |
138 |
Correct |
267 ms |
36504 KB |
Output is correct |
139 |
Correct |
30 ms |
18748 KB |
Output is correct |
140 |
Correct |
34 ms |
18748 KB |
Output is correct |
141 |
Correct |
523 ms |
47216 KB |
Output is correct |
142 |
Correct |
410 ms |
46416 KB |
Output is correct |
143 |
Correct |
30 ms |
18740 KB |
Output is correct |
144 |
Correct |
139 ms |
27172 KB |
Output is correct |
145 |
Correct |
480 ms |
46152 KB |
Output is correct |
146 |
Correct |
447 ms |
47308 KB |
Output is correct |
147 |
Correct |
363 ms |
44640 KB |
Output is correct |
148 |
Correct |
51 ms |
18740 KB |
Output is correct |
149 |
Correct |
34 ms |
18716 KB |
Output is correct |
150 |
Correct |
176 ms |
28272 KB |
Output is correct |
151 |
Correct |
182 ms |
27984 KB |
Output is correct |
152 |
Correct |
184 ms |
27328 KB |
Output is correct |
153 |
Correct |
1034 ms |
136196 KB |
Output is correct |
154 |
Correct |
1206 ms |
135584 KB |
Output is correct |
155 |
Correct |
2005 ms |
187780 KB |
Output is correct |
156 |
Correct |
873 ms |
71168 KB |
Output is correct |
157 |
Correct |
864 ms |
115696 KB |
Output is correct |
158 |
Correct |
2584 ms |
326916 KB |
Output is correct |
159 |
Correct |
1841 ms |
212836 KB |
Output is correct |
160 |
Correct |
118 ms |
29216 KB |
Output is correct |
161 |
Correct |
139 ms |
30236 KB |
Output is correct |
162 |
Correct |
162 ms |
27940 KB |
Output is correct |
163 |
Correct |
150 ms |
28452 KB |
Output is correct |
164 |
Correct |
218 ms |
35364 KB |
Output is correct |
165 |
Correct |
877 ms |
133752 KB |
Output is correct |
166 |
Correct |
925 ms |
147320 KB |
Output is correct |
167 |
Correct |
1267 ms |
199836 KB |
Output is correct |
168 |
Correct |
911 ms |
108124 KB |
Output is correct |
169 |
Correct |
176 ms |
29268 KB |
Output is correct |
170 |
Correct |
144 ms |
27152 KB |
Output is correct |
171 |
Correct |
152 ms |
26776 KB |
Output is correct |
172 |
Correct |
155 ms |
35520 KB |
Output is correct |
173 |
Correct |
170 ms |
27172 KB |
Output is correct |
174 |
Correct |
134 ms |
26916 KB |
Output is correct |
175 |
Correct |
146 ms |
26952 KB |
Output is correct |
176 |
Correct |
173 ms |
26608 KB |
Output is correct |
177 |
Correct |
2649 ms |
295144 KB |
Output is correct |
178 |
Correct |
2788 ms |
90680 KB |
Output is correct |
179 |
Correct |
867 ms |
96276 KB |
Output is correct |
180 |
Correct |
251 ms |
28860 KB |
Output is correct |
181 |
Correct |
690 ms |
48660 KB |
Output is correct |
182 |
Correct |
232 ms |
27892 KB |
Output is correct |
183 |
Correct |
155 ms |
27304 KB |
Output is correct |
184 |
Correct |
229 ms |
27600 KB |
Output is correct |
185 |
Correct |
144 ms |
27044 KB |
Output is correct |
186 |
Correct |
579 ms |
42352 KB |
Output is correct |
187 |
Correct |
321 ms |
37640 KB |
Output is correct |
188 |
Correct |
714 ms |
45528 KB |
Output is correct |
189 |
Correct |
294 ms |
40196 KB |
Output is correct |
190 |
Correct |
472 ms |
39012 KB |
Output is correct |
191 |
Correct |
269 ms |
36596 KB |
Output is correct |
192 |
Correct |
329 ms |
29624 KB |
Output is correct |
193 |
Correct |
415 ms |
44940 KB |
Output is correct |
194 |
Correct |
1374 ms |
142700 KB |
Output is correct |
195 |
Correct |
42 ms |
18744 KB |
Output is correct |
196 |
Correct |
34 ms |
18772 KB |
Output is correct |
197 |
Correct |
35 ms |
18712 KB |
Output is correct |
198 |
Correct |
33 ms |
18608 KB |
Output is correct |