# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
521082 |
2022-01-31T17:22:53 Z |
cig32 |
Mergers (JOI19_mergers) |
C++17 |
|
3000 ms |
92740 KB |
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 5e5 + 10;
#define int long long
vector<int> adj[MAXN];
int fin[MAXN];
int s[MAXN];
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
vector<int> e;
int dep[MAXN];
int tin[MAXN];
int tim = 0;
void dfs1(int node, int prev, int cur) {
e.push_back(node);
dep[node] = cur;
tin[node] = ++tim;
for(int x: adj[node]) {
if(x != prev) {
dfs1(x, node, cur + 1);
e.push_back(node);
}
}
}
pair<int, int> st[8*MAXN];
void u(int l, int r, int tar, int idx, int val, int o) {
if(l == r) {
st[idx] = {val, o};
return;
}
int mid = (l+r) >> 1;
if(tar <= mid) u(l, mid, tar, 2*idx+1, val, o);
else u(mid+1, r, tar, 2*idx+2, val, o);
st[idx] = min(st[2*idx+1], st[2*idx+2]);
}
pair<int, int> qu(int l, int r, int constl, int constr, int idx) {
if(l<=constl && constr<=r) return st[idx];
int mid = (constl + constr) >> 1;
if(mid < l || r < constl) return qu(l, r, mid+1, constr, 2*idx+2);
if(constr < l || r < mid+1) return qu(l, r, constl, mid, 2*idx+1);
return min(
qu(l, r, constl, mid, 2*idx+1),
qu(l, r, mid+1, constr, 2*idx+2)
);
}
void update(int i, int v, int key) {
u(0, MAXN-1, i, 0, v, key);
}
int l[MAXN], r[MAXN];
int lca(int x, int y) {
int m1 = min(l[x], l[y]);
int m2 = max(r[x], r[y]);
return qu(m1, m2, 0, MAXN-1, 0).second;
}
int delta[MAXN];
bool cmp(int a, int b) {
return tin[a] < tin[b];
}
map<pair<int, int>, bool> lit;
int dfs2(int node, int prev) {
int s = delta[node];
for(int x: adj[node]) {
if(x != prev) s += dfs2(x, node);
}
if(s == 0) lit[{node, prev}] = lit[{prev, node}] = 1;
return s;
}
int32_t main() {
int n,k;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 >> s[i];
for(int i=0; i<=n; i++) dsu[i] = i;
dfs1(1, -1, 0);
for(int i=0; i<e.size(); i++) r[e[i]] = i + 1;
for(int i=e.size()-1; i>=0; i--) l[e[i]] = i + 1;
for(int i=0; i<e.size(); i++) update(i + 1, dep[e[i]], e[i]);
vector<int> wow[k+1];
for(int i=1; i<=n; i++) {
wow[s[i]].push_back(i);
}
for(int i=1; i<=k; i++) {
if(wow[i].size() == 1) continue;
sort(wow[i].begin(), wow[i].end(), cmp);
int l = lca(wow[i][0], wow[i][wow[i].size() - 1]);
for(int j=0; j<wow[i].size(); j++) {
delta[wow[i][j]]++;
}
delta[l] -= wow[i].size();
}
dfs2(1, -1);
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(!lit[{i, x}]) {
union_(i, x);
}
}
}
lit.clear();
for(int i=1; i<=n; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && lit[{set_of(i), set_of(x)}] == 0) {
lit[{set_of(i), set_of(x)}] = 1;
lit[{set_of(x), set_of(i)}] = 1;
fin[set_of(i)]++;
fin[set_of(x)]++;
}
}
}
int s = 0;
for(int i=1; i<=n; i++) {
s += (fin[i] == 1);
}
cout << (s + 1) / 2 << "\n";
}
Compilation message
mergers.cpp: In function 'int32_t main()':
mergers.cpp:89:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i=0; i<e.size(); i++) r[e[i]] = i + 1;
| ~^~~~~~~~~
mergers.cpp:91:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i=0; i<e.size(); i++) update(i + 1, dep[e[i]], e[i]);
| ~^~~~~~~~~
mergers.cpp:100:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int j=0; j<wow[i].size(); j++) {
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12108 KB |
Output is correct |
2 |
Correct |
6 ms |
12108 KB |
Output is correct |
3 |
Correct |
6 ms |
12108 KB |
Output is correct |
4 |
Correct |
6 ms |
12092 KB |
Output is correct |
5 |
Correct |
6 ms |
12108 KB |
Output is correct |
6 |
Correct |
6 ms |
12108 KB |
Output is correct |
7 |
Correct |
6 ms |
12108 KB |
Output is correct |
8 |
Correct |
7 ms |
12108 KB |
Output is correct |
9 |
Correct |
6 ms |
12108 KB |
Output is correct |
10 |
Correct |
6 ms |
12108 KB |
Output is correct |
11 |
Correct |
6 ms |
12176 KB |
Output is correct |
12 |
Correct |
9 ms |
12108 KB |
Output is correct |
13 |
Correct |
6 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12112 KB |
Output is correct |
15 |
Correct |
7 ms |
12108 KB |
Output is correct |
16 |
Correct |
6 ms |
12164 KB |
Output is correct |
17 |
Correct |
6 ms |
12108 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
5 ms |
12108 KB |
Output is correct |
20 |
Correct |
7 ms |
12108 KB |
Output is correct |
21 |
Correct |
7 ms |
12108 KB |
Output is correct |
22 |
Correct |
7 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12140 KB |
Output is correct |
24 |
Correct |
6 ms |
12164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12108 KB |
Output is correct |
2 |
Correct |
6 ms |
12108 KB |
Output is correct |
3 |
Correct |
6 ms |
12108 KB |
Output is correct |
4 |
Correct |
6 ms |
12092 KB |
Output is correct |
5 |
Correct |
6 ms |
12108 KB |
Output is correct |
6 |
Correct |
6 ms |
12108 KB |
Output is correct |
7 |
Correct |
6 ms |
12108 KB |
Output is correct |
8 |
Correct |
7 ms |
12108 KB |
Output is correct |
9 |
Correct |
6 ms |
12108 KB |
Output is correct |
10 |
Correct |
6 ms |
12108 KB |
Output is correct |
11 |
Correct |
6 ms |
12176 KB |
Output is correct |
12 |
Correct |
9 ms |
12108 KB |
Output is correct |
13 |
Correct |
6 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12112 KB |
Output is correct |
15 |
Correct |
7 ms |
12108 KB |
Output is correct |
16 |
Correct |
6 ms |
12164 KB |
Output is correct |
17 |
Correct |
6 ms |
12108 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
5 ms |
12108 KB |
Output is correct |
20 |
Correct |
7 ms |
12108 KB |
Output is correct |
21 |
Correct |
7 ms |
12108 KB |
Output is correct |
22 |
Correct |
7 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12140 KB |
Output is correct |
24 |
Correct |
6 ms |
12164 KB |
Output is correct |
25 |
Correct |
7 ms |
12108 KB |
Output is correct |
26 |
Correct |
12 ms |
13132 KB |
Output is correct |
27 |
Correct |
11 ms |
13132 KB |
Output is correct |
28 |
Correct |
12 ms |
13388 KB |
Output is correct |
29 |
Correct |
17 ms |
13120 KB |
Output is correct |
30 |
Correct |
11 ms |
13076 KB |
Output is correct |
31 |
Correct |
6 ms |
12108 KB |
Output is correct |
32 |
Correct |
14 ms |
13328 KB |
Output is correct |
33 |
Correct |
7 ms |
12084 KB |
Output is correct |
34 |
Correct |
11 ms |
13004 KB |
Output is correct |
35 |
Correct |
13 ms |
13204 KB |
Output is correct |
36 |
Correct |
11 ms |
13084 KB |
Output is correct |
37 |
Correct |
11 ms |
13076 KB |
Output is correct |
38 |
Correct |
7 ms |
12064 KB |
Output is correct |
39 |
Correct |
11 ms |
13132 KB |
Output is correct |
40 |
Correct |
13 ms |
12960 KB |
Output is correct |
41 |
Correct |
12 ms |
13088 KB |
Output is correct |
42 |
Correct |
12 ms |
13132 KB |
Output is correct |
43 |
Correct |
11 ms |
13388 KB |
Output is correct |
44 |
Correct |
7 ms |
12108 KB |
Output is correct |
45 |
Correct |
12 ms |
13268 KB |
Output is correct |
46 |
Correct |
13 ms |
13132 KB |
Output is correct |
47 |
Correct |
7 ms |
12108 KB |
Output is correct |
48 |
Correct |
17 ms |
13028 KB |
Output is correct |
49 |
Correct |
14 ms |
13176 KB |
Output is correct |
50 |
Correct |
15 ms |
13188 KB |
Output is correct |
51 |
Correct |
13 ms |
13024 KB |
Output is correct |
52 |
Correct |
12 ms |
13004 KB |
Output is correct |
53 |
Correct |
13 ms |
13064 KB |
Output is correct |
54 |
Correct |
12 ms |
13004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12108 KB |
Output is correct |
2 |
Correct |
6 ms |
12108 KB |
Output is correct |
3 |
Correct |
6 ms |
12108 KB |
Output is correct |
4 |
Correct |
6 ms |
12092 KB |
Output is correct |
5 |
Correct |
6 ms |
12108 KB |
Output is correct |
6 |
Correct |
6 ms |
12108 KB |
Output is correct |
7 |
Correct |
6 ms |
12108 KB |
Output is correct |
8 |
Correct |
7 ms |
12108 KB |
Output is correct |
9 |
Correct |
6 ms |
12108 KB |
Output is correct |
10 |
Correct |
6 ms |
12108 KB |
Output is correct |
11 |
Correct |
6 ms |
12176 KB |
Output is correct |
12 |
Correct |
9 ms |
12108 KB |
Output is correct |
13 |
Correct |
6 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12112 KB |
Output is correct |
15 |
Correct |
7 ms |
12108 KB |
Output is correct |
16 |
Correct |
6 ms |
12164 KB |
Output is correct |
17 |
Correct |
6 ms |
12108 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
5 ms |
12108 KB |
Output is correct |
20 |
Correct |
7 ms |
12108 KB |
Output is correct |
21 |
Correct |
7 ms |
12108 KB |
Output is correct |
22 |
Correct |
7 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12140 KB |
Output is correct |
24 |
Correct |
6 ms |
12164 KB |
Output is correct |
25 |
Correct |
7 ms |
12108 KB |
Output is correct |
26 |
Correct |
226 ms |
43104 KB |
Output is correct |
27 |
Correct |
208 ms |
42752 KB |
Output is correct |
28 |
Correct |
11 ms |
13004 KB |
Output is correct |
29 |
Correct |
7 ms |
12108 KB |
Output is correct |
30 |
Correct |
6 ms |
12124 KB |
Output is correct |
31 |
Correct |
220 ms |
42948 KB |
Output is correct |
32 |
Correct |
11 ms |
13004 KB |
Output is correct |
33 |
Correct |
222 ms |
52380 KB |
Output is correct |
34 |
Correct |
202 ms |
42864 KB |
Output is correct |
35 |
Correct |
12 ms |
12980 KB |
Output is correct |
36 |
Correct |
216 ms |
43444 KB |
Output is correct |
37 |
Correct |
11 ms |
13024 KB |
Output is correct |
38 |
Correct |
11 ms |
13004 KB |
Output is correct |
39 |
Correct |
191 ms |
43412 KB |
Output is correct |
40 |
Correct |
11 ms |
13284 KB |
Output is correct |
41 |
Correct |
212 ms |
42696 KB |
Output is correct |
42 |
Correct |
221 ms |
45564 KB |
Output is correct |
43 |
Correct |
7 ms |
12072 KB |
Output is correct |
44 |
Correct |
234 ms |
52780 KB |
Output is correct |
45 |
Correct |
204 ms |
49020 KB |
Output is correct |
46 |
Correct |
11 ms |
13004 KB |
Output is correct |
47 |
Correct |
11 ms |
13080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
43028 KB |
Output is correct |
2 |
Correct |
388 ms |
47700 KB |
Output is correct |
3 |
Correct |
14 ms |
13136 KB |
Output is correct |
4 |
Correct |
11 ms |
13004 KB |
Output is correct |
5 |
Correct |
6 ms |
12076 KB |
Output is correct |
6 |
Correct |
6 ms |
12108 KB |
Output is correct |
7 |
Correct |
11 ms |
13088 KB |
Output is correct |
8 |
Correct |
247 ms |
45792 KB |
Output is correct |
9 |
Correct |
15 ms |
13004 KB |
Output is correct |
10 |
Correct |
216 ms |
43376 KB |
Output is correct |
11 |
Correct |
6 ms |
12108 KB |
Output is correct |
12 |
Correct |
221 ms |
43108 KB |
Output is correct |
13 |
Correct |
265 ms |
45600 KB |
Output is correct |
14 |
Correct |
395 ms |
47568 KB |
Output is correct |
15 |
Correct |
219 ms |
43436 KB |
Output is correct |
16 |
Correct |
13 ms |
13128 KB |
Output is correct |
17 |
Correct |
7 ms |
12108 KB |
Output is correct |
18 |
Correct |
396 ms |
47908 KB |
Output is correct |
19 |
Correct |
381 ms |
52532 KB |
Output is correct |
20 |
Correct |
12 ms |
13092 KB |
Output is correct |
21 |
Correct |
6 ms |
12108 KB |
Output is correct |
22 |
Correct |
258 ms |
46532 KB |
Output is correct |
23 |
Correct |
12 ms |
13080 KB |
Output is correct |
24 |
Correct |
247 ms |
44072 KB |
Output is correct |
25 |
Correct |
441 ms |
51628 KB |
Output is correct |
26 |
Correct |
13 ms |
13132 KB |
Output is correct |
27 |
Correct |
14 ms |
13260 KB |
Output is correct |
28 |
Correct |
12 ms |
13004 KB |
Output is correct |
29 |
Correct |
11 ms |
13004 KB |
Output is correct |
30 |
Correct |
12 ms |
13152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12108 KB |
Output is correct |
2 |
Correct |
6 ms |
12108 KB |
Output is correct |
3 |
Correct |
6 ms |
12108 KB |
Output is correct |
4 |
Correct |
6 ms |
12092 KB |
Output is correct |
5 |
Correct |
6 ms |
12108 KB |
Output is correct |
6 |
Correct |
6 ms |
12108 KB |
Output is correct |
7 |
Correct |
6 ms |
12108 KB |
Output is correct |
8 |
Correct |
7 ms |
12108 KB |
Output is correct |
9 |
Correct |
6 ms |
12108 KB |
Output is correct |
10 |
Correct |
6 ms |
12108 KB |
Output is correct |
11 |
Correct |
6 ms |
12176 KB |
Output is correct |
12 |
Correct |
9 ms |
12108 KB |
Output is correct |
13 |
Correct |
6 ms |
12108 KB |
Output is correct |
14 |
Correct |
7 ms |
12112 KB |
Output is correct |
15 |
Correct |
7 ms |
12108 KB |
Output is correct |
16 |
Correct |
6 ms |
12164 KB |
Output is correct |
17 |
Correct |
6 ms |
12108 KB |
Output is correct |
18 |
Correct |
6 ms |
12108 KB |
Output is correct |
19 |
Correct |
5 ms |
12108 KB |
Output is correct |
20 |
Correct |
7 ms |
12108 KB |
Output is correct |
21 |
Correct |
7 ms |
12108 KB |
Output is correct |
22 |
Correct |
7 ms |
12108 KB |
Output is correct |
23 |
Correct |
6 ms |
12140 KB |
Output is correct |
24 |
Correct |
6 ms |
12164 KB |
Output is correct |
25 |
Correct |
7 ms |
12108 KB |
Output is correct |
26 |
Correct |
12 ms |
13132 KB |
Output is correct |
27 |
Correct |
11 ms |
13132 KB |
Output is correct |
28 |
Correct |
12 ms |
13388 KB |
Output is correct |
29 |
Correct |
17 ms |
13120 KB |
Output is correct |
30 |
Correct |
11 ms |
13076 KB |
Output is correct |
31 |
Correct |
6 ms |
12108 KB |
Output is correct |
32 |
Correct |
14 ms |
13328 KB |
Output is correct |
33 |
Correct |
7 ms |
12084 KB |
Output is correct |
34 |
Correct |
11 ms |
13004 KB |
Output is correct |
35 |
Correct |
13 ms |
13204 KB |
Output is correct |
36 |
Correct |
11 ms |
13084 KB |
Output is correct |
37 |
Correct |
11 ms |
13076 KB |
Output is correct |
38 |
Correct |
7 ms |
12064 KB |
Output is correct |
39 |
Correct |
11 ms |
13132 KB |
Output is correct |
40 |
Correct |
13 ms |
12960 KB |
Output is correct |
41 |
Correct |
12 ms |
13088 KB |
Output is correct |
42 |
Correct |
12 ms |
13132 KB |
Output is correct |
43 |
Correct |
11 ms |
13388 KB |
Output is correct |
44 |
Correct |
7 ms |
12108 KB |
Output is correct |
45 |
Correct |
12 ms |
13268 KB |
Output is correct |
46 |
Correct |
13 ms |
13132 KB |
Output is correct |
47 |
Correct |
7 ms |
12108 KB |
Output is correct |
48 |
Correct |
17 ms |
13028 KB |
Output is correct |
49 |
Correct |
14 ms |
13176 KB |
Output is correct |
50 |
Correct |
15 ms |
13188 KB |
Output is correct |
51 |
Correct |
13 ms |
13024 KB |
Output is correct |
52 |
Correct |
12 ms |
13004 KB |
Output is correct |
53 |
Correct |
13 ms |
13064 KB |
Output is correct |
54 |
Correct |
12 ms |
13004 KB |
Output is correct |
55 |
Correct |
7 ms |
12108 KB |
Output is correct |
56 |
Correct |
226 ms |
43104 KB |
Output is correct |
57 |
Correct |
208 ms |
42752 KB |
Output is correct |
58 |
Correct |
11 ms |
13004 KB |
Output is correct |
59 |
Correct |
7 ms |
12108 KB |
Output is correct |
60 |
Correct |
6 ms |
12124 KB |
Output is correct |
61 |
Correct |
220 ms |
42948 KB |
Output is correct |
62 |
Correct |
11 ms |
13004 KB |
Output is correct |
63 |
Correct |
222 ms |
52380 KB |
Output is correct |
64 |
Correct |
202 ms |
42864 KB |
Output is correct |
65 |
Correct |
12 ms |
12980 KB |
Output is correct |
66 |
Correct |
216 ms |
43444 KB |
Output is correct |
67 |
Correct |
11 ms |
13024 KB |
Output is correct |
68 |
Correct |
11 ms |
13004 KB |
Output is correct |
69 |
Correct |
191 ms |
43412 KB |
Output is correct |
70 |
Correct |
11 ms |
13284 KB |
Output is correct |
71 |
Correct |
212 ms |
42696 KB |
Output is correct |
72 |
Correct |
221 ms |
45564 KB |
Output is correct |
73 |
Correct |
7 ms |
12072 KB |
Output is correct |
74 |
Correct |
234 ms |
52780 KB |
Output is correct |
75 |
Correct |
204 ms |
49020 KB |
Output is correct |
76 |
Correct |
11 ms |
13004 KB |
Output is correct |
77 |
Correct |
11 ms |
13080 KB |
Output is correct |
78 |
Correct |
201 ms |
43028 KB |
Output is correct |
79 |
Correct |
388 ms |
47700 KB |
Output is correct |
80 |
Correct |
14 ms |
13136 KB |
Output is correct |
81 |
Correct |
11 ms |
13004 KB |
Output is correct |
82 |
Correct |
6 ms |
12076 KB |
Output is correct |
83 |
Correct |
6 ms |
12108 KB |
Output is correct |
84 |
Correct |
11 ms |
13088 KB |
Output is correct |
85 |
Correct |
247 ms |
45792 KB |
Output is correct |
86 |
Correct |
15 ms |
13004 KB |
Output is correct |
87 |
Correct |
216 ms |
43376 KB |
Output is correct |
88 |
Correct |
6 ms |
12108 KB |
Output is correct |
89 |
Correct |
221 ms |
43108 KB |
Output is correct |
90 |
Correct |
265 ms |
45600 KB |
Output is correct |
91 |
Correct |
395 ms |
47568 KB |
Output is correct |
92 |
Correct |
219 ms |
43436 KB |
Output is correct |
93 |
Correct |
13 ms |
13128 KB |
Output is correct |
94 |
Correct |
7 ms |
12108 KB |
Output is correct |
95 |
Correct |
396 ms |
47908 KB |
Output is correct |
96 |
Correct |
381 ms |
52532 KB |
Output is correct |
97 |
Correct |
12 ms |
13092 KB |
Output is correct |
98 |
Correct |
6 ms |
12108 KB |
Output is correct |
99 |
Correct |
258 ms |
46532 KB |
Output is correct |
100 |
Correct |
12 ms |
13080 KB |
Output is correct |
101 |
Correct |
247 ms |
44072 KB |
Output is correct |
102 |
Correct |
441 ms |
51628 KB |
Output is correct |
103 |
Correct |
13 ms |
13132 KB |
Output is correct |
104 |
Correct |
14 ms |
13260 KB |
Output is correct |
105 |
Correct |
12 ms |
13004 KB |
Output is correct |
106 |
Correct |
11 ms |
13004 KB |
Output is correct |
107 |
Correct |
12 ms |
13152 KB |
Output is correct |
108 |
Execution timed out |
3061 ms |
92740 KB |
Time limit exceeded |
109 |
Halted |
0 ms |
0 KB |
- |