# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
817655 |
2023-08-09T14:38:25 Z |
LucaIlie |
Mergers (JOI19_mergers) |
C++17 |
|
1483 ms |
155884 KB |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5;
int state[MAX_N + 1], depth[MAX_N + 1], parent[MAX_N + 1], preord[MAX_N + 1];
vector<int> edges[MAX_N + 1], cities[MAX_N + 1];
set<int> stateEdges[MAX_N + 1];
struct DSU {
int comp;
int p[MAX_N + 1];
void init( int k ) {
for ( int u = 1; u <= k; u++ )
p[u] = u;
comp = k;
}
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;
comp--;
p[u] = v;
}
} dsu;
int pre = 0;
void dfs( int u, int p ) {
parent[u] = p;
depth[u] = depth[p] + 1;
preord[u] = pre++;
for ( int v: edges[u] ) {
if ( v == p )
continue;
dfs( v, 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 );
dsu.init( k );
for ( int s = 1; s <= k; s++ ) {
sort( cities[s].begin(), cities[s].end(), []( int u, int v ) {
return preord[u] < preord[v];
} );
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[u]] );
u = parent[u];
}
while ( depth[v] > depth[u] ) {
dsu.merge( state[v], state[parent[v]] );
v = parent[v];
}
while ( u != v ) {
dsu.merge( state[u], state[parent[u]] );
u = parent[u];
dsu.merge( state[v], state[parent[v]] );
v = parent[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:72:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for ( int i = 0; i < cities[s].size() - 1; i++ ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47256 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47232 KB |
Output is correct |
4 |
Correct |
19 ms |
47188 KB |
Output is correct |
5 |
Correct |
22 ms |
47256 KB |
Output is correct |
6 |
Correct |
21 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47308 KB |
Output is correct |
8 |
Correct |
25 ms |
47300 KB |
Output is correct |
9 |
Correct |
22 ms |
47280 KB |
Output is correct |
10 |
Correct |
24 ms |
47316 KB |
Output is correct |
11 |
Correct |
20 ms |
47316 KB |
Output is correct |
12 |
Correct |
21 ms |
47316 KB |
Output is correct |
13 |
Correct |
20 ms |
47284 KB |
Output is correct |
14 |
Correct |
21 ms |
47284 KB |
Output is correct |
15 |
Correct |
21 ms |
47232 KB |
Output is correct |
16 |
Correct |
21 ms |
47188 KB |
Output is correct |
17 |
Correct |
21 ms |
47284 KB |
Output is correct |
18 |
Correct |
20 ms |
47268 KB |
Output is correct |
19 |
Correct |
20 ms |
47280 KB |
Output is correct |
20 |
Correct |
20 ms |
47312 KB |
Output is correct |
21 |
Correct |
20 ms |
47288 KB |
Output is correct |
22 |
Correct |
20 ms |
47316 KB |
Output is correct |
23 |
Correct |
20 ms |
47188 KB |
Output is correct |
24 |
Correct |
20 ms |
47232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47256 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47232 KB |
Output is correct |
4 |
Correct |
19 ms |
47188 KB |
Output is correct |
5 |
Correct |
22 ms |
47256 KB |
Output is correct |
6 |
Correct |
21 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47308 KB |
Output is correct |
8 |
Correct |
25 ms |
47300 KB |
Output is correct |
9 |
Correct |
22 ms |
47280 KB |
Output is correct |
10 |
Correct |
24 ms |
47316 KB |
Output is correct |
11 |
Correct |
20 ms |
47316 KB |
Output is correct |
12 |
Correct |
21 ms |
47316 KB |
Output is correct |
13 |
Correct |
20 ms |
47284 KB |
Output is correct |
14 |
Correct |
21 ms |
47284 KB |
Output is correct |
15 |
Correct |
21 ms |
47232 KB |
Output is correct |
16 |
Correct |
21 ms |
47188 KB |
Output is correct |
17 |
Correct |
21 ms |
47284 KB |
Output is correct |
18 |
Correct |
20 ms |
47268 KB |
Output is correct |
19 |
Correct |
20 ms |
47280 KB |
Output is correct |
20 |
Correct |
20 ms |
47312 KB |
Output is correct |
21 |
Correct |
20 ms |
47288 KB |
Output is correct |
22 |
Correct |
20 ms |
47316 KB |
Output is correct |
23 |
Correct |
20 ms |
47188 KB |
Output is correct |
24 |
Correct |
20 ms |
47232 KB |
Output is correct |
25 |
Correct |
23 ms |
47204 KB |
Output is correct |
26 |
Correct |
23 ms |
47700 KB |
Output is correct |
27 |
Correct |
22 ms |
47424 KB |
Output is correct |
28 |
Correct |
24 ms |
47792 KB |
Output is correct |
29 |
Correct |
24 ms |
47796 KB |
Output is correct |
30 |
Correct |
23 ms |
47424 KB |
Output is correct |
31 |
Correct |
22 ms |
47272 KB |
Output is correct |
32 |
Correct |
25 ms |
47628 KB |
Output is correct |
33 |
Correct |
20 ms |
47316 KB |
Output is correct |
34 |
Correct |
22 ms |
47444 KB |
Output is correct |
35 |
Correct |
23 ms |
47796 KB |
Output is correct |
36 |
Correct |
22 ms |
47464 KB |
Output is correct |
37 |
Correct |
22 ms |
47544 KB |
Output is correct |
38 |
Correct |
21 ms |
47408 KB |
Output is correct |
39 |
Correct |
25 ms |
47552 KB |
Output is correct |
40 |
Correct |
25 ms |
47424 KB |
Output is correct |
41 |
Correct |
23 ms |
47444 KB |
Output is correct |
42 |
Correct |
27 ms |
47564 KB |
Output is correct |
43 |
Correct |
22 ms |
47644 KB |
Output is correct |
44 |
Correct |
24 ms |
47260 KB |
Output is correct |
45 |
Correct |
22 ms |
47572 KB |
Output is correct |
46 |
Correct |
23 ms |
47508 KB |
Output is correct |
47 |
Correct |
20 ms |
47316 KB |
Output is correct |
48 |
Correct |
22 ms |
47444 KB |
Output is correct |
49 |
Correct |
24 ms |
47772 KB |
Output is correct |
50 |
Correct |
22 ms |
47820 KB |
Output is correct |
51 |
Correct |
22 ms |
47444 KB |
Output is correct |
52 |
Correct |
22 ms |
47516 KB |
Output is correct |
53 |
Correct |
27 ms |
47596 KB |
Output is correct |
54 |
Correct |
23 ms |
47444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47256 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47232 KB |
Output is correct |
4 |
Correct |
19 ms |
47188 KB |
Output is correct |
5 |
Correct |
22 ms |
47256 KB |
Output is correct |
6 |
Correct |
21 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47308 KB |
Output is correct |
8 |
Correct |
25 ms |
47300 KB |
Output is correct |
9 |
Correct |
22 ms |
47280 KB |
Output is correct |
10 |
Correct |
24 ms |
47316 KB |
Output is correct |
11 |
Correct |
20 ms |
47316 KB |
Output is correct |
12 |
Correct |
21 ms |
47316 KB |
Output is correct |
13 |
Correct |
20 ms |
47284 KB |
Output is correct |
14 |
Correct |
21 ms |
47284 KB |
Output is correct |
15 |
Correct |
21 ms |
47232 KB |
Output is correct |
16 |
Correct |
21 ms |
47188 KB |
Output is correct |
17 |
Correct |
21 ms |
47284 KB |
Output is correct |
18 |
Correct |
20 ms |
47268 KB |
Output is correct |
19 |
Correct |
20 ms |
47280 KB |
Output is correct |
20 |
Correct |
20 ms |
47312 KB |
Output is correct |
21 |
Correct |
20 ms |
47288 KB |
Output is correct |
22 |
Correct |
20 ms |
47316 KB |
Output is correct |
23 |
Correct |
20 ms |
47188 KB |
Output is correct |
24 |
Correct |
20 ms |
47232 KB |
Output is correct |
25 |
Correct |
21 ms |
47316 KB |
Output is correct |
26 |
Correct |
90 ms |
54316 KB |
Output is correct |
27 |
Correct |
128 ms |
53964 KB |
Output is correct |
28 |
Correct |
24 ms |
47424 KB |
Output is correct |
29 |
Correct |
20 ms |
47212 KB |
Output is correct |
30 |
Correct |
22 ms |
47196 KB |
Output is correct |
31 |
Correct |
97 ms |
53912 KB |
Output is correct |
32 |
Correct |
23 ms |
47444 KB |
Output is correct |
33 |
Correct |
177 ms |
58300 KB |
Output is correct |
34 |
Correct |
104 ms |
54004 KB |
Output is correct |
35 |
Correct |
24 ms |
47444 KB |
Output is correct |
36 |
Correct |
99 ms |
54304 KB |
Output is correct |
37 |
Correct |
24 ms |
47444 KB |
Output is correct |
38 |
Correct |
25 ms |
47412 KB |
Output is correct |
39 |
Correct |
92 ms |
54212 KB |
Output is correct |
40 |
Correct |
25 ms |
47636 KB |
Output is correct |
41 |
Correct |
103 ms |
53888 KB |
Output is correct |
42 |
Correct |
150 ms |
55364 KB |
Output is correct |
43 |
Correct |
21 ms |
47280 KB |
Output is correct |
44 |
Correct |
101 ms |
58400 KB |
Output is correct |
45 |
Correct |
177 ms |
56204 KB |
Output is correct |
46 |
Correct |
24 ms |
47500 KB |
Output is correct |
47 |
Correct |
25 ms |
47412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
54332 KB |
Output is correct |
2 |
Correct |
186 ms |
67008 KB |
Output is correct |
3 |
Correct |
25 ms |
47700 KB |
Output is correct |
4 |
Correct |
24 ms |
47420 KB |
Output is correct |
5 |
Correct |
19 ms |
47316 KB |
Output is correct |
6 |
Correct |
20 ms |
47316 KB |
Output is correct |
7 |
Correct |
22 ms |
47412 KB |
Output is correct |
8 |
Correct |
122 ms |
56500 KB |
Output is correct |
9 |
Correct |
24 ms |
47444 KB |
Output is correct |
10 |
Correct |
101 ms |
54124 KB |
Output is correct |
11 |
Correct |
23 ms |
47300 KB |
Output is correct |
12 |
Correct |
99 ms |
54152 KB |
Output is correct |
13 |
Correct |
128 ms |
57332 KB |
Output is correct |
14 |
Correct |
147 ms |
66692 KB |
Output is correct |
15 |
Correct |
105 ms |
54188 KB |
Output is correct |
16 |
Correct |
27 ms |
47516 KB |
Output is correct |
17 |
Correct |
22 ms |
47280 KB |
Output is correct |
18 |
Correct |
156 ms |
64800 KB |
Output is correct |
19 |
Correct |
146 ms |
68940 KB |
Output is correct |
20 |
Correct |
24 ms |
47500 KB |
Output is correct |
21 |
Correct |
21 ms |
47284 KB |
Output is correct |
22 |
Correct |
108 ms |
57472 KB |
Output is correct |
23 |
Correct |
23 ms |
47420 KB |
Output is correct |
24 |
Correct |
117 ms |
55364 KB |
Output is correct |
25 |
Correct |
148 ms |
68240 KB |
Output is correct |
26 |
Correct |
26 ms |
47848 KB |
Output is correct |
27 |
Correct |
26 ms |
47896 KB |
Output is correct |
28 |
Correct |
25 ms |
47496 KB |
Output is correct |
29 |
Correct |
27 ms |
47404 KB |
Output is correct |
30 |
Correct |
23 ms |
47512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47256 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
20 ms |
47232 KB |
Output is correct |
4 |
Correct |
19 ms |
47188 KB |
Output is correct |
5 |
Correct |
22 ms |
47256 KB |
Output is correct |
6 |
Correct |
21 ms |
47188 KB |
Output is correct |
7 |
Correct |
20 ms |
47308 KB |
Output is correct |
8 |
Correct |
25 ms |
47300 KB |
Output is correct |
9 |
Correct |
22 ms |
47280 KB |
Output is correct |
10 |
Correct |
24 ms |
47316 KB |
Output is correct |
11 |
Correct |
20 ms |
47316 KB |
Output is correct |
12 |
Correct |
21 ms |
47316 KB |
Output is correct |
13 |
Correct |
20 ms |
47284 KB |
Output is correct |
14 |
Correct |
21 ms |
47284 KB |
Output is correct |
15 |
Correct |
21 ms |
47232 KB |
Output is correct |
16 |
Correct |
21 ms |
47188 KB |
Output is correct |
17 |
Correct |
21 ms |
47284 KB |
Output is correct |
18 |
Correct |
20 ms |
47268 KB |
Output is correct |
19 |
Correct |
20 ms |
47280 KB |
Output is correct |
20 |
Correct |
20 ms |
47312 KB |
Output is correct |
21 |
Correct |
20 ms |
47288 KB |
Output is correct |
22 |
Correct |
20 ms |
47316 KB |
Output is correct |
23 |
Correct |
20 ms |
47188 KB |
Output is correct |
24 |
Correct |
20 ms |
47232 KB |
Output is correct |
25 |
Correct |
23 ms |
47204 KB |
Output is correct |
26 |
Correct |
23 ms |
47700 KB |
Output is correct |
27 |
Correct |
22 ms |
47424 KB |
Output is correct |
28 |
Correct |
24 ms |
47792 KB |
Output is correct |
29 |
Correct |
24 ms |
47796 KB |
Output is correct |
30 |
Correct |
23 ms |
47424 KB |
Output is correct |
31 |
Correct |
22 ms |
47272 KB |
Output is correct |
32 |
Correct |
25 ms |
47628 KB |
Output is correct |
33 |
Correct |
20 ms |
47316 KB |
Output is correct |
34 |
Correct |
22 ms |
47444 KB |
Output is correct |
35 |
Correct |
23 ms |
47796 KB |
Output is correct |
36 |
Correct |
22 ms |
47464 KB |
Output is correct |
37 |
Correct |
22 ms |
47544 KB |
Output is correct |
38 |
Correct |
21 ms |
47408 KB |
Output is correct |
39 |
Correct |
25 ms |
47552 KB |
Output is correct |
40 |
Correct |
25 ms |
47424 KB |
Output is correct |
41 |
Correct |
23 ms |
47444 KB |
Output is correct |
42 |
Correct |
27 ms |
47564 KB |
Output is correct |
43 |
Correct |
22 ms |
47644 KB |
Output is correct |
44 |
Correct |
24 ms |
47260 KB |
Output is correct |
45 |
Correct |
22 ms |
47572 KB |
Output is correct |
46 |
Correct |
23 ms |
47508 KB |
Output is correct |
47 |
Correct |
20 ms |
47316 KB |
Output is correct |
48 |
Correct |
22 ms |
47444 KB |
Output is correct |
49 |
Correct |
24 ms |
47772 KB |
Output is correct |
50 |
Correct |
22 ms |
47820 KB |
Output is correct |
51 |
Correct |
22 ms |
47444 KB |
Output is correct |
52 |
Correct |
22 ms |
47516 KB |
Output is correct |
53 |
Correct |
27 ms |
47596 KB |
Output is correct |
54 |
Correct |
23 ms |
47444 KB |
Output is correct |
55 |
Correct |
21 ms |
47316 KB |
Output is correct |
56 |
Correct |
90 ms |
54316 KB |
Output is correct |
57 |
Correct |
128 ms |
53964 KB |
Output is correct |
58 |
Correct |
24 ms |
47424 KB |
Output is correct |
59 |
Correct |
20 ms |
47212 KB |
Output is correct |
60 |
Correct |
22 ms |
47196 KB |
Output is correct |
61 |
Correct |
97 ms |
53912 KB |
Output is correct |
62 |
Correct |
23 ms |
47444 KB |
Output is correct |
63 |
Correct |
177 ms |
58300 KB |
Output is correct |
64 |
Correct |
104 ms |
54004 KB |
Output is correct |
65 |
Correct |
24 ms |
47444 KB |
Output is correct |
66 |
Correct |
99 ms |
54304 KB |
Output is correct |
67 |
Correct |
24 ms |
47444 KB |
Output is correct |
68 |
Correct |
25 ms |
47412 KB |
Output is correct |
69 |
Correct |
92 ms |
54212 KB |
Output is correct |
70 |
Correct |
25 ms |
47636 KB |
Output is correct |
71 |
Correct |
103 ms |
53888 KB |
Output is correct |
72 |
Correct |
150 ms |
55364 KB |
Output is correct |
73 |
Correct |
21 ms |
47280 KB |
Output is correct |
74 |
Correct |
101 ms |
58400 KB |
Output is correct |
75 |
Correct |
177 ms |
56204 KB |
Output is correct |
76 |
Correct |
24 ms |
47500 KB |
Output is correct |
77 |
Correct |
25 ms |
47412 KB |
Output is correct |
78 |
Correct |
94 ms |
54332 KB |
Output is correct |
79 |
Correct |
186 ms |
67008 KB |
Output is correct |
80 |
Correct |
25 ms |
47700 KB |
Output is correct |
81 |
Correct |
24 ms |
47420 KB |
Output is correct |
82 |
Correct |
19 ms |
47316 KB |
Output is correct |
83 |
Correct |
20 ms |
47316 KB |
Output is correct |
84 |
Correct |
22 ms |
47412 KB |
Output is correct |
85 |
Correct |
122 ms |
56500 KB |
Output is correct |
86 |
Correct |
24 ms |
47444 KB |
Output is correct |
87 |
Correct |
101 ms |
54124 KB |
Output is correct |
88 |
Correct |
23 ms |
47300 KB |
Output is correct |
89 |
Correct |
99 ms |
54152 KB |
Output is correct |
90 |
Correct |
128 ms |
57332 KB |
Output is correct |
91 |
Correct |
147 ms |
66692 KB |
Output is correct |
92 |
Correct |
105 ms |
54188 KB |
Output is correct |
93 |
Correct |
27 ms |
47516 KB |
Output is correct |
94 |
Correct |
22 ms |
47280 KB |
Output is correct |
95 |
Correct |
156 ms |
64800 KB |
Output is correct |
96 |
Correct |
146 ms |
68940 KB |
Output is correct |
97 |
Correct |
24 ms |
47500 KB |
Output is correct |
98 |
Correct |
21 ms |
47284 KB |
Output is correct |
99 |
Correct |
108 ms |
57472 KB |
Output is correct |
100 |
Correct |
23 ms |
47420 KB |
Output is correct |
101 |
Correct |
117 ms |
55364 KB |
Output is correct |
102 |
Correct |
148 ms |
68240 KB |
Output is correct |
103 |
Correct |
26 ms |
47848 KB |
Output is correct |
104 |
Correct |
26 ms |
47896 KB |
Output is correct |
105 |
Correct |
25 ms |
47496 KB |
Output is correct |
106 |
Correct |
27 ms |
47404 KB |
Output is correct |
107 |
Correct |
23 ms |
47512 KB |
Output is correct |
108 |
Correct |
542 ms |
93960 KB |
Output is correct |
109 |
Correct |
703 ms |
91584 KB |
Output is correct |
110 |
Correct |
655 ms |
98208 KB |
Output is correct |
111 |
Correct |
1103 ms |
155884 KB |
Output is correct |
112 |
Correct |
1162 ms |
131712 KB |
Output is correct |
113 |
Correct |
1119 ms |
136516 KB |
Output is correct |
114 |
Correct |
579 ms |
80992 KB |
Output is correct |
115 |
Correct |
576 ms |
80968 KB |
Output is correct |
116 |
Correct |
730 ms |
86132 KB |
Output is correct |
117 |
Correct |
1070 ms |
145916 KB |
Output is correct |
118 |
Correct |
554 ms |
82256 KB |
Output is correct |
119 |
Correct |
1029 ms |
145928 KB |
Output is correct |
120 |
Correct |
1116 ms |
153960 KB |
Output is correct |
121 |
Correct |
1063 ms |
145660 KB |
Output is correct |
122 |
Correct |
899 ms |
99888 KB |
Output is correct |
123 |
Correct |
1483 ms |
147340 KB |
Output is correct |
124 |
Correct |
1000 ms |
127564 KB |
Output is correct |