# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
987112 |
2024-05-21T22:29:11 Z |
OAleksa |
Mergers (JOI19_mergers) |
C++14 |
|
1716 ms |
262144 KB |
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 5e5 + 69;
int n, k, a[N], ans, cc[N], is[N], have[N];
vector<int> g[N], c[N];
vector<int> t[N];
map<int, int> cnt[N];
void dfs(int v, int p) {
cnt[v][a[v]] = 1;
if (c[a[v]].size() == 1)
have[v] = 1;
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
int o = have[v];
if (cnt[u].size() > cnt[v].size()) {
have[v] += have[u];
swap(cnt[u], cnt[v]);
}
else
o = 0;
for (auto j : cnt[u]) {
cnt[v][j.f] += j.s;
have[v] += (cnt[v][j.f] == (int)c[j.f].size());
}
have[v] -= o;
}
assert(have[v] <= cnt[v].size());
if (have[v] == cnt[v].size())
cc[v] = 1;
}
void build(int v, int p, int c) {
int x = c;
if (cc[v] && v > 1) {
t[v].push_back(c);
t[c].push_back(v);
x = v;
}
for (auto u : g[v]) {
if (u == p)
continue;
build(u, v, x);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> k;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1;i <= n;i++) {
cin >> a[i];
c[a[i]].push_back(i);
}
dfs(1, 0);
build(1, 0, 1);
int o = 0;
assert(cc[1]);
for (int i = 1;i <= n;i++) {
if (cc[i] && (int)t[i].size() == 1)
o += 1;
}
ans = (o + 1) / 2;
cout << ans << '\n';
}
return 0;
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from mergers.cpp:1:
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:31:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | assert(have[v] <= cnt[v].size());
| ~~~~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:32:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | if (have[v] == cnt[v].size())
| ~~~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
64088 KB |
Output is correct |
2 |
Correct |
15 ms |
64092 KB |
Output is correct |
3 |
Correct |
16 ms |
64092 KB |
Output is correct |
4 |
Correct |
19 ms |
64092 KB |
Output is correct |
5 |
Correct |
19 ms |
64092 KB |
Output is correct |
6 |
Correct |
17 ms |
64092 KB |
Output is correct |
7 |
Correct |
18 ms |
64240 KB |
Output is correct |
8 |
Correct |
18 ms |
64260 KB |
Output is correct |
9 |
Correct |
19 ms |
64092 KB |
Output is correct |
10 |
Correct |
16 ms |
64200 KB |
Output is correct |
11 |
Correct |
21 ms |
64368 KB |
Output is correct |
12 |
Correct |
19 ms |
62040 KB |
Output is correct |
13 |
Correct |
19 ms |
64092 KB |
Output is correct |
14 |
Correct |
19 ms |
64092 KB |
Output is correct |
15 |
Correct |
19 ms |
64088 KB |
Output is correct |
16 |
Correct |
19 ms |
64092 KB |
Output is correct |
17 |
Correct |
19 ms |
64092 KB |
Output is correct |
18 |
Correct |
19 ms |
64092 KB |
Output is correct |
19 |
Correct |
18 ms |
64092 KB |
Output is correct |
20 |
Correct |
15 ms |
64092 KB |
Output is correct |
21 |
Correct |
16 ms |
64092 KB |
Output is correct |
22 |
Correct |
19 ms |
64092 KB |
Output is correct |
23 |
Correct |
15 ms |
64092 KB |
Output is correct |
24 |
Correct |
20 ms |
64088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
64088 KB |
Output is correct |
2 |
Correct |
15 ms |
64092 KB |
Output is correct |
3 |
Correct |
16 ms |
64092 KB |
Output is correct |
4 |
Correct |
19 ms |
64092 KB |
Output is correct |
5 |
Correct |
19 ms |
64092 KB |
Output is correct |
6 |
Correct |
17 ms |
64092 KB |
Output is correct |
7 |
Correct |
18 ms |
64240 KB |
Output is correct |
8 |
Correct |
18 ms |
64260 KB |
Output is correct |
9 |
Correct |
19 ms |
64092 KB |
Output is correct |
10 |
Correct |
16 ms |
64200 KB |
Output is correct |
11 |
Correct |
21 ms |
64368 KB |
Output is correct |
12 |
Correct |
19 ms |
62040 KB |
Output is correct |
13 |
Correct |
19 ms |
64092 KB |
Output is correct |
14 |
Correct |
19 ms |
64092 KB |
Output is correct |
15 |
Correct |
19 ms |
64088 KB |
Output is correct |
16 |
Correct |
19 ms |
64092 KB |
Output is correct |
17 |
Correct |
19 ms |
64092 KB |
Output is correct |
18 |
Correct |
19 ms |
64092 KB |
Output is correct |
19 |
Correct |
18 ms |
64092 KB |
Output is correct |
20 |
Correct |
15 ms |
64092 KB |
Output is correct |
21 |
Correct |
16 ms |
64092 KB |
Output is correct |
22 |
Correct |
19 ms |
64092 KB |
Output is correct |
23 |
Correct |
15 ms |
64092 KB |
Output is correct |
24 |
Correct |
20 ms |
64088 KB |
Output is correct |
25 |
Correct |
15 ms |
64088 KB |
Output is correct |
26 |
Correct |
18 ms |
64860 KB |
Output is correct |
27 |
Correct |
17 ms |
64344 KB |
Output is correct |
28 |
Correct |
23 ms |
64860 KB |
Output is correct |
29 |
Correct |
23 ms |
65116 KB |
Output is correct |
30 |
Correct |
21 ms |
64604 KB |
Output is correct |
31 |
Correct |
18 ms |
64092 KB |
Output is correct |
32 |
Correct |
22 ms |
65112 KB |
Output is correct |
33 |
Correct |
18 ms |
64244 KB |
Output is correct |
34 |
Correct |
17 ms |
64348 KB |
Output is correct |
35 |
Correct |
24 ms |
65004 KB |
Output is correct |
36 |
Correct |
17 ms |
64348 KB |
Output is correct |
37 |
Correct |
22 ms |
64860 KB |
Output is correct |
38 |
Correct |
18 ms |
64092 KB |
Output is correct |
39 |
Correct |
22 ms |
64936 KB |
Output is correct |
40 |
Correct |
18 ms |
64348 KB |
Output is correct |
41 |
Correct |
22 ms |
64720 KB |
Output is correct |
42 |
Correct |
21 ms |
64604 KB |
Output is correct |
43 |
Correct |
20 ms |
65112 KB |
Output is correct |
44 |
Correct |
18 ms |
64088 KB |
Output is correct |
45 |
Correct |
21 ms |
64856 KB |
Output is correct |
46 |
Correct |
22 ms |
64600 KB |
Output is correct |
47 |
Correct |
20 ms |
64092 KB |
Output is correct |
48 |
Correct |
18 ms |
64600 KB |
Output is correct |
49 |
Correct |
22 ms |
64604 KB |
Output is correct |
50 |
Correct |
22 ms |
64860 KB |
Output is correct |
51 |
Correct |
20 ms |
64604 KB |
Output is correct |
52 |
Correct |
19 ms |
64348 KB |
Output is correct |
53 |
Correct |
21 ms |
64604 KB |
Output is correct |
54 |
Correct |
21 ms |
64604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
64088 KB |
Output is correct |
2 |
Correct |
15 ms |
64092 KB |
Output is correct |
3 |
Correct |
16 ms |
64092 KB |
Output is correct |
4 |
Correct |
19 ms |
64092 KB |
Output is correct |
5 |
Correct |
19 ms |
64092 KB |
Output is correct |
6 |
Correct |
17 ms |
64092 KB |
Output is correct |
7 |
Correct |
18 ms |
64240 KB |
Output is correct |
8 |
Correct |
18 ms |
64260 KB |
Output is correct |
9 |
Correct |
19 ms |
64092 KB |
Output is correct |
10 |
Correct |
16 ms |
64200 KB |
Output is correct |
11 |
Correct |
21 ms |
64368 KB |
Output is correct |
12 |
Correct |
19 ms |
62040 KB |
Output is correct |
13 |
Correct |
19 ms |
64092 KB |
Output is correct |
14 |
Correct |
19 ms |
64092 KB |
Output is correct |
15 |
Correct |
19 ms |
64088 KB |
Output is correct |
16 |
Correct |
19 ms |
64092 KB |
Output is correct |
17 |
Correct |
19 ms |
64092 KB |
Output is correct |
18 |
Correct |
19 ms |
64092 KB |
Output is correct |
19 |
Correct |
18 ms |
64092 KB |
Output is correct |
20 |
Correct |
15 ms |
64092 KB |
Output is correct |
21 |
Correct |
16 ms |
64092 KB |
Output is correct |
22 |
Correct |
19 ms |
64092 KB |
Output is correct |
23 |
Correct |
15 ms |
64092 KB |
Output is correct |
24 |
Correct |
20 ms |
64088 KB |
Output is correct |
25 |
Correct |
15 ms |
64092 KB |
Output is correct |
26 |
Correct |
79 ms |
72904 KB |
Output is correct |
27 |
Correct |
106 ms |
79176 KB |
Output is correct |
28 |
Correct |
21 ms |
64348 KB |
Output is correct |
29 |
Correct |
18 ms |
64088 KB |
Output is correct |
30 |
Correct |
18 ms |
64092 KB |
Output is correct |
31 |
Correct |
74 ms |
72528 KB |
Output is correct |
32 |
Correct |
17 ms |
64344 KB |
Output is correct |
33 |
Correct |
102 ms |
87044 KB |
Output is correct |
34 |
Correct |
105 ms |
77908 KB |
Output is correct |
35 |
Correct |
20 ms |
64344 KB |
Output is correct |
36 |
Correct |
131 ms |
74300 KB |
Output is correct |
37 |
Correct |
20 ms |
64344 KB |
Output is correct |
38 |
Correct |
20 ms |
64444 KB |
Output is correct |
39 |
Correct |
85 ms |
72780 KB |
Output is correct |
40 |
Correct |
16 ms |
64856 KB |
Output is correct |
41 |
Correct |
83 ms |
72392 KB |
Output is correct |
42 |
Correct |
99 ms |
77136 KB |
Output is correct |
43 |
Correct |
16 ms |
64252 KB |
Output is correct |
44 |
Correct |
106 ms |
87500 KB |
Output is correct |
45 |
Correct |
96 ms |
81232 KB |
Output is correct |
46 |
Correct |
20 ms |
64344 KB |
Output is correct |
47 |
Correct |
21 ms |
64604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
72812 KB |
Output is correct |
2 |
Correct |
162 ms |
83688 KB |
Output is correct |
3 |
Correct |
21 ms |
64856 KB |
Output is correct |
4 |
Correct |
21 ms |
64604 KB |
Output is correct |
5 |
Correct |
15 ms |
64092 KB |
Output is correct |
6 |
Correct |
15 ms |
64092 KB |
Output is correct |
7 |
Correct |
21 ms |
64600 KB |
Output is correct |
8 |
Correct |
181 ms |
86864 KB |
Output is correct |
9 |
Correct |
20 ms |
64348 KB |
Output is correct |
10 |
Correct |
105 ms |
73004 KB |
Output is correct |
11 |
Correct |
18 ms |
64092 KB |
Output is correct |
12 |
Correct |
95 ms |
72788 KB |
Output is correct |
13 |
Correct |
151 ms |
86012 KB |
Output is correct |
14 |
Correct |
254 ms |
102740 KB |
Output is correct |
15 |
Correct |
79 ms |
72772 KB |
Output is correct |
16 |
Correct |
21 ms |
64604 KB |
Output is correct |
17 |
Correct |
18 ms |
64092 KB |
Output is correct |
18 |
Correct |
135 ms |
83272 KB |
Output is correct |
19 |
Correct |
214 ms |
93424 KB |
Output is correct |
20 |
Correct |
22 ms |
64604 KB |
Output is correct |
21 |
Correct |
19 ms |
64092 KB |
Output is correct |
22 |
Correct |
137 ms |
76948 KB |
Output is correct |
23 |
Correct |
21 ms |
64600 KB |
Output is correct |
24 |
Correct |
106 ms |
75320 KB |
Output is correct |
25 |
Correct |
196 ms |
90996 KB |
Output is correct |
26 |
Correct |
21 ms |
64604 KB |
Output is correct |
27 |
Correct |
21 ms |
65032 KB |
Output is correct |
28 |
Correct |
20 ms |
64444 KB |
Output is correct |
29 |
Correct |
21 ms |
64348 KB |
Output is correct |
30 |
Correct |
20 ms |
64632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
64088 KB |
Output is correct |
2 |
Correct |
15 ms |
64092 KB |
Output is correct |
3 |
Correct |
16 ms |
64092 KB |
Output is correct |
4 |
Correct |
19 ms |
64092 KB |
Output is correct |
5 |
Correct |
19 ms |
64092 KB |
Output is correct |
6 |
Correct |
17 ms |
64092 KB |
Output is correct |
7 |
Correct |
18 ms |
64240 KB |
Output is correct |
8 |
Correct |
18 ms |
64260 KB |
Output is correct |
9 |
Correct |
19 ms |
64092 KB |
Output is correct |
10 |
Correct |
16 ms |
64200 KB |
Output is correct |
11 |
Correct |
21 ms |
64368 KB |
Output is correct |
12 |
Correct |
19 ms |
62040 KB |
Output is correct |
13 |
Correct |
19 ms |
64092 KB |
Output is correct |
14 |
Correct |
19 ms |
64092 KB |
Output is correct |
15 |
Correct |
19 ms |
64088 KB |
Output is correct |
16 |
Correct |
19 ms |
64092 KB |
Output is correct |
17 |
Correct |
19 ms |
64092 KB |
Output is correct |
18 |
Correct |
19 ms |
64092 KB |
Output is correct |
19 |
Correct |
18 ms |
64092 KB |
Output is correct |
20 |
Correct |
15 ms |
64092 KB |
Output is correct |
21 |
Correct |
16 ms |
64092 KB |
Output is correct |
22 |
Correct |
19 ms |
64092 KB |
Output is correct |
23 |
Correct |
15 ms |
64092 KB |
Output is correct |
24 |
Correct |
20 ms |
64088 KB |
Output is correct |
25 |
Correct |
15 ms |
64088 KB |
Output is correct |
26 |
Correct |
18 ms |
64860 KB |
Output is correct |
27 |
Correct |
17 ms |
64344 KB |
Output is correct |
28 |
Correct |
23 ms |
64860 KB |
Output is correct |
29 |
Correct |
23 ms |
65116 KB |
Output is correct |
30 |
Correct |
21 ms |
64604 KB |
Output is correct |
31 |
Correct |
18 ms |
64092 KB |
Output is correct |
32 |
Correct |
22 ms |
65112 KB |
Output is correct |
33 |
Correct |
18 ms |
64244 KB |
Output is correct |
34 |
Correct |
17 ms |
64348 KB |
Output is correct |
35 |
Correct |
24 ms |
65004 KB |
Output is correct |
36 |
Correct |
17 ms |
64348 KB |
Output is correct |
37 |
Correct |
22 ms |
64860 KB |
Output is correct |
38 |
Correct |
18 ms |
64092 KB |
Output is correct |
39 |
Correct |
22 ms |
64936 KB |
Output is correct |
40 |
Correct |
18 ms |
64348 KB |
Output is correct |
41 |
Correct |
22 ms |
64720 KB |
Output is correct |
42 |
Correct |
21 ms |
64604 KB |
Output is correct |
43 |
Correct |
20 ms |
65112 KB |
Output is correct |
44 |
Correct |
18 ms |
64088 KB |
Output is correct |
45 |
Correct |
21 ms |
64856 KB |
Output is correct |
46 |
Correct |
22 ms |
64600 KB |
Output is correct |
47 |
Correct |
20 ms |
64092 KB |
Output is correct |
48 |
Correct |
18 ms |
64600 KB |
Output is correct |
49 |
Correct |
22 ms |
64604 KB |
Output is correct |
50 |
Correct |
22 ms |
64860 KB |
Output is correct |
51 |
Correct |
20 ms |
64604 KB |
Output is correct |
52 |
Correct |
19 ms |
64348 KB |
Output is correct |
53 |
Correct |
21 ms |
64604 KB |
Output is correct |
54 |
Correct |
21 ms |
64604 KB |
Output is correct |
55 |
Correct |
15 ms |
64092 KB |
Output is correct |
56 |
Correct |
79 ms |
72904 KB |
Output is correct |
57 |
Correct |
106 ms |
79176 KB |
Output is correct |
58 |
Correct |
21 ms |
64348 KB |
Output is correct |
59 |
Correct |
18 ms |
64088 KB |
Output is correct |
60 |
Correct |
18 ms |
64092 KB |
Output is correct |
61 |
Correct |
74 ms |
72528 KB |
Output is correct |
62 |
Correct |
17 ms |
64344 KB |
Output is correct |
63 |
Correct |
102 ms |
87044 KB |
Output is correct |
64 |
Correct |
105 ms |
77908 KB |
Output is correct |
65 |
Correct |
20 ms |
64344 KB |
Output is correct |
66 |
Correct |
131 ms |
74300 KB |
Output is correct |
67 |
Correct |
20 ms |
64344 KB |
Output is correct |
68 |
Correct |
20 ms |
64444 KB |
Output is correct |
69 |
Correct |
85 ms |
72780 KB |
Output is correct |
70 |
Correct |
16 ms |
64856 KB |
Output is correct |
71 |
Correct |
83 ms |
72392 KB |
Output is correct |
72 |
Correct |
99 ms |
77136 KB |
Output is correct |
73 |
Correct |
16 ms |
64252 KB |
Output is correct |
74 |
Correct |
106 ms |
87500 KB |
Output is correct |
75 |
Correct |
96 ms |
81232 KB |
Output is correct |
76 |
Correct |
20 ms |
64344 KB |
Output is correct |
77 |
Correct |
21 ms |
64604 KB |
Output is correct |
78 |
Correct |
79 ms |
72812 KB |
Output is correct |
79 |
Correct |
162 ms |
83688 KB |
Output is correct |
80 |
Correct |
21 ms |
64856 KB |
Output is correct |
81 |
Correct |
21 ms |
64604 KB |
Output is correct |
82 |
Correct |
15 ms |
64092 KB |
Output is correct |
83 |
Correct |
15 ms |
64092 KB |
Output is correct |
84 |
Correct |
21 ms |
64600 KB |
Output is correct |
85 |
Correct |
181 ms |
86864 KB |
Output is correct |
86 |
Correct |
20 ms |
64348 KB |
Output is correct |
87 |
Correct |
105 ms |
73004 KB |
Output is correct |
88 |
Correct |
18 ms |
64092 KB |
Output is correct |
89 |
Correct |
95 ms |
72788 KB |
Output is correct |
90 |
Correct |
151 ms |
86012 KB |
Output is correct |
91 |
Correct |
254 ms |
102740 KB |
Output is correct |
92 |
Correct |
79 ms |
72772 KB |
Output is correct |
93 |
Correct |
21 ms |
64604 KB |
Output is correct |
94 |
Correct |
18 ms |
64092 KB |
Output is correct |
95 |
Correct |
135 ms |
83272 KB |
Output is correct |
96 |
Correct |
214 ms |
93424 KB |
Output is correct |
97 |
Correct |
22 ms |
64604 KB |
Output is correct |
98 |
Correct |
19 ms |
64092 KB |
Output is correct |
99 |
Correct |
137 ms |
76948 KB |
Output is correct |
100 |
Correct |
21 ms |
64600 KB |
Output is correct |
101 |
Correct |
106 ms |
75320 KB |
Output is correct |
102 |
Correct |
196 ms |
90996 KB |
Output is correct |
103 |
Correct |
21 ms |
64604 KB |
Output is correct |
104 |
Correct |
21 ms |
65032 KB |
Output is correct |
105 |
Correct |
20 ms |
64444 KB |
Output is correct |
106 |
Correct |
21 ms |
64348 KB |
Output is correct |
107 |
Correct |
20 ms |
64632 KB |
Output is correct |
108 |
Correct |
1066 ms |
124288 KB |
Output is correct |
109 |
Correct |
912 ms |
144104 KB |
Output is correct |
110 |
Correct |
1074 ms |
166704 KB |
Output is correct |
111 |
Correct |
1716 ms |
216264 KB |
Output is correct |
112 |
Correct |
1493 ms |
194976 KB |
Output is correct |
113 |
Correct |
1153 ms |
158004 KB |
Output is correct |
114 |
Correct |
670 ms |
107372 KB |
Output is correct |
115 |
Correct |
655 ms |
107708 KB |
Output is correct |
116 |
Correct |
1179 ms |
181996 KB |
Output is correct |
117 |
Runtime error |
1205 ms |
262144 KB |
Execution killed with signal 9 |
118 |
Halted |
0 ms |
0 KB |
- |