# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
817772 |
2023-08-09T16:06:19 Z |
LucaIlie |
Mergers (JOI19_mergers) |
C++17 |
|
1583 ms |
211776 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], updates[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 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 ) {
for ( int v: edges[u] ) {
if ( v == p )
continue;
dfsMerge( v, u );
updates[u] += updates[v];
}
if ( updates[u] > 0 )
dsu.merge( state[u], state[parent[0][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 );
updates[u]++;
updates[v]++;
updates[l] -= 2;
}
}
dsu.init( k );
dfsMerge( 1, 0 );
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:106:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for ( int i = 0; i < cities[s].size() - 1; i++ ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
84384 KB |
Output is correct |
2 |
Correct |
43 ms |
84368 KB |
Output is correct |
3 |
Correct |
41 ms |
84396 KB |
Output is correct |
4 |
Correct |
42 ms |
84436 KB |
Output is correct |
5 |
Correct |
42 ms |
84392 KB |
Output is correct |
6 |
Correct |
42 ms |
84444 KB |
Output is correct |
7 |
Correct |
40 ms |
84436 KB |
Output is correct |
8 |
Correct |
42 ms |
84436 KB |
Output is correct |
9 |
Correct |
41 ms |
84384 KB |
Output is correct |
10 |
Correct |
41 ms |
84428 KB |
Output is correct |
11 |
Correct |
42 ms |
84484 KB |
Output is correct |
12 |
Correct |
42 ms |
84428 KB |
Output is correct |
13 |
Correct |
43 ms |
84436 KB |
Output is correct |
14 |
Correct |
42 ms |
84356 KB |
Output is correct |
15 |
Correct |
43 ms |
84408 KB |
Output is correct |
16 |
Correct |
50 ms |
84472 KB |
Output is correct |
17 |
Correct |
47 ms |
84380 KB |
Output is correct |
18 |
Correct |
41 ms |
84480 KB |
Output is correct |
19 |
Correct |
41 ms |
84436 KB |
Output is correct |
20 |
Correct |
42 ms |
84428 KB |
Output is correct |
21 |
Correct |
44 ms |
84416 KB |
Output is correct |
22 |
Correct |
42 ms |
84480 KB |
Output is correct |
23 |
Correct |
50 ms |
84472 KB |
Output is correct |
24 |
Correct |
53 ms |
84448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
84384 KB |
Output is correct |
2 |
Correct |
43 ms |
84368 KB |
Output is correct |
3 |
Correct |
41 ms |
84396 KB |
Output is correct |
4 |
Correct |
42 ms |
84436 KB |
Output is correct |
5 |
Correct |
42 ms |
84392 KB |
Output is correct |
6 |
Correct |
42 ms |
84444 KB |
Output is correct |
7 |
Correct |
40 ms |
84436 KB |
Output is correct |
8 |
Correct |
42 ms |
84436 KB |
Output is correct |
9 |
Correct |
41 ms |
84384 KB |
Output is correct |
10 |
Correct |
41 ms |
84428 KB |
Output is correct |
11 |
Correct |
42 ms |
84484 KB |
Output is correct |
12 |
Correct |
42 ms |
84428 KB |
Output is correct |
13 |
Correct |
43 ms |
84436 KB |
Output is correct |
14 |
Correct |
42 ms |
84356 KB |
Output is correct |
15 |
Correct |
43 ms |
84408 KB |
Output is correct |
16 |
Correct |
50 ms |
84472 KB |
Output is correct |
17 |
Correct |
47 ms |
84380 KB |
Output is correct |
18 |
Correct |
41 ms |
84480 KB |
Output is correct |
19 |
Correct |
41 ms |
84436 KB |
Output is correct |
20 |
Correct |
42 ms |
84428 KB |
Output is correct |
21 |
Correct |
44 ms |
84416 KB |
Output is correct |
22 |
Correct |
42 ms |
84480 KB |
Output is correct |
23 |
Correct |
50 ms |
84472 KB |
Output is correct |
24 |
Correct |
53 ms |
84448 KB |
Output is correct |
25 |
Correct |
41 ms |
84472 KB |
Output is correct |
26 |
Correct |
45 ms |
84812 KB |
Output is correct |
27 |
Correct |
45 ms |
84684 KB |
Output is correct |
28 |
Correct |
45 ms |
85036 KB |
Output is correct |
29 |
Correct |
45 ms |
84944 KB |
Output is correct |
30 |
Correct |
45 ms |
84692 KB |
Output is correct |
31 |
Correct |
42 ms |
84476 KB |
Output is correct |
32 |
Correct |
46 ms |
85040 KB |
Output is correct |
33 |
Correct |
41 ms |
84364 KB |
Output is correct |
34 |
Correct |
45 ms |
84688 KB |
Output is correct |
35 |
Correct |
47 ms |
84948 KB |
Output is correct |
36 |
Correct |
44 ms |
84552 KB |
Output is correct |
37 |
Correct |
45 ms |
84648 KB |
Output is correct |
38 |
Correct |
43 ms |
84480 KB |
Output is correct |
39 |
Correct |
45 ms |
84668 KB |
Output is correct |
40 |
Correct |
45 ms |
84616 KB |
Output is correct |
41 |
Correct |
44 ms |
84648 KB |
Output is correct |
42 |
Correct |
45 ms |
84740 KB |
Output is correct |
43 |
Correct |
46 ms |
84872 KB |
Output is correct |
44 |
Correct |
42 ms |
84452 KB |
Output is correct |
45 |
Correct |
43 ms |
84740 KB |
Output is correct |
46 |
Correct |
45 ms |
84636 KB |
Output is correct |
47 |
Correct |
43 ms |
84468 KB |
Output is correct |
48 |
Correct |
44 ms |
84620 KB |
Output is correct |
49 |
Correct |
45 ms |
84936 KB |
Output is correct |
50 |
Correct |
44 ms |
85032 KB |
Output is correct |
51 |
Correct |
46 ms |
84616 KB |
Output is correct |
52 |
Correct |
44 ms |
84600 KB |
Output is correct |
53 |
Correct |
44 ms |
84684 KB |
Output is correct |
54 |
Correct |
45 ms |
84620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
84384 KB |
Output is correct |
2 |
Correct |
43 ms |
84368 KB |
Output is correct |
3 |
Correct |
41 ms |
84396 KB |
Output is correct |
4 |
Correct |
42 ms |
84436 KB |
Output is correct |
5 |
Correct |
42 ms |
84392 KB |
Output is correct |
6 |
Correct |
42 ms |
84444 KB |
Output is correct |
7 |
Correct |
40 ms |
84436 KB |
Output is correct |
8 |
Correct |
42 ms |
84436 KB |
Output is correct |
9 |
Correct |
41 ms |
84384 KB |
Output is correct |
10 |
Correct |
41 ms |
84428 KB |
Output is correct |
11 |
Correct |
42 ms |
84484 KB |
Output is correct |
12 |
Correct |
42 ms |
84428 KB |
Output is correct |
13 |
Correct |
43 ms |
84436 KB |
Output is correct |
14 |
Correct |
42 ms |
84356 KB |
Output is correct |
15 |
Correct |
43 ms |
84408 KB |
Output is correct |
16 |
Correct |
50 ms |
84472 KB |
Output is correct |
17 |
Correct |
47 ms |
84380 KB |
Output is correct |
18 |
Correct |
41 ms |
84480 KB |
Output is correct |
19 |
Correct |
41 ms |
84436 KB |
Output is correct |
20 |
Correct |
42 ms |
84428 KB |
Output is correct |
21 |
Correct |
44 ms |
84416 KB |
Output is correct |
22 |
Correct |
42 ms |
84480 KB |
Output is correct |
23 |
Correct |
50 ms |
84472 KB |
Output is correct |
24 |
Correct |
53 ms |
84448 KB |
Output is correct |
25 |
Correct |
49 ms |
84428 KB |
Output is correct |
26 |
Correct |
136 ms |
90568 KB |
Output is correct |
27 |
Correct |
161 ms |
90236 KB |
Output is correct |
28 |
Correct |
44 ms |
84612 KB |
Output is correct |
29 |
Correct |
41 ms |
84464 KB |
Output is correct |
30 |
Correct |
42 ms |
84428 KB |
Output is correct |
31 |
Correct |
158 ms |
90180 KB |
Output is correct |
32 |
Correct |
44 ms |
84584 KB |
Output is correct |
33 |
Correct |
179 ms |
100400 KB |
Output is correct |
34 |
Correct |
158 ms |
91156 KB |
Output is correct |
35 |
Correct |
44 ms |
84556 KB |
Output is correct |
36 |
Correct |
164 ms |
92236 KB |
Output is correct |
37 |
Correct |
45 ms |
84668 KB |
Output is correct |
38 |
Correct |
54 ms |
84572 KB |
Output is correct |
39 |
Correct |
141 ms |
91440 KB |
Output is correct |
40 |
Correct |
45 ms |
84992 KB |
Output is correct |
41 |
Correct |
149 ms |
91040 KB |
Output is correct |
42 |
Correct |
193 ms |
94520 KB |
Output is correct |
43 |
Correct |
42 ms |
84464 KB |
Output is correct |
44 |
Correct |
165 ms |
101568 KB |
Output is correct |
45 |
Correct |
179 ms |
96400 KB |
Output is correct |
46 |
Correct |
45 ms |
84688 KB |
Output is correct |
47 |
Correct |
45 ms |
84584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
91136 KB |
Output is correct |
2 |
Correct |
199 ms |
103052 KB |
Output is correct |
3 |
Correct |
45 ms |
84788 KB |
Output is correct |
4 |
Correct |
44 ms |
84684 KB |
Output is correct |
5 |
Correct |
43 ms |
84364 KB |
Output is correct |
6 |
Correct |
41 ms |
84428 KB |
Output is correct |
7 |
Correct |
47 ms |
84852 KB |
Output is correct |
8 |
Correct |
177 ms |
92488 KB |
Output is correct |
9 |
Correct |
44 ms |
84692 KB |
Output is correct |
10 |
Correct |
162 ms |
90368 KB |
Output is correct |
11 |
Correct |
43 ms |
84376 KB |
Output is correct |
12 |
Correct |
166 ms |
90492 KB |
Output is correct |
13 |
Correct |
178 ms |
93272 KB |
Output is correct |
14 |
Correct |
189 ms |
102588 KB |
Output is correct |
15 |
Correct |
136 ms |
90556 KB |
Output is correct |
16 |
Correct |
46 ms |
84744 KB |
Output is correct |
17 |
Correct |
42 ms |
84428 KB |
Output is correct |
18 |
Correct |
190 ms |
100804 KB |
Output is correct |
19 |
Correct |
189 ms |
108112 KB |
Output is correct |
20 |
Correct |
46 ms |
84672 KB |
Output is correct |
21 |
Correct |
42 ms |
84400 KB |
Output is correct |
22 |
Correct |
179 ms |
93544 KB |
Output is correct |
23 |
Correct |
44 ms |
84708 KB |
Output is correct |
24 |
Correct |
170 ms |
91468 KB |
Output is correct |
25 |
Correct |
183 ms |
106352 KB |
Output is correct |
26 |
Correct |
45 ms |
85032 KB |
Output is correct |
27 |
Correct |
46 ms |
85088 KB |
Output is correct |
28 |
Correct |
46 ms |
84628 KB |
Output is correct |
29 |
Correct |
45 ms |
84576 KB |
Output is correct |
30 |
Correct |
46 ms |
84668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
84384 KB |
Output is correct |
2 |
Correct |
43 ms |
84368 KB |
Output is correct |
3 |
Correct |
41 ms |
84396 KB |
Output is correct |
4 |
Correct |
42 ms |
84436 KB |
Output is correct |
5 |
Correct |
42 ms |
84392 KB |
Output is correct |
6 |
Correct |
42 ms |
84444 KB |
Output is correct |
7 |
Correct |
40 ms |
84436 KB |
Output is correct |
8 |
Correct |
42 ms |
84436 KB |
Output is correct |
9 |
Correct |
41 ms |
84384 KB |
Output is correct |
10 |
Correct |
41 ms |
84428 KB |
Output is correct |
11 |
Correct |
42 ms |
84484 KB |
Output is correct |
12 |
Correct |
42 ms |
84428 KB |
Output is correct |
13 |
Correct |
43 ms |
84436 KB |
Output is correct |
14 |
Correct |
42 ms |
84356 KB |
Output is correct |
15 |
Correct |
43 ms |
84408 KB |
Output is correct |
16 |
Correct |
50 ms |
84472 KB |
Output is correct |
17 |
Correct |
47 ms |
84380 KB |
Output is correct |
18 |
Correct |
41 ms |
84480 KB |
Output is correct |
19 |
Correct |
41 ms |
84436 KB |
Output is correct |
20 |
Correct |
42 ms |
84428 KB |
Output is correct |
21 |
Correct |
44 ms |
84416 KB |
Output is correct |
22 |
Correct |
42 ms |
84480 KB |
Output is correct |
23 |
Correct |
50 ms |
84472 KB |
Output is correct |
24 |
Correct |
53 ms |
84448 KB |
Output is correct |
25 |
Correct |
41 ms |
84472 KB |
Output is correct |
26 |
Correct |
45 ms |
84812 KB |
Output is correct |
27 |
Correct |
45 ms |
84684 KB |
Output is correct |
28 |
Correct |
45 ms |
85036 KB |
Output is correct |
29 |
Correct |
45 ms |
84944 KB |
Output is correct |
30 |
Correct |
45 ms |
84692 KB |
Output is correct |
31 |
Correct |
42 ms |
84476 KB |
Output is correct |
32 |
Correct |
46 ms |
85040 KB |
Output is correct |
33 |
Correct |
41 ms |
84364 KB |
Output is correct |
34 |
Correct |
45 ms |
84688 KB |
Output is correct |
35 |
Correct |
47 ms |
84948 KB |
Output is correct |
36 |
Correct |
44 ms |
84552 KB |
Output is correct |
37 |
Correct |
45 ms |
84648 KB |
Output is correct |
38 |
Correct |
43 ms |
84480 KB |
Output is correct |
39 |
Correct |
45 ms |
84668 KB |
Output is correct |
40 |
Correct |
45 ms |
84616 KB |
Output is correct |
41 |
Correct |
44 ms |
84648 KB |
Output is correct |
42 |
Correct |
45 ms |
84740 KB |
Output is correct |
43 |
Correct |
46 ms |
84872 KB |
Output is correct |
44 |
Correct |
42 ms |
84452 KB |
Output is correct |
45 |
Correct |
43 ms |
84740 KB |
Output is correct |
46 |
Correct |
45 ms |
84636 KB |
Output is correct |
47 |
Correct |
43 ms |
84468 KB |
Output is correct |
48 |
Correct |
44 ms |
84620 KB |
Output is correct |
49 |
Correct |
45 ms |
84936 KB |
Output is correct |
50 |
Correct |
44 ms |
85032 KB |
Output is correct |
51 |
Correct |
46 ms |
84616 KB |
Output is correct |
52 |
Correct |
44 ms |
84600 KB |
Output is correct |
53 |
Correct |
44 ms |
84684 KB |
Output is correct |
54 |
Correct |
45 ms |
84620 KB |
Output is correct |
55 |
Correct |
49 ms |
84428 KB |
Output is correct |
56 |
Correct |
136 ms |
90568 KB |
Output is correct |
57 |
Correct |
161 ms |
90236 KB |
Output is correct |
58 |
Correct |
44 ms |
84612 KB |
Output is correct |
59 |
Correct |
41 ms |
84464 KB |
Output is correct |
60 |
Correct |
42 ms |
84428 KB |
Output is correct |
61 |
Correct |
158 ms |
90180 KB |
Output is correct |
62 |
Correct |
44 ms |
84584 KB |
Output is correct |
63 |
Correct |
179 ms |
100400 KB |
Output is correct |
64 |
Correct |
158 ms |
91156 KB |
Output is correct |
65 |
Correct |
44 ms |
84556 KB |
Output is correct |
66 |
Correct |
164 ms |
92236 KB |
Output is correct |
67 |
Correct |
45 ms |
84668 KB |
Output is correct |
68 |
Correct |
54 ms |
84572 KB |
Output is correct |
69 |
Correct |
141 ms |
91440 KB |
Output is correct |
70 |
Correct |
45 ms |
84992 KB |
Output is correct |
71 |
Correct |
149 ms |
91040 KB |
Output is correct |
72 |
Correct |
193 ms |
94520 KB |
Output is correct |
73 |
Correct |
42 ms |
84464 KB |
Output is correct |
74 |
Correct |
165 ms |
101568 KB |
Output is correct |
75 |
Correct |
179 ms |
96400 KB |
Output is correct |
76 |
Correct |
45 ms |
84688 KB |
Output is correct |
77 |
Correct |
45 ms |
84584 KB |
Output is correct |
78 |
Correct |
144 ms |
91136 KB |
Output is correct |
79 |
Correct |
199 ms |
103052 KB |
Output is correct |
80 |
Correct |
45 ms |
84788 KB |
Output is correct |
81 |
Correct |
44 ms |
84684 KB |
Output is correct |
82 |
Correct |
43 ms |
84364 KB |
Output is correct |
83 |
Correct |
41 ms |
84428 KB |
Output is correct |
84 |
Correct |
47 ms |
84852 KB |
Output is correct |
85 |
Correct |
177 ms |
92488 KB |
Output is correct |
86 |
Correct |
44 ms |
84692 KB |
Output is correct |
87 |
Correct |
162 ms |
90368 KB |
Output is correct |
88 |
Correct |
43 ms |
84376 KB |
Output is correct |
89 |
Correct |
166 ms |
90492 KB |
Output is correct |
90 |
Correct |
178 ms |
93272 KB |
Output is correct |
91 |
Correct |
189 ms |
102588 KB |
Output is correct |
92 |
Correct |
136 ms |
90556 KB |
Output is correct |
93 |
Correct |
46 ms |
84744 KB |
Output is correct |
94 |
Correct |
42 ms |
84428 KB |
Output is correct |
95 |
Correct |
190 ms |
100804 KB |
Output is correct |
96 |
Correct |
189 ms |
108112 KB |
Output is correct |
97 |
Correct |
46 ms |
84672 KB |
Output is correct |
98 |
Correct |
42 ms |
84400 KB |
Output is correct |
99 |
Correct |
179 ms |
93544 KB |
Output is correct |
100 |
Correct |
44 ms |
84708 KB |
Output is correct |
101 |
Correct |
170 ms |
91468 KB |
Output is correct |
102 |
Correct |
183 ms |
106352 KB |
Output is correct |
103 |
Correct |
45 ms |
85032 KB |
Output is correct |
104 |
Correct |
46 ms |
85088 KB |
Output is correct |
105 |
Correct |
46 ms |
84628 KB |
Output is correct |
106 |
Correct |
45 ms |
84576 KB |
Output is correct |
107 |
Correct |
46 ms |
84668 KB |
Output is correct |
108 |
Correct |
1269 ms |
131244 KB |
Output is correct |
109 |
Correct |
1512 ms |
143464 KB |
Output is correct |
110 |
Correct |
1509 ms |
159280 KB |
Output is correct |
111 |
Correct |
1277 ms |
211776 KB |
Output is correct |
112 |
Correct |
1168 ms |
179852 KB |
Output is correct |
113 |
Correct |
1308 ms |
173588 KB |
Output is correct |
114 |
Correct |
917 ms |
118128 KB |
Output is correct |
115 |
Correct |
878 ms |
118068 KB |
Output is correct |
116 |
Correct |
1583 ms |
123440 KB |
Output is correct |
117 |
Correct |
1112 ms |
182968 KB |
Output is correct |
118 |
Correct |
1245 ms |
119596 KB |
Output is correct |
119 |
Correct |
1106 ms |
182992 KB |
Output is correct |
120 |
Correct |
1184 ms |
202740 KB |
Output is correct |
121 |
Correct |
1082 ms |
182836 KB |
Output is correct |
122 |
Correct |
1552 ms |
137116 KB |
Output is correct |
123 |
Correct |
1461 ms |
182624 KB |
Output is correct |
124 |
Correct |
1085 ms |
165012 KB |
Output is correct |