#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> f;
DSU(int n) : f(n) { iota(f.begin(), f.end(), 0); }
int leader(int u) {
while (u != f[u]) {
u = f[u] = f[f[u]];
}
return u;
}
bool unionize(int u, int v) {
u = leader(u);
v = leader(v);
if (u == v) return false;
f[v] = u;
return true;
}
bool same(int u, int v) {
return leader(u) == leader(v);
}
};
struct Tree {
const int INF = 1e9 + 12345;
int n;
vector<vector<array<int, 2>>> adj;
Tree(int n) : n(n), adj(n) {}
void addEdge(int u, int v, int w) {
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
void cutEdge(int u, int v, int w) {
adj[u].erase(find(adj[u].begin(), adj[u].end(), array<int, 2>{v, w}));
adj[v].erase(find(adj[v].begin(), adj[v].end(), array<int, 2>{u, w}));
}
array<int, 3> findMinEdge(int s, int t) {
vector<array<int, 2>> par(n, array<int, 2>{-1, -1});
queue<int> que;
que.push(s);
par[s] = {s, -1};
while (!que.empty()) {
int u = que.front();
que.pop();
if (u == t) break;
for (auto [v, w] : adj[u]) {
if (par[v][0] == -1) {
par[v][0] = u;
par[v][1] = w;
que.push(v);
}
}
}
int mn = INF;
int u, v;
while (t != s) {
auto [tt, w] = par[t];
if (w < mn) {
mn = w;
u = t;
v = tt;
}
t = tt;
}
return {u, v, mn};
}
};
int sgn(int x) {
if (x == 0) return 0;
return x < 0 ? -1 : +1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<array<int, 3>> edges(m);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
edges[i] = {u, v, w};
}
sort(edges.begin(), edges.end(), [&](array<int, 3> e0, array<int, 3> e1) { return e0[2] < e1[2]; });
vector<array<int, 2>> events_l = [&] {
vector<array<int, 2>> events;
DSU dsu(n);
Tree tree(n);
for (int i = 0; i < m; i++) {
auto [u, v, w] = edges[i];
if (dsu.same(u, v)) {
auto [uu, vv, ww] = tree.findMinEdge(u, v);
tree.cutEdge(uu, vv, ww);
if (ww < w) {
events.push_back({(ww + w) / 2 + 1, +w});
events.push_back({w + 1, -w});
}
} else {
events.push_back({1, +w});
events.push_back({w + 1, -w});
}
dsu.unionize(u, v);
tree.addEdge(u, v, w);
}
sort(events.begin(), events.end());
return events;
}();
vector<array<int, 2>> events_r = [&] {
vector<array<int, 2>> events;
DSU dsu(n);
Tree tree(n);
for (int i = m - 1; i >= 0; i--) {
auto [u, v, w] = edges[i];
if (dsu.same(u, v)) {
auto [uu, vv, ww] = tree.findMinEdge(u, v);
tree.cutEdge(uu, vv, ww);
ww = -ww;
events.push_back({w + 1, +w});
events.push_back({(ww + w) / 2 + 1, -w});
} else {
events.push_back({w + 1, +w});
events.push_back({(int)1e9 + 1, -w});
}
dsu.unionize(u, v);
tree.addEdge(u, v, -w);
}
sort(events.begin(), events.end());
return events;
}();
int ptr_l = 0, ptr_r;
long long sum_l = 0, sum_r = 0;
long long cnt_l = 0, cnt_r = 0;
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
while (ptr_l < events_l.size() && x >= events_l[ptr_l][0]) {
sum_l += events_l[ptr_l][1];
cnt_l += sgn(events_l[ptr_l][1]);
ptr_l++;
}
while (ptr_r < events_r.size() && x >= events_r[ptr_r][0]) {
sum_r += events_r[ptr_r][1];
cnt_r += sgn(events_r[ptr_r][1]);
ptr_r++;
}
cout << sum_l - cnt_l * x + cnt_r * x - sum_r << '\n';
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:156:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | while (ptr_l < events_l.size() && x >= events_l[ptr_l][0]) {
| ~~~~~~^~~~~~~~~~~~~~~~~
reconstruction.cpp:161:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
161 | while (ptr_r < events_r.size() && x >= events_r[ptr_r][0]) {
| ~~~~~~^~~~~~~~~~~~~~~~~
reconstruction.cpp: In member function 'std::array<int, 3> Tree::findMinEdge(int, int)':
reconstruction.cpp:74:25: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
74 | return {u, v, mn};
| ^
reconstruction.cpp:74:25: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:161:16: warning: 'ptr_r' may be used uninitialized in this function [-Wmaybe-uninitialized]
161 | while (ptr_r < events_r.size() && x >= events_r[ptr_r][0]) {
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
925 ms |
7988 KB |
Output is correct |
21 |
Correct |
192 ms |
7904 KB |
Output is correct |
22 |
Correct |
170 ms |
8000 KB |
Output is correct |
23 |
Correct |
211 ms |
7944 KB |
Output is correct |
24 |
Correct |
417 ms |
7904 KB |
Output is correct |
25 |
Correct |
900 ms |
8196 KB |
Output is correct |
26 |
Correct |
923 ms |
7996 KB |
Output is correct |
27 |
Correct |
930 ms |
7988 KB |
Output is correct |
28 |
Correct |
928 ms |
8164 KB |
Output is correct |
29 |
Correct |
812 ms |
5392 KB |
Output is correct |
30 |
Correct |
955 ms |
7940 KB |
Output is correct |
31 |
Correct |
933 ms |
7996 KB |
Output is correct |
32 |
Correct |
926 ms |
7908 KB |
Output is correct |
33 |
Correct |
931 ms |
8392 KB |
Output is correct |
34 |
Correct |
775 ms |
5548 KB |
Output is correct |
35 |
Correct |
937 ms |
7976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
298 ms |
27180 KB |
Output is correct |
5 |
Correct |
309 ms |
27236 KB |
Output is correct |
6 |
Correct |
299 ms |
27116 KB |
Output is correct |
7 |
Correct |
299 ms |
29108 KB |
Output is correct |
8 |
Correct |
290 ms |
29412 KB |
Output is correct |
9 |
Correct |
301 ms |
29264 KB |
Output is correct |
10 |
Correct |
295 ms |
27468 KB |
Output is correct |
11 |
Correct |
297 ms |
29320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
157 ms |
22412 KB |
Output is correct |
21 |
Correct |
159 ms |
22608 KB |
Output is correct |
22 |
Correct |
167 ms |
22516 KB |
Output is correct |
23 |
Correct |
155 ms |
22352 KB |
Output is correct |
24 |
Correct |
156 ms |
22348 KB |
Output is correct |
25 |
Correct |
164 ms |
22624 KB |
Output is correct |
26 |
Correct |
161 ms |
22468 KB |
Output is correct |
27 |
Correct |
157 ms |
22612 KB |
Output is correct |
28 |
Correct |
161 ms |
22352 KB |
Output is correct |
29 |
Correct |
157 ms |
22372 KB |
Output is correct |
30 |
Correct |
162 ms |
22628 KB |
Output is correct |
31 |
Correct |
157 ms |
22356 KB |
Output is correct |
32 |
Correct |
162 ms |
22868 KB |
Output is correct |
33 |
Correct |
159 ms |
22356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
925 ms |
7988 KB |
Output is correct |
21 |
Correct |
192 ms |
7904 KB |
Output is correct |
22 |
Correct |
170 ms |
8000 KB |
Output is correct |
23 |
Correct |
211 ms |
7944 KB |
Output is correct |
24 |
Correct |
417 ms |
7904 KB |
Output is correct |
25 |
Correct |
900 ms |
8196 KB |
Output is correct |
26 |
Correct |
923 ms |
7996 KB |
Output is correct |
27 |
Correct |
930 ms |
7988 KB |
Output is correct |
28 |
Correct |
928 ms |
8164 KB |
Output is correct |
29 |
Correct |
812 ms |
5392 KB |
Output is correct |
30 |
Correct |
955 ms |
7940 KB |
Output is correct |
31 |
Correct |
933 ms |
7996 KB |
Output is correct |
32 |
Correct |
926 ms |
7908 KB |
Output is correct |
33 |
Correct |
931 ms |
8392 KB |
Output is correct |
34 |
Correct |
775 ms |
5548 KB |
Output is correct |
35 |
Correct |
937 ms |
7976 KB |
Output is correct |
36 |
Correct |
959 ms |
8256 KB |
Output is correct |
37 |
Correct |
169 ms |
8120 KB |
Output is correct |
38 |
Correct |
175 ms |
8120 KB |
Output is correct |
39 |
Correct |
208 ms |
8044 KB |
Output is correct |
40 |
Correct |
423 ms |
8280 KB |
Output is correct |
41 |
Correct |
894 ms |
8364 KB |
Output is correct |
42 |
Correct |
937 ms |
8296 KB |
Output is correct |
43 |
Correct |
969 ms |
8168 KB |
Output is correct |
44 |
Correct |
947 ms |
8124 KB |
Output is correct |
45 |
Correct |
813 ms |
5836 KB |
Output is correct |
46 |
Correct |
959 ms |
8136 KB |
Output is correct |
47 |
Correct |
931 ms |
8096 KB |
Output is correct |
48 |
Correct |
939 ms |
8332 KB |
Output is correct |
49 |
Correct |
932 ms |
8168 KB |
Output is correct |
50 |
Correct |
775 ms |
5828 KB |
Output is correct |
51 |
Correct |
933 ms |
8152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
925 ms |
7988 KB |
Output is correct |
21 |
Correct |
192 ms |
7904 KB |
Output is correct |
22 |
Correct |
170 ms |
8000 KB |
Output is correct |
23 |
Correct |
211 ms |
7944 KB |
Output is correct |
24 |
Correct |
417 ms |
7904 KB |
Output is correct |
25 |
Correct |
900 ms |
8196 KB |
Output is correct |
26 |
Correct |
923 ms |
7996 KB |
Output is correct |
27 |
Correct |
930 ms |
7988 KB |
Output is correct |
28 |
Correct |
928 ms |
8164 KB |
Output is correct |
29 |
Correct |
812 ms |
5392 KB |
Output is correct |
30 |
Correct |
955 ms |
7940 KB |
Output is correct |
31 |
Correct |
933 ms |
7996 KB |
Output is correct |
32 |
Correct |
926 ms |
7908 KB |
Output is correct |
33 |
Correct |
931 ms |
8392 KB |
Output is correct |
34 |
Correct |
775 ms |
5548 KB |
Output is correct |
35 |
Correct |
937 ms |
7976 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
298 ms |
27180 KB |
Output is correct |
40 |
Correct |
309 ms |
27236 KB |
Output is correct |
41 |
Correct |
299 ms |
27116 KB |
Output is correct |
42 |
Correct |
299 ms |
29108 KB |
Output is correct |
43 |
Correct |
290 ms |
29412 KB |
Output is correct |
44 |
Correct |
301 ms |
29264 KB |
Output is correct |
45 |
Correct |
295 ms |
27468 KB |
Output is correct |
46 |
Correct |
297 ms |
29320 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
157 ms |
22412 KB |
Output is correct |
49 |
Correct |
159 ms |
22608 KB |
Output is correct |
50 |
Correct |
167 ms |
22516 KB |
Output is correct |
51 |
Correct |
155 ms |
22352 KB |
Output is correct |
52 |
Correct |
156 ms |
22348 KB |
Output is correct |
53 |
Correct |
164 ms |
22624 KB |
Output is correct |
54 |
Correct |
161 ms |
22468 KB |
Output is correct |
55 |
Correct |
157 ms |
22612 KB |
Output is correct |
56 |
Correct |
161 ms |
22352 KB |
Output is correct |
57 |
Correct |
157 ms |
22372 KB |
Output is correct |
58 |
Correct |
162 ms |
22628 KB |
Output is correct |
59 |
Correct |
157 ms |
22356 KB |
Output is correct |
60 |
Correct |
162 ms |
22868 KB |
Output is correct |
61 |
Correct |
159 ms |
22356 KB |
Output is correct |
62 |
Correct |
959 ms |
8256 KB |
Output is correct |
63 |
Correct |
169 ms |
8120 KB |
Output is correct |
64 |
Correct |
175 ms |
8120 KB |
Output is correct |
65 |
Correct |
208 ms |
8044 KB |
Output is correct |
66 |
Correct |
423 ms |
8280 KB |
Output is correct |
67 |
Correct |
894 ms |
8364 KB |
Output is correct |
68 |
Correct |
937 ms |
8296 KB |
Output is correct |
69 |
Correct |
969 ms |
8168 KB |
Output is correct |
70 |
Correct |
947 ms |
8124 KB |
Output is correct |
71 |
Correct |
813 ms |
5836 KB |
Output is correct |
72 |
Correct |
959 ms |
8136 KB |
Output is correct |
73 |
Correct |
931 ms |
8096 KB |
Output is correct |
74 |
Correct |
939 ms |
8332 KB |
Output is correct |
75 |
Correct |
932 ms |
8168 KB |
Output is correct |
76 |
Correct |
775 ms |
5828 KB |
Output is correct |
77 |
Correct |
933 ms |
8152 KB |
Output is correct |
78 |
Correct |
1084 ms |
26224 KB |
Output is correct |
79 |
Correct |
347 ms |
28424 KB |
Output is correct |
80 |
Correct |
336 ms |
27232 KB |
Output is correct |
81 |
Correct |
368 ms |
27356 KB |
Output is correct |
82 |
Correct |
578 ms |
26284 KB |
Output is correct |
83 |
Correct |
1079 ms |
26308 KB |
Output is correct |
84 |
Correct |
1104 ms |
26260 KB |
Output is correct |
85 |
Correct |
1084 ms |
26484 KB |
Output is correct |
86 |
Correct |
1087 ms |
26240 KB |
Output is correct |
87 |
Correct |
1006 ms |
26744 KB |
Output is correct |
88 |
Correct |
1085 ms |
26224 KB |
Output is correct |
89 |
Correct |
1092 ms |
26252 KB |
Output is correct |
90 |
Correct |
1076 ms |
26252 KB |
Output is correct |
91 |
Correct |
1087 ms |
26084 KB |
Output is correct |
92 |
Correct |
948 ms |
27872 KB |
Output is correct |
93 |
Correct |
1135 ms |
27616 KB |
Output is correct |