# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
447159 |
2021-07-24T19:30:56 Z |
aryan12 |
Mergers (JOI19_mergers) |
C++17 |
|
1279 ms |
165328 KB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 5e5 + 5;
vector<int> g[N], states[N];
int depth[N];
int DP[20][N];
int par[N];
int Find(int x) {
if(par[x] == x) {
return x;
}
return par[x] = Find(par[x]);
}
void Unite(int a, int b) {
a = Find(a), b = Find(b);
if(depth[a] > depth[b]) {
swap(a, b);
}
par[b] = a;
}
void dfs(int node, int parent) {
DP[0][node] = parent;
for(auto i: g[node]) {
if(i == parent) {
continue;
}
depth[i] = depth[node] + 1;
dfs(i, node);
}
}
int LCA(int x, int y) {
if(depth[x] > depth[y]) {
swap(x, y);
}
int diff = depth[y] - depth[x];
for(int i = 19; i >= 0; i--) {
if((1 << i) <= diff) {
diff -= (1 << i);
y = DP[i][y];
}
}
if(x == y) {
return x;
}
for(int i = 19; i >= 0; i--) {
if(DP[i][x] != DP[i][y]) {
x = DP[i][x];
y = DP[i][y];
}
}
return DP[0][x];
}
bool cmp(int a, int b) {
return depth[a] > depth[b];
}
void Solve() {
for(int i = 0; i < N; i++) {
par[i] = i;
}
int n, k;
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
depth[1] = 1;
dfs(1, -1);
for(int i = 1; i < 20; i++) {
for(int j = 1; j <= n; j++) {
if(DP[i - 1][j] == -1) {
DP[i][j] = -1;
}
else {
DP[i][j] = DP[i - 1][DP[i - 1][j]];
}
}
}
for(int i = 1; i <= n; i++) {
int cur_state;
cin >> cur_state;
states[cur_state].push_back(i);
}
for(int i = 1; i <= k; i++) {
sort(states[i].begin(), states[i].end(), cmp);
/*cout << "i = " << i << ", size = " << states[i].size() << "\n";
for(int j = 0; j < states[i].size(); j++) {
cout << states[i][j] << " ";
}
cout << "\n";*/
int len = states[i].size();
int node = states[i][0];
for(int j = 1; j < len; j++) {
int lca = LCA(node, states[i][j]);
int cur_node = Find(node);
while(depth[cur_node] > depth[lca]) {
Unite(cur_node, DP[0][cur_node]);
cur_node = Find(cur_node);
}
cur_node = Find(states[i][j]);
while(depth[cur_node] > depth[lca]) {
Unite(cur_node, DP[0][cur_node]);
cur_node = Find(cur_node);
}
node = lca;
}
}
int degree[n + 1];
for(int i = 0; i <= n; i++) {
degree[i] = 0;
}
for(int i = 1; i <= n; i++) {
for(auto j: g[i]) {
if(Find(j) != Find(i) && j != DP[0][i]) {
degree[Find(i)]++;
degree[Find(j)]++;
}
}
}
int ans = 0;
/*for(int i = 1; i <= n; i++) {
cout << degree[i] << " ";
}
cout << "\n";
for(int i = 1; i <= n; i++) {
cout << Find(i) << " ";
}
cout << "\n";*/
for(int i = 1; i <= n; i++) {
if(degree[i] == 1) {
ans++;
}
}
cout << (ans + 1) / 2 << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27864 KB |
Output is correct |
2 |
Correct |
15 ms |
27828 KB |
Output is correct |
3 |
Correct |
15 ms |
27852 KB |
Output is correct |
4 |
Correct |
16 ms |
27864 KB |
Output is correct |
5 |
Correct |
15 ms |
27840 KB |
Output is correct |
6 |
Correct |
15 ms |
27832 KB |
Output is correct |
7 |
Correct |
15 ms |
27856 KB |
Output is correct |
8 |
Correct |
16 ms |
27868 KB |
Output is correct |
9 |
Correct |
15 ms |
27868 KB |
Output is correct |
10 |
Correct |
15 ms |
27852 KB |
Output is correct |
11 |
Correct |
15 ms |
27872 KB |
Output is correct |
12 |
Correct |
15 ms |
27824 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
15 ms |
27852 KB |
Output is correct |
15 |
Correct |
15 ms |
27852 KB |
Output is correct |
16 |
Correct |
15 ms |
27856 KB |
Output is correct |
17 |
Correct |
15 ms |
27852 KB |
Output is correct |
18 |
Correct |
15 ms |
27868 KB |
Output is correct |
19 |
Correct |
15 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27808 KB |
Output is correct |
21 |
Correct |
15 ms |
27852 KB |
Output is correct |
22 |
Correct |
15 ms |
27852 KB |
Output is correct |
23 |
Correct |
15 ms |
27872 KB |
Output is correct |
24 |
Correct |
15 ms |
27852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27864 KB |
Output is correct |
2 |
Correct |
15 ms |
27828 KB |
Output is correct |
3 |
Correct |
15 ms |
27852 KB |
Output is correct |
4 |
Correct |
16 ms |
27864 KB |
Output is correct |
5 |
Correct |
15 ms |
27840 KB |
Output is correct |
6 |
Correct |
15 ms |
27832 KB |
Output is correct |
7 |
Correct |
15 ms |
27856 KB |
Output is correct |
8 |
Correct |
16 ms |
27868 KB |
Output is correct |
9 |
Correct |
15 ms |
27868 KB |
Output is correct |
10 |
Correct |
15 ms |
27852 KB |
Output is correct |
11 |
Correct |
15 ms |
27872 KB |
Output is correct |
12 |
Correct |
15 ms |
27824 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
15 ms |
27852 KB |
Output is correct |
15 |
Correct |
15 ms |
27852 KB |
Output is correct |
16 |
Correct |
15 ms |
27856 KB |
Output is correct |
17 |
Correct |
15 ms |
27852 KB |
Output is correct |
18 |
Correct |
15 ms |
27868 KB |
Output is correct |
19 |
Correct |
15 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27808 KB |
Output is correct |
21 |
Correct |
15 ms |
27852 KB |
Output is correct |
22 |
Correct |
15 ms |
27852 KB |
Output is correct |
23 |
Correct |
15 ms |
27872 KB |
Output is correct |
24 |
Correct |
15 ms |
27852 KB |
Output is correct |
25 |
Correct |
16 ms |
27792 KB |
Output is correct |
26 |
Correct |
17 ms |
28492 KB |
Output is correct |
27 |
Correct |
17 ms |
28560 KB |
Output is correct |
28 |
Correct |
18 ms |
28676 KB |
Output is correct |
29 |
Correct |
17 ms |
28636 KB |
Output is correct |
30 |
Correct |
17 ms |
28492 KB |
Output is correct |
31 |
Correct |
15 ms |
27852 KB |
Output is correct |
32 |
Correct |
18 ms |
28708 KB |
Output is correct |
33 |
Correct |
15 ms |
27796 KB |
Output is correct |
34 |
Correct |
17 ms |
28500 KB |
Output is correct |
35 |
Correct |
18 ms |
28576 KB |
Output is correct |
36 |
Correct |
17 ms |
28468 KB |
Output is correct |
37 |
Correct |
17 ms |
28580 KB |
Output is correct |
38 |
Correct |
15 ms |
27848 KB |
Output is correct |
39 |
Correct |
17 ms |
28608 KB |
Output is correct |
40 |
Correct |
18 ms |
28500 KB |
Output is correct |
41 |
Correct |
17 ms |
28544 KB |
Output is correct |
42 |
Correct |
17 ms |
28620 KB |
Output is correct |
43 |
Correct |
16 ms |
28620 KB |
Output is correct |
44 |
Correct |
15 ms |
27852 KB |
Output is correct |
45 |
Correct |
17 ms |
28620 KB |
Output is correct |
46 |
Correct |
17 ms |
28492 KB |
Output is correct |
47 |
Correct |
15 ms |
27852 KB |
Output is correct |
48 |
Correct |
18 ms |
28504 KB |
Output is correct |
49 |
Correct |
17 ms |
28620 KB |
Output is correct |
50 |
Correct |
17 ms |
28580 KB |
Output is correct |
51 |
Correct |
17 ms |
28492 KB |
Output is correct |
52 |
Correct |
17 ms |
28620 KB |
Output is correct |
53 |
Correct |
17 ms |
28572 KB |
Output is correct |
54 |
Correct |
17 ms |
28580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27864 KB |
Output is correct |
2 |
Correct |
15 ms |
27828 KB |
Output is correct |
3 |
Correct |
15 ms |
27852 KB |
Output is correct |
4 |
Correct |
16 ms |
27864 KB |
Output is correct |
5 |
Correct |
15 ms |
27840 KB |
Output is correct |
6 |
Correct |
15 ms |
27832 KB |
Output is correct |
7 |
Correct |
15 ms |
27856 KB |
Output is correct |
8 |
Correct |
16 ms |
27868 KB |
Output is correct |
9 |
Correct |
15 ms |
27868 KB |
Output is correct |
10 |
Correct |
15 ms |
27852 KB |
Output is correct |
11 |
Correct |
15 ms |
27872 KB |
Output is correct |
12 |
Correct |
15 ms |
27824 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
15 ms |
27852 KB |
Output is correct |
15 |
Correct |
15 ms |
27852 KB |
Output is correct |
16 |
Correct |
15 ms |
27856 KB |
Output is correct |
17 |
Correct |
15 ms |
27852 KB |
Output is correct |
18 |
Correct |
15 ms |
27868 KB |
Output is correct |
19 |
Correct |
15 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27808 KB |
Output is correct |
21 |
Correct |
15 ms |
27852 KB |
Output is correct |
22 |
Correct |
15 ms |
27852 KB |
Output is correct |
23 |
Correct |
15 ms |
27872 KB |
Output is correct |
24 |
Correct |
15 ms |
27852 KB |
Output is correct |
25 |
Correct |
15 ms |
27816 KB |
Output is correct |
26 |
Correct |
83 ms |
51460 KB |
Output is correct |
27 |
Correct |
103 ms |
51268 KB |
Output is correct |
28 |
Correct |
17 ms |
28544 KB |
Output is correct |
29 |
Correct |
15 ms |
27852 KB |
Output is correct |
30 |
Correct |
15 ms |
27856 KB |
Output is correct |
31 |
Correct |
102 ms |
51152 KB |
Output is correct |
32 |
Correct |
17 ms |
28492 KB |
Output is correct |
33 |
Correct |
134 ms |
54340 KB |
Output is correct |
34 |
Correct |
102 ms |
51252 KB |
Output is correct |
35 |
Correct |
17 ms |
28492 KB |
Output is correct |
36 |
Correct |
112 ms |
50500 KB |
Output is correct |
37 |
Correct |
17 ms |
28500 KB |
Output is correct |
38 |
Correct |
17 ms |
28612 KB |
Output is correct |
39 |
Correct |
83 ms |
51376 KB |
Output is correct |
40 |
Correct |
17 ms |
28668 KB |
Output is correct |
41 |
Correct |
115 ms |
51012 KB |
Output is correct |
42 |
Correct |
125 ms |
51344 KB |
Output is correct |
43 |
Correct |
15 ms |
27852 KB |
Output is correct |
44 |
Correct |
113 ms |
54468 KB |
Output is correct |
45 |
Correct |
230 ms |
53448 KB |
Output is correct |
46 |
Correct |
17 ms |
28640 KB |
Output is correct |
47 |
Correct |
18 ms |
28532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
50100 KB |
Output is correct |
2 |
Correct |
83 ms |
52056 KB |
Output is correct |
3 |
Correct |
18 ms |
28632 KB |
Output is correct |
4 |
Correct |
18 ms |
28492 KB |
Output is correct |
5 |
Correct |
15 ms |
27844 KB |
Output is correct |
6 |
Correct |
15 ms |
27852 KB |
Output is correct |
7 |
Correct |
17 ms |
28492 KB |
Output is correct |
8 |
Correct |
196 ms |
52344 KB |
Output is correct |
9 |
Correct |
17 ms |
28504 KB |
Output is correct |
10 |
Correct |
115 ms |
51400 KB |
Output is correct |
11 |
Correct |
15 ms |
27820 KB |
Output is correct |
12 |
Correct |
108 ms |
50884 KB |
Output is correct |
13 |
Correct |
159 ms |
52268 KB |
Output is correct |
14 |
Correct |
99 ms |
53608 KB |
Output is correct |
15 |
Correct |
83 ms |
51412 KB |
Output is correct |
16 |
Correct |
17 ms |
28620 KB |
Output is correct |
17 |
Correct |
16 ms |
27916 KB |
Output is correct |
18 |
Correct |
109 ms |
53584 KB |
Output is correct |
19 |
Correct |
106 ms |
54700 KB |
Output is correct |
20 |
Correct |
17 ms |
28492 KB |
Output is correct |
21 |
Correct |
15 ms |
27852 KB |
Output is correct |
22 |
Correct |
171 ms |
52416 KB |
Output is correct |
23 |
Correct |
18 ms |
28496 KB |
Output is correct |
24 |
Correct |
127 ms |
51684 KB |
Output is correct |
25 |
Correct |
109 ms |
54724 KB |
Output is correct |
26 |
Correct |
17 ms |
28620 KB |
Output is correct |
27 |
Correct |
19 ms |
28560 KB |
Output is correct |
28 |
Correct |
17 ms |
28504 KB |
Output is correct |
29 |
Correct |
17 ms |
28508 KB |
Output is correct |
30 |
Correct |
17 ms |
28620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27864 KB |
Output is correct |
2 |
Correct |
15 ms |
27828 KB |
Output is correct |
3 |
Correct |
15 ms |
27852 KB |
Output is correct |
4 |
Correct |
16 ms |
27864 KB |
Output is correct |
5 |
Correct |
15 ms |
27840 KB |
Output is correct |
6 |
Correct |
15 ms |
27832 KB |
Output is correct |
7 |
Correct |
15 ms |
27856 KB |
Output is correct |
8 |
Correct |
16 ms |
27868 KB |
Output is correct |
9 |
Correct |
15 ms |
27868 KB |
Output is correct |
10 |
Correct |
15 ms |
27852 KB |
Output is correct |
11 |
Correct |
15 ms |
27872 KB |
Output is correct |
12 |
Correct |
15 ms |
27824 KB |
Output is correct |
13 |
Correct |
15 ms |
27852 KB |
Output is correct |
14 |
Correct |
15 ms |
27852 KB |
Output is correct |
15 |
Correct |
15 ms |
27852 KB |
Output is correct |
16 |
Correct |
15 ms |
27856 KB |
Output is correct |
17 |
Correct |
15 ms |
27852 KB |
Output is correct |
18 |
Correct |
15 ms |
27868 KB |
Output is correct |
19 |
Correct |
15 ms |
27852 KB |
Output is correct |
20 |
Correct |
15 ms |
27808 KB |
Output is correct |
21 |
Correct |
15 ms |
27852 KB |
Output is correct |
22 |
Correct |
15 ms |
27852 KB |
Output is correct |
23 |
Correct |
15 ms |
27872 KB |
Output is correct |
24 |
Correct |
15 ms |
27852 KB |
Output is correct |
25 |
Correct |
16 ms |
27792 KB |
Output is correct |
26 |
Correct |
17 ms |
28492 KB |
Output is correct |
27 |
Correct |
17 ms |
28560 KB |
Output is correct |
28 |
Correct |
18 ms |
28676 KB |
Output is correct |
29 |
Correct |
17 ms |
28636 KB |
Output is correct |
30 |
Correct |
17 ms |
28492 KB |
Output is correct |
31 |
Correct |
15 ms |
27852 KB |
Output is correct |
32 |
Correct |
18 ms |
28708 KB |
Output is correct |
33 |
Correct |
15 ms |
27796 KB |
Output is correct |
34 |
Correct |
17 ms |
28500 KB |
Output is correct |
35 |
Correct |
18 ms |
28576 KB |
Output is correct |
36 |
Correct |
17 ms |
28468 KB |
Output is correct |
37 |
Correct |
17 ms |
28580 KB |
Output is correct |
38 |
Correct |
15 ms |
27848 KB |
Output is correct |
39 |
Correct |
17 ms |
28608 KB |
Output is correct |
40 |
Correct |
18 ms |
28500 KB |
Output is correct |
41 |
Correct |
17 ms |
28544 KB |
Output is correct |
42 |
Correct |
17 ms |
28620 KB |
Output is correct |
43 |
Correct |
16 ms |
28620 KB |
Output is correct |
44 |
Correct |
15 ms |
27852 KB |
Output is correct |
45 |
Correct |
17 ms |
28620 KB |
Output is correct |
46 |
Correct |
17 ms |
28492 KB |
Output is correct |
47 |
Correct |
15 ms |
27852 KB |
Output is correct |
48 |
Correct |
18 ms |
28504 KB |
Output is correct |
49 |
Correct |
17 ms |
28620 KB |
Output is correct |
50 |
Correct |
17 ms |
28580 KB |
Output is correct |
51 |
Correct |
17 ms |
28492 KB |
Output is correct |
52 |
Correct |
17 ms |
28620 KB |
Output is correct |
53 |
Correct |
17 ms |
28572 KB |
Output is correct |
54 |
Correct |
17 ms |
28580 KB |
Output is correct |
55 |
Correct |
15 ms |
27816 KB |
Output is correct |
56 |
Correct |
83 ms |
51460 KB |
Output is correct |
57 |
Correct |
103 ms |
51268 KB |
Output is correct |
58 |
Correct |
17 ms |
28544 KB |
Output is correct |
59 |
Correct |
15 ms |
27852 KB |
Output is correct |
60 |
Correct |
15 ms |
27856 KB |
Output is correct |
61 |
Correct |
102 ms |
51152 KB |
Output is correct |
62 |
Correct |
17 ms |
28492 KB |
Output is correct |
63 |
Correct |
134 ms |
54340 KB |
Output is correct |
64 |
Correct |
102 ms |
51252 KB |
Output is correct |
65 |
Correct |
17 ms |
28492 KB |
Output is correct |
66 |
Correct |
112 ms |
50500 KB |
Output is correct |
67 |
Correct |
17 ms |
28500 KB |
Output is correct |
68 |
Correct |
17 ms |
28612 KB |
Output is correct |
69 |
Correct |
83 ms |
51376 KB |
Output is correct |
70 |
Correct |
17 ms |
28668 KB |
Output is correct |
71 |
Correct |
115 ms |
51012 KB |
Output is correct |
72 |
Correct |
125 ms |
51344 KB |
Output is correct |
73 |
Correct |
15 ms |
27852 KB |
Output is correct |
74 |
Correct |
113 ms |
54468 KB |
Output is correct |
75 |
Correct |
230 ms |
53448 KB |
Output is correct |
76 |
Correct |
17 ms |
28640 KB |
Output is correct |
77 |
Correct |
18 ms |
28532 KB |
Output is correct |
78 |
Correct |
80 ms |
50100 KB |
Output is correct |
79 |
Correct |
83 ms |
52056 KB |
Output is correct |
80 |
Correct |
18 ms |
28632 KB |
Output is correct |
81 |
Correct |
18 ms |
28492 KB |
Output is correct |
82 |
Correct |
15 ms |
27844 KB |
Output is correct |
83 |
Correct |
15 ms |
27852 KB |
Output is correct |
84 |
Correct |
17 ms |
28492 KB |
Output is correct |
85 |
Correct |
196 ms |
52344 KB |
Output is correct |
86 |
Correct |
17 ms |
28504 KB |
Output is correct |
87 |
Correct |
115 ms |
51400 KB |
Output is correct |
88 |
Correct |
15 ms |
27820 KB |
Output is correct |
89 |
Correct |
108 ms |
50884 KB |
Output is correct |
90 |
Correct |
159 ms |
52268 KB |
Output is correct |
91 |
Correct |
99 ms |
53608 KB |
Output is correct |
92 |
Correct |
83 ms |
51412 KB |
Output is correct |
93 |
Correct |
17 ms |
28620 KB |
Output is correct |
94 |
Correct |
16 ms |
27916 KB |
Output is correct |
95 |
Correct |
109 ms |
53584 KB |
Output is correct |
96 |
Correct |
106 ms |
54700 KB |
Output is correct |
97 |
Correct |
17 ms |
28492 KB |
Output is correct |
98 |
Correct |
15 ms |
27852 KB |
Output is correct |
99 |
Correct |
171 ms |
52416 KB |
Output is correct |
100 |
Correct |
18 ms |
28496 KB |
Output is correct |
101 |
Correct |
127 ms |
51684 KB |
Output is correct |
102 |
Correct |
109 ms |
54724 KB |
Output is correct |
103 |
Correct |
17 ms |
28620 KB |
Output is correct |
104 |
Correct |
19 ms |
28560 KB |
Output is correct |
105 |
Correct |
17 ms |
28504 KB |
Output is correct |
106 |
Correct |
17 ms |
28508 KB |
Output is correct |
107 |
Correct |
17 ms |
28620 KB |
Output is correct |
108 |
Correct |
948 ms |
151872 KB |
Output is correct |
109 |
Correct |
908 ms |
148352 KB |
Output is correct |
110 |
Correct |
914 ms |
155056 KB |
Output is correct |
111 |
Correct |
837 ms |
165328 KB |
Output is correct |
112 |
Correct |
846 ms |
163600 KB |
Output is correct |
113 |
Correct |
680 ms |
157556 KB |
Output is correct |
114 |
Correct |
767 ms |
144212 KB |
Output is correct |
115 |
Correct |
740 ms |
144168 KB |
Output is correct |
116 |
Correct |
1279 ms |
149492 KB |
Output is correct |
117 |
Correct |
741 ms |
158152 KB |
Output is correct |
118 |
Correct |
780 ms |
145028 KB |
Output is correct |
119 |
Correct |
742 ms |
158020 KB |
Output is correct |
120 |
Correct |
796 ms |
163904 KB |
Output is correct |
121 |
Correct |
744 ms |
158140 KB |
Output is correct |
122 |
Correct |
1197 ms |
152424 KB |
Output is correct |
123 |
Correct |
539 ms |
159104 KB |
Output is correct |
124 |
Correct |
823 ms |
152548 KB |
Output is correct |