# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
817715 |
2023-08-09T15:14:56 Z |
LucaIlie |
Mergers (JOI19_mergers) |
C++17 |
|
3000 ms |
105824 KB |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5;
const int MAX_LOG_N = 19;
int state[MAX_N + 1], parent[MAX_LOG_N + 1][MAX_N + 1], depth[MAX_N + 1], uplus[MAX_N + 1], uminus[MAX_N + 1];
vector<int> edges[MAX_N + 1], cities[MAX_N + 1];
set<int> stateEdges[MAX_N + 1];
struct DSU {
int p[MAX_N + 1];
void init( int k ) {
for ( int u = 1; u <= k; u++ )
p[u] = u;
}
int find( int u ) {
if ( p[u] == u )
return u;
p[u] = find( p[u] );
return p[u];
}
void merge( int u, int v ) {
u = find( u );
v = find( v );
if ( u == v )
return;
p[u] = v;
}
} dsu;
void dfs( int u, int p ) {
depth[u] = depth[p] + 1;
parent[0][u] = p;
for ( int v: edges[u] ) {
if ( v == p )
continue;
dfs( v, u );
}
}
void init() {
for ( int p = 1; p <= MAX_LOG_N; p++ ) {
for ( int u = 0; u <= MAX_N; u++ )
parent[p][u] = parent[p - 1][parent[p - 1][u]];
}
}
int ancestor( int u, int d ) {
for ( int p = MAX_LOG_N; p >= 0; p-- ) {
if ( d >= (1 << p) ) {
d -= (1 << p);
u = parent[p][u];
}
}
return u;
}
int lca( int u, int v ) {
if ( depth[u] > depth[v] )
swap( u, v );
for ( int p = MAX_LOG_N; p >= 0; p-- ) {
if ( depth[parent[p][v]] >= depth[u] )
v = parent[p][v];
}
if ( u == v )
return u;
for ( int p = MAX_LOG_N; p >= 0; p-- ) {
if ( parent[p][u] != parent[p][v] ) {
u = parent[p][u];
v = parent[p][v];
}
}
return parent[0][u];
}
void dfsMerge( int u, int p, int upd ) {
upd += uplus[u];
if ( upd > 0 )
dsu.merge( state[u], state[parent[0][u]] );
for ( int v: edges[u] ) {
if ( v == p )
continue;
dfsMerge( v, u, upd + uminus[u] );
}
}
int main() {
int n, k;
cin >> n >> k;
for ( int i = 0; i < n - 1; i++ ) {
int u, v;
cin >> u >> v;
edges[u].push_back( v );
edges[v].push_back( u );
}
for ( int u = 1; u <= n; u++ ) {
cin >> state[u];
cities[state[u]].push_back( u );
}
dfs( 1, 0 );
init();
for ( int s = 1; s <= k; s++ ) {
for ( int i = 0; i < cities[s].size() - 1; i++ ) {
int u = cities[s][i], v = cities[s][i + 1];
int l = lca( u, v );
int w = ancestor( u, depth[u] - depth[l] - 1 );
int t = ancestor( v, depth[v] - depth[l] - 1 );
if ( l == u ) {
uplus[t]++;
uminus[v]--;
}
else if ( l == v ) {
uplus[w]++;
uminus[u]--;
}
else {
uplus[w]++;
uminus[u]--;
uplus[t]++;
uminus[v]--;
}
}
}
dsu.init( k );
for ( int s = 1; s <= k; s++ ) {
for ( int i = 0; i < cities[s].size() - 1; i++ ) {
int u = cities[s][i], v = cities[s][i + 1];
while ( depth[u] > depth[v] ) {
dsu.merge( state[u], state[parent[0][u]] );
u = parent[0][u];
}
while ( depth[v] > depth[u] ) {
dsu.merge( state[v], state[parent[0][v]] );
v = parent[0][v];
}
while ( u != v ) {
dsu.merge( state[u], state[parent[0][u]] );
u = parent[0][u];
dsu.merge( state[v], state[parent[0][v]] );
v = parent[0][v];
}
}
}
for ( int u = 1; u <= n; u++ )
state[u] = dsu.find( state[u] );
for ( int u = 1; u <= n; u++ ) {
for ( int v: edges[u] ) {
if ( state[u] == state[v] )
continue;
stateEdges[state[u]].insert( state[v] );
stateEdges[state[v]].insert( state[u] );
}
}
int leafs = 0;
for ( int s = 1; s <= k; s++ ) {
if ( stateEdges[s].size() == 1 )
leafs++;
}
cout << (leafs + 1) / 2;
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:117:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for ( int i = 0; i < cities[s].size() - 1; i++ ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:141:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for ( int i = 0; i < cities[s].size() - 1; i++ ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
84428 KB |
Output is correct |
2 |
Correct |
39 ms |
84428 KB |
Output is correct |
3 |
Correct |
38 ms |
84428 KB |
Output is correct |
4 |
Correct |
38 ms |
84476 KB |
Output is correct |
5 |
Correct |
38 ms |
84452 KB |
Output is correct |
6 |
Correct |
40 ms |
84412 KB |
Output is correct |
7 |
Correct |
39 ms |
84556 KB |
Output is correct |
8 |
Correct |
38 ms |
84368 KB |
Output is correct |
9 |
Correct |
38 ms |
84480 KB |
Output is correct |
10 |
Correct |
46 ms |
84456 KB |
Output is correct |
11 |
Correct |
40 ms |
84392 KB |
Output is correct |
12 |
Correct |
38 ms |
84448 KB |
Output is correct |
13 |
Correct |
39 ms |
84480 KB |
Output is correct |
14 |
Correct |
38 ms |
84396 KB |
Output is correct |
15 |
Correct |
39 ms |
84392 KB |
Output is correct |
16 |
Correct |
39 ms |
84432 KB |
Output is correct |
17 |
Correct |
39 ms |
84428 KB |
Output is correct |
18 |
Correct |
38 ms |
84408 KB |
Output is correct |
19 |
Correct |
39 ms |
84428 KB |
Output is correct |
20 |
Correct |
38 ms |
84436 KB |
Output is correct |
21 |
Correct |
44 ms |
84488 KB |
Output is correct |
22 |
Correct |
39 ms |
84428 KB |
Output is correct |
23 |
Correct |
44 ms |
84384 KB |
Output is correct |
24 |
Correct |
44 ms |
84524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
84428 KB |
Output is correct |
2 |
Correct |
39 ms |
84428 KB |
Output is correct |
3 |
Correct |
38 ms |
84428 KB |
Output is correct |
4 |
Correct |
38 ms |
84476 KB |
Output is correct |
5 |
Correct |
38 ms |
84452 KB |
Output is correct |
6 |
Correct |
40 ms |
84412 KB |
Output is correct |
7 |
Correct |
39 ms |
84556 KB |
Output is correct |
8 |
Correct |
38 ms |
84368 KB |
Output is correct |
9 |
Correct |
38 ms |
84480 KB |
Output is correct |
10 |
Correct |
46 ms |
84456 KB |
Output is correct |
11 |
Correct |
40 ms |
84392 KB |
Output is correct |
12 |
Correct |
38 ms |
84448 KB |
Output is correct |
13 |
Correct |
39 ms |
84480 KB |
Output is correct |
14 |
Correct |
38 ms |
84396 KB |
Output is correct |
15 |
Correct |
39 ms |
84392 KB |
Output is correct |
16 |
Correct |
39 ms |
84432 KB |
Output is correct |
17 |
Correct |
39 ms |
84428 KB |
Output is correct |
18 |
Correct |
38 ms |
84408 KB |
Output is correct |
19 |
Correct |
39 ms |
84428 KB |
Output is correct |
20 |
Correct |
38 ms |
84436 KB |
Output is correct |
21 |
Correct |
44 ms |
84488 KB |
Output is correct |
22 |
Correct |
39 ms |
84428 KB |
Output is correct |
23 |
Correct |
44 ms |
84384 KB |
Output is correct |
24 |
Correct |
44 ms |
84524 KB |
Output is correct |
25 |
Correct |
41 ms |
84472 KB |
Output is correct |
26 |
Correct |
43 ms |
84812 KB |
Output is correct |
27 |
Correct |
47 ms |
84684 KB |
Output is correct |
28 |
Correct |
44 ms |
84976 KB |
Output is correct |
29 |
Correct |
45 ms |
85000 KB |
Output is correct |
30 |
Correct |
41 ms |
84680 KB |
Output is correct |
31 |
Correct |
39 ms |
84428 KB |
Output is correct |
32 |
Correct |
41 ms |
84932 KB |
Output is correct |
33 |
Correct |
39 ms |
84408 KB |
Output is correct |
34 |
Correct |
41 ms |
84692 KB |
Output is correct |
35 |
Correct |
46 ms |
84920 KB |
Output is correct |
36 |
Correct |
42 ms |
84596 KB |
Output is correct |
37 |
Correct |
42 ms |
84712 KB |
Output is correct |
38 |
Correct |
39 ms |
84496 KB |
Output is correct |
39 |
Correct |
43 ms |
84680 KB |
Output is correct |
40 |
Correct |
41 ms |
84680 KB |
Output is correct |
41 |
Correct |
44 ms |
84728 KB |
Output is correct |
42 |
Correct |
42 ms |
84684 KB |
Output is correct |
43 |
Correct |
49 ms |
84792 KB |
Output is correct |
44 |
Correct |
39 ms |
84452 KB |
Output is correct |
45 |
Correct |
42 ms |
84756 KB |
Output is correct |
46 |
Correct |
42 ms |
84636 KB |
Output is correct |
47 |
Correct |
44 ms |
84428 KB |
Output is correct |
48 |
Correct |
42 ms |
84736 KB |
Output is correct |
49 |
Correct |
42 ms |
84952 KB |
Output is correct |
50 |
Correct |
41 ms |
84996 KB |
Output is correct |
51 |
Correct |
41 ms |
84684 KB |
Output is correct |
52 |
Correct |
41 ms |
84612 KB |
Output is correct |
53 |
Correct |
41 ms |
84708 KB |
Output is correct |
54 |
Correct |
47 ms |
84660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
84428 KB |
Output is correct |
2 |
Correct |
39 ms |
84428 KB |
Output is correct |
3 |
Correct |
38 ms |
84428 KB |
Output is correct |
4 |
Correct |
38 ms |
84476 KB |
Output is correct |
5 |
Correct |
38 ms |
84452 KB |
Output is correct |
6 |
Correct |
40 ms |
84412 KB |
Output is correct |
7 |
Correct |
39 ms |
84556 KB |
Output is correct |
8 |
Correct |
38 ms |
84368 KB |
Output is correct |
9 |
Correct |
38 ms |
84480 KB |
Output is correct |
10 |
Correct |
46 ms |
84456 KB |
Output is correct |
11 |
Correct |
40 ms |
84392 KB |
Output is correct |
12 |
Correct |
38 ms |
84448 KB |
Output is correct |
13 |
Correct |
39 ms |
84480 KB |
Output is correct |
14 |
Correct |
38 ms |
84396 KB |
Output is correct |
15 |
Correct |
39 ms |
84392 KB |
Output is correct |
16 |
Correct |
39 ms |
84432 KB |
Output is correct |
17 |
Correct |
39 ms |
84428 KB |
Output is correct |
18 |
Correct |
38 ms |
84408 KB |
Output is correct |
19 |
Correct |
39 ms |
84428 KB |
Output is correct |
20 |
Correct |
38 ms |
84436 KB |
Output is correct |
21 |
Correct |
44 ms |
84488 KB |
Output is correct |
22 |
Correct |
39 ms |
84428 KB |
Output is correct |
23 |
Correct |
44 ms |
84384 KB |
Output is correct |
24 |
Correct |
44 ms |
84524 KB |
Output is correct |
25 |
Correct |
38 ms |
84400 KB |
Output is correct |
26 |
Correct |
130 ms |
91932 KB |
Output is correct |
27 |
Correct |
156 ms |
91572 KB |
Output is correct |
28 |
Correct |
43 ms |
84632 KB |
Output is correct |
29 |
Correct |
39 ms |
84368 KB |
Output is correct |
30 |
Correct |
41 ms |
84444 KB |
Output is correct |
31 |
Correct |
153 ms |
91536 KB |
Output is correct |
32 |
Correct |
49 ms |
84704 KB |
Output is correct |
33 |
Execution timed out |
3069 ms |
95820 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
90448 KB |
Output is correct |
2 |
Correct |
177 ms |
102032 KB |
Output is correct |
3 |
Correct |
41 ms |
84812 KB |
Output is correct |
4 |
Correct |
45 ms |
84632 KB |
Output is correct |
5 |
Correct |
38 ms |
84436 KB |
Output is correct |
6 |
Correct |
39 ms |
84452 KB |
Output is correct |
7 |
Correct |
42 ms |
84596 KB |
Output is correct |
8 |
Correct |
170 ms |
94056 KB |
Output is correct |
9 |
Correct |
41 ms |
84688 KB |
Output is correct |
10 |
Correct |
158 ms |
91784 KB |
Output is correct |
11 |
Correct |
39 ms |
84496 KB |
Output is correct |
12 |
Correct |
189 ms |
91804 KB |
Output is correct |
13 |
Correct |
170 ms |
94860 KB |
Output is correct |
14 |
Correct |
201 ms |
103484 KB |
Output is correct |
15 |
Correct |
128 ms |
91844 KB |
Output is correct |
16 |
Correct |
41 ms |
84740 KB |
Output is correct |
17 |
Correct |
38 ms |
84432 KB |
Output is correct |
18 |
Correct |
181 ms |
102428 KB |
Output is correct |
19 |
Correct |
167 ms |
105824 KB |
Output is correct |
20 |
Correct |
43 ms |
84796 KB |
Output is correct |
21 |
Correct |
38 ms |
84408 KB |
Output is correct |
22 |
Correct |
176 ms |
94984 KB |
Output is correct |
23 |
Correct |
41 ms |
84672 KB |
Output is correct |
24 |
Correct |
164 ms |
92904 KB |
Output is correct |
25 |
Correct |
179 ms |
105028 KB |
Output is correct |
26 |
Correct |
44 ms |
84940 KB |
Output is correct |
27 |
Correct |
41 ms |
85052 KB |
Output is correct |
28 |
Correct |
41 ms |
84724 KB |
Output is correct |
29 |
Correct |
42 ms |
84704 KB |
Output is correct |
30 |
Correct |
42 ms |
84788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
84428 KB |
Output is correct |
2 |
Correct |
39 ms |
84428 KB |
Output is correct |
3 |
Correct |
38 ms |
84428 KB |
Output is correct |
4 |
Correct |
38 ms |
84476 KB |
Output is correct |
5 |
Correct |
38 ms |
84452 KB |
Output is correct |
6 |
Correct |
40 ms |
84412 KB |
Output is correct |
7 |
Correct |
39 ms |
84556 KB |
Output is correct |
8 |
Correct |
38 ms |
84368 KB |
Output is correct |
9 |
Correct |
38 ms |
84480 KB |
Output is correct |
10 |
Correct |
46 ms |
84456 KB |
Output is correct |
11 |
Correct |
40 ms |
84392 KB |
Output is correct |
12 |
Correct |
38 ms |
84448 KB |
Output is correct |
13 |
Correct |
39 ms |
84480 KB |
Output is correct |
14 |
Correct |
38 ms |
84396 KB |
Output is correct |
15 |
Correct |
39 ms |
84392 KB |
Output is correct |
16 |
Correct |
39 ms |
84432 KB |
Output is correct |
17 |
Correct |
39 ms |
84428 KB |
Output is correct |
18 |
Correct |
38 ms |
84408 KB |
Output is correct |
19 |
Correct |
39 ms |
84428 KB |
Output is correct |
20 |
Correct |
38 ms |
84436 KB |
Output is correct |
21 |
Correct |
44 ms |
84488 KB |
Output is correct |
22 |
Correct |
39 ms |
84428 KB |
Output is correct |
23 |
Correct |
44 ms |
84384 KB |
Output is correct |
24 |
Correct |
44 ms |
84524 KB |
Output is correct |
25 |
Correct |
41 ms |
84472 KB |
Output is correct |
26 |
Correct |
43 ms |
84812 KB |
Output is correct |
27 |
Correct |
47 ms |
84684 KB |
Output is correct |
28 |
Correct |
44 ms |
84976 KB |
Output is correct |
29 |
Correct |
45 ms |
85000 KB |
Output is correct |
30 |
Correct |
41 ms |
84680 KB |
Output is correct |
31 |
Correct |
39 ms |
84428 KB |
Output is correct |
32 |
Correct |
41 ms |
84932 KB |
Output is correct |
33 |
Correct |
39 ms |
84408 KB |
Output is correct |
34 |
Correct |
41 ms |
84692 KB |
Output is correct |
35 |
Correct |
46 ms |
84920 KB |
Output is correct |
36 |
Correct |
42 ms |
84596 KB |
Output is correct |
37 |
Correct |
42 ms |
84712 KB |
Output is correct |
38 |
Correct |
39 ms |
84496 KB |
Output is correct |
39 |
Correct |
43 ms |
84680 KB |
Output is correct |
40 |
Correct |
41 ms |
84680 KB |
Output is correct |
41 |
Correct |
44 ms |
84728 KB |
Output is correct |
42 |
Correct |
42 ms |
84684 KB |
Output is correct |
43 |
Correct |
49 ms |
84792 KB |
Output is correct |
44 |
Correct |
39 ms |
84452 KB |
Output is correct |
45 |
Correct |
42 ms |
84756 KB |
Output is correct |
46 |
Correct |
42 ms |
84636 KB |
Output is correct |
47 |
Correct |
44 ms |
84428 KB |
Output is correct |
48 |
Correct |
42 ms |
84736 KB |
Output is correct |
49 |
Correct |
42 ms |
84952 KB |
Output is correct |
50 |
Correct |
41 ms |
84996 KB |
Output is correct |
51 |
Correct |
41 ms |
84684 KB |
Output is correct |
52 |
Correct |
41 ms |
84612 KB |
Output is correct |
53 |
Correct |
41 ms |
84708 KB |
Output is correct |
54 |
Correct |
47 ms |
84660 KB |
Output is correct |
55 |
Correct |
38 ms |
84400 KB |
Output is correct |
56 |
Correct |
130 ms |
91932 KB |
Output is correct |
57 |
Correct |
156 ms |
91572 KB |
Output is correct |
58 |
Correct |
43 ms |
84632 KB |
Output is correct |
59 |
Correct |
39 ms |
84368 KB |
Output is correct |
60 |
Correct |
41 ms |
84444 KB |
Output is correct |
61 |
Correct |
153 ms |
91536 KB |
Output is correct |
62 |
Correct |
49 ms |
84704 KB |
Output is correct |
63 |
Execution timed out |
3069 ms |
95820 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |