# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
824219 |
2023-08-13T19:07:42 Z |
vjudge1 |
Mergers (JOI19_mergers) |
C++17 |
|
900 ms |
233108 KB |
#include <bits/stdc++.h>
using namespace std;
// 123
static const int N = 1e6 + 10;
class lca_t {
public:
int rmq[20][N];
int pos[N], d[N];
vector<int> adj[N];
int timer;
int n;
lca_t(int n = 0) : n(n) {}
void add_edge(int u, int v) {
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
void dfs(int u, int p) {
rmq[0][timer++] = u;
for (int v : adj[u]) {
if (v == p) continue;
d[v] = d[u] + 1;
dfs(v, u);
rmq[0][timer++] = u;
}
}
void build(int root) {
timer = 0;
d[root] = 0;
dfs(root, -1);
for (int i = 0; i < timer; i++) pos[rmq[0][i]] = i;
assert(__lg(timer) < 20);
for (int i = 1; i <= __lg(timer); i++) {
for (int j = 0; j + (1 << i) <= timer; j++) {
rmq[i][j] = d[rmq[i - 1][j]] < d[rmq[i - 1][j + (1 << (i - 1))]]
? rmq[i - 1][j]
: rmq[i - 1][j + (1 << (i - 1))];
}
}
}
int get(int u, int v) {
int l = pos[u], r = pos[v];
if (l > r) swap(l, r);
int i = __lg(r - l + 1);
r -= (1 << i) - 1;
return d[rmq[i][l]] < d[rmq[i][r]] ? rmq[i][l] : rmq[i][r];
}
} lca;
class DSU {
public:
DSU(int n) {
prt.resize(n + 1, -1);
}
int SZ(int u) {
return -prt[root(u)];
}
int root(int u) {
return (prt[u] < 0 ? u : (prt[u] = root(prt[u])));
}
bool connected(int u, int v) {
return ((u = root(u)) == (v = root(v)));
}
bool unite(int u, int v) {
if ((u = root(u)) == (v = root(v))) {
return 0;
}
if (prt[u] > prt[v]) {
u ^= v ^= u ^= v;
}
prt[u] += prt[v];
prt[v] = u;
return 1;
}
private:
vector<int> prt;
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
lca.add_edge(u, v);
}
vector<vector<int>> group(k);
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
for (int i = 0; i < n; i++) group[a[i]].emplace_back(i);
lca.build(0);
vector<int> pf(n, 0);
for (int i = 0; i < k; i++) {
int top = group[i][0];
for (int j : group[i]) top = lca.get(top, j);
pf[top] -= group[i].size();
for (int j : group[i]) pf[j]++;
}
DSU dsu(n);
function<void(int, int)> dfs = [&](int u, int p) {
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
pf[u] += pf[v];
if (pf[v]) {
dsu.unite(u, v);
}
}
};
dfs(0, -1);
vector<int> deg(n);
for (int i = 0; i < n; i++) {
for (int j : adj[i]) {
if (i > j) continue;
int x = dsu.root(i), y = dsu.root(j);
if (x == y) continue;
deg[x]++, deg[y]++;
}
}
int res = 0;
for (int i = 0; i < n; i++) res += deg[i] == 1;
cout << (res + 1 >> 1);
}
Compilation message
mergers.cpp: In function 'int32_t main()':
mergers.cpp:141:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
141 | cout << (res + 1 >> 1);
| ~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23852 KB |
Output is correct |
2 |
Correct |
11 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23816 KB |
Output is correct |
7 |
Correct |
11 ms |
23832 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23816 KB |
Output is correct |
10 |
Correct |
11 ms |
23824 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23816 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23816 KB |
Output is correct |
18 |
Correct |
13 ms |
23896 KB |
Output is correct |
19 |
Correct |
12 ms |
23812 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23892 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23852 KB |
Output is correct |
2 |
Correct |
11 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23816 KB |
Output is correct |
7 |
Correct |
11 ms |
23832 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23816 KB |
Output is correct |
10 |
Correct |
11 ms |
23824 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23816 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23816 KB |
Output is correct |
18 |
Correct |
13 ms |
23896 KB |
Output is correct |
19 |
Correct |
12 ms |
23812 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23892 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
13 ms |
23816 KB |
Output is correct |
26 |
Correct |
16 ms |
24672 KB |
Output is correct |
27 |
Correct |
13 ms |
24592 KB |
Output is correct |
28 |
Correct |
13 ms |
24980 KB |
Output is correct |
29 |
Correct |
13 ms |
24660 KB |
Output is correct |
30 |
Correct |
14 ms |
24596 KB |
Output is correct |
31 |
Correct |
11 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
25096 KB |
Output is correct |
33 |
Correct |
11 ms |
23780 KB |
Output is correct |
34 |
Correct |
14 ms |
24532 KB |
Output is correct |
35 |
Correct |
15 ms |
24728 KB |
Output is correct |
36 |
Correct |
13 ms |
24492 KB |
Output is correct |
37 |
Correct |
13 ms |
24536 KB |
Output is correct |
38 |
Correct |
11 ms |
23892 KB |
Output is correct |
39 |
Correct |
13 ms |
24656 KB |
Output is correct |
40 |
Correct |
16 ms |
24548 KB |
Output is correct |
41 |
Correct |
13 ms |
24596 KB |
Output is correct |
42 |
Correct |
13 ms |
24660 KB |
Output is correct |
43 |
Correct |
14 ms |
24976 KB |
Output is correct |
44 |
Correct |
11 ms |
23820 KB |
Output is correct |
45 |
Correct |
14 ms |
24752 KB |
Output is correct |
46 |
Correct |
13 ms |
24660 KB |
Output is correct |
47 |
Correct |
11 ms |
23808 KB |
Output is correct |
48 |
Correct |
13 ms |
24508 KB |
Output is correct |
49 |
Correct |
13 ms |
24724 KB |
Output is correct |
50 |
Correct |
15 ms |
24852 KB |
Output is correct |
51 |
Correct |
14 ms |
24544 KB |
Output is correct |
52 |
Correct |
15 ms |
24532 KB |
Output is correct |
53 |
Correct |
15 ms |
24600 KB |
Output is correct |
54 |
Correct |
12 ms |
24532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23852 KB |
Output is correct |
2 |
Correct |
11 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23816 KB |
Output is correct |
7 |
Correct |
11 ms |
23832 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23816 KB |
Output is correct |
10 |
Correct |
11 ms |
23824 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23816 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23816 KB |
Output is correct |
18 |
Correct |
13 ms |
23896 KB |
Output is correct |
19 |
Correct |
12 ms |
23812 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23892 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
64 ms |
50312 KB |
Output is correct |
27 |
Correct |
81 ms |
49768 KB |
Output is correct |
28 |
Correct |
13 ms |
24532 KB |
Output is correct |
29 |
Correct |
12 ms |
23820 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
86 ms |
49668 KB |
Output is correct |
32 |
Correct |
12 ms |
24592 KB |
Output is correct |
33 |
Correct |
105 ms |
65484 KB |
Output is correct |
34 |
Correct |
97 ms |
49808 KB |
Output is correct |
35 |
Correct |
13 ms |
24532 KB |
Output is correct |
36 |
Correct |
82 ms |
51344 KB |
Output is correct |
37 |
Correct |
13 ms |
24464 KB |
Output is correct |
38 |
Correct |
13 ms |
24592 KB |
Output is correct |
39 |
Correct |
64 ms |
50332 KB |
Output is correct |
40 |
Correct |
13 ms |
25128 KB |
Output is correct |
41 |
Correct |
78 ms |
49672 KB |
Output is correct |
42 |
Correct |
103 ms |
54864 KB |
Output is correct |
43 |
Correct |
11 ms |
23892 KB |
Output is correct |
44 |
Correct |
122 ms |
66088 KB |
Output is correct |
45 |
Correct |
86 ms |
57912 KB |
Output is correct |
46 |
Correct |
12 ms |
24532 KB |
Output is correct |
47 |
Correct |
16 ms |
24592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
49368 KB |
Output is correct |
2 |
Correct |
82 ms |
54240 KB |
Output is correct |
3 |
Correct |
12 ms |
24532 KB |
Output is correct |
4 |
Correct |
12 ms |
24504 KB |
Output is correct |
5 |
Correct |
14 ms |
23816 KB |
Output is correct |
6 |
Correct |
10 ms |
23812 KB |
Output is correct |
7 |
Correct |
11 ms |
24592 KB |
Output is correct |
8 |
Correct |
106 ms |
51980 KB |
Output is correct |
9 |
Correct |
12 ms |
24608 KB |
Output is correct |
10 |
Correct |
121 ms |
49720 KB |
Output is correct |
11 |
Correct |
11 ms |
23908 KB |
Output is correct |
12 |
Correct |
101 ms |
49692 KB |
Output is correct |
13 |
Correct |
95 ms |
51868 KB |
Output is correct |
14 |
Correct |
138 ms |
54668 KB |
Output is correct |
15 |
Correct |
64 ms |
50332 KB |
Output is correct |
16 |
Correct |
12 ms |
24660 KB |
Output is correct |
17 |
Correct |
11 ms |
23812 KB |
Output is correct |
18 |
Correct |
79 ms |
54720 KB |
Output is correct |
19 |
Correct |
106 ms |
63312 KB |
Output is correct |
20 |
Correct |
13 ms |
24596 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
98 ms |
52460 KB |
Output is correct |
23 |
Correct |
14 ms |
24544 KB |
Output is correct |
24 |
Correct |
114 ms |
50280 KB |
Output is correct |
25 |
Correct |
104 ms |
60328 KB |
Output is correct |
26 |
Correct |
13 ms |
24772 KB |
Output is correct |
27 |
Correct |
14 ms |
24788 KB |
Output is correct |
28 |
Correct |
14 ms |
24484 KB |
Output is correct |
29 |
Correct |
12 ms |
24524 KB |
Output is correct |
30 |
Correct |
12 ms |
24660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23852 KB |
Output is correct |
2 |
Correct |
11 ms |
23892 KB |
Output is correct |
3 |
Correct |
11 ms |
23816 KB |
Output is correct |
4 |
Correct |
12 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23816 KB |
Output is correct |
7 |
Correct |
11 ms |
23832 KB |
Output is correct |
8 |
Correct |
12 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23816 KB |
Output is correct |
10 |
Correct |
11 ms |
23824 KB |
Output is correct |
11 |
Correct |
11 ms |
23892 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23816 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23816 KB |
Output is correct |
18 |
Correct |
13 ms |
23896 KB |
Output is correct |
19 |
Correct |
12 ms |
23812 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23892 KB |
Output is correct |
22 |
Correct |
11 ms |
23892 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
13 ms |
23816 KB |
Output is correct |
26 |
Correct |
16 ms |
24672 KB |
Output is correct |
27 |
Correct |
13 ms |
24592 KB |
Output is correct |
28 |
Correct |
13 ms |
24980 KB |
Output is correct |
29 |
Correct |
13 ms |
24660 KB |
Output is correct |
30 |
Correct |
14 ms |
24596 KB |
Output is correct |
31 |
Correct |
11 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
25096 KB |
Output is correct |
33 |
Correct |
11 ms |
23780 KB |
Output is correct |
34 |
Correct |
14 ms |
24532 KB |
Output is correct |
35 |
Correct |
15 ms |
24728 KB |
Output is correct |
36 |
Correct |
13 ms |
24492 KB |
Output is correct |
37 |
Correct |
13 ms |
24536 KB |
Output is correct |
38 |
Correct |
11 ms |
23892 KB |
Output is correct |
39 |
Correct |
13 ms |
24656 KB |
Output is correct |
40 |
Correct |
16 ms |
24548 KB |
Output is correct |
41 |
Correct |
13 ms |
24596 KB |
Output is correct |
42 |
Correct |
13 ms |
24660 KB |
Output is correct |
43 |
Correct |
14 ms |
24976 KB |
Output is correct |
44 |
Correct |
11 ms |
23820 KB |
Output is correct |
45 |
Correct |
14 ms |
24752 KB |
Output is correct |
46 |
Correct |
13 ms |
24660 KB |
Output is correct |
47 |
Correct |
11 ms |
23808 KB |
Output is correct |
48 |
Correct |
13 ms |
24508 KB |
Output is correct |
49 |
Correct |
13 ms |
24724 KB |
Output is correct |
50 |
Correct |
15 ms |
24852 KB |
Output is correct |
51 |
Correct |
14 ms |
24544 KB |
Output is correct |
52 |
Correct |
15 ms |
24532 KB |
Output is correct |
53 |
Correct |
15 ms |
24600 KB |
Output is correct |
54 |
Correct |
12 ms |
24532 KB |
Output is correct |
55 |
Correct |
11 ms |
23892 KB |
Output is correct |
56 |
Correct |
64 ms |
50312 KB |
Output is correct |
57 |
Correct |
81 ms |
49768 KB |
Output is correct |
58 |
Correct |
13 ms |
24532 KB |
Output is correct |
59 |
Correct |
12 ms |
23820 KB |
Output is correct |
60 |
Correct |
12 ms |
23892 KB |
Output is correct |
61 |
Correct |
86 ms |
49668 KB |
Output is correct |
62 |
Correct |
12 ms |
24592 KB |
Output is correct |
63 |
Correct |
105 ms |
65484 KB |
Output is correct |
64 |
Correct |
97 ms |
49808 KB |
Output is correct |
65 |
Correct |
13 ms |
24532 KB |
Output is correct |
66 |
Correct |
82 ms |
51344 KB |
Output is correct |
67 |
Correct |
13 ms |
24464 KB |
Output is correct |
68 |
Correct |
13 ms |
24592 KB |
Output is correct |
69 |
Correct |
64 ms |
50332 KB |
Output is correct |
70 |
Correct |
13 ms |
25128 KB |
Output is correct |
71 |
Correct |
78 ms |
49672 KB |
Output is correct |
72 |
Correct |
103 ms |
54864 KB |
Output is correct |
73 |
Correct |
11 ms |
23892 KB |
Output is correct |
74 |
Correct |
122 ms |
66088 KB |
Output is correct |
75 |
Correct |
86 ms |
57912 KB |
Output is correct |
76 |
Correct |
12 ms |
24532 KB |
Output is correct |
77 |
Correct |
16 ms |
24592 KB |
Output is correct |
78 |
Correct |
67 ms |
49368 KB |
Output is correct |
79 |
Correct |
82 ms |
54240 KB |
Output is correct |
80 |
Correct |
12 ms |
24532 KB |
Output is correct |
81 |
Correct |
12 ms |
24504 KB |
Output is correct |
82 |
Correct |
14 ms |
23816 KB |
Output is correct |
83 |
Correct |
10 ms |
23812 KB |
Output is correct |
84 |
Correct |
11 ms |
24592 KB |
Output is correct |
85 |
Correct |
106 ms |
51980 KB |
Output is correct |
86 |
Correct |
12 ms |
24608 KB |
Output is correct |
87 |
Correct |
121 ms |
49720 KB |
Output is correct |
88 |
Correct |
11 ms |
23908 KB |
Output is correct |
89 |
Correct |
101 ms |
49692 KB |
Output is correct |
90 |
Correct |
95 ms |
51868 KB |
Output is correct |
91 |
Correct |
138 ms |
54668 KB |
Output is correct |
92 |
Correct |
64 ms |
50332 KB |
Output is correct |
93 |
Correct |
12 ms |
24660 KB |
Output is correct |
94 |
Correct |
11 ms |
23812 KB |
Output is correct |
95 |
Correct |
79 ms |
54720 KB |
Output is correct |
96 |
Correct |
106 ms |
63312 KB |
Output is correct |
97 |
Correct |
13 ms |
24596 KB |
Output is correct |
98 |
Correct |
11 ms |
23892 KB |
Output is correct |
99 |
Correct |
98 ms |
52460 KB |
Output is correct |
100 |
Correct |
14 ms |
24544 KB |
Output is correct |
101 |
Correct |
114 ms |
50280 KB |
Output is correct |
102 |
Correct |
104 ms |
60328 KB |
Output is correct |
103 |
Correct |
13 ms |
24772 KB |
Output is correct |
104 |
Correct |
14 ms |
24788 KB |
Output is correct |
105 |
Correct |
14 ms |
24484 KB |
Output is correct |
106 |
Correct |
12 ms |
24524 KB |
Output is correct |
107 |
Correct |
12 ms |
24660 KB |
Output is correct |
108 |
Correct |
536 ms |
168968 KB |
Output is correct |
109 |
Correct |
815 ms |
196704 KB |
Output is correct |
110 |
Correct |
879 ms |
221364 KB |
Output is correct |
111 |
Correct |
900 ms |
233108 KB |
Output is correct |
112 |
Correct |
842 ms |
211592 KB |
Output is correct |
113 |
Correct |
552 ms |
182388 KB |
Output is correct |
114 |
Correct |
639 ms |
157064 KB |
Output is correct |
115 |
Correct |
686 ms |
157176 KB |
Output is correct |
116 |
Correct |
759 ms |
162160 KB |
Output is correct |
117 |
Correct |
745 ms |
182328 KB |
Output is correct |
118 |
Correct |
695 ms |
155528 KB |
Output is correct |
119 |
Correct |
736 ms |
182272 KB |
Output is correct |
120 |
Correct |
831 ms |
213088 KB |
Output is correct |
121 |
Correct |
809 ms |
182328 KB |
Output is correct |
122 |
Correct |
825 ms |
171440 KB |
Output is correct |
123 |
Correct |
550 ms |
185196 KB |
Output is correct |
124 |
Correct |
878 ms |
179000 KB |
Output is correct |