#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int Q = 1e5 + 1, N = 500;
vector<int> save[Q];
array<int, 3> e[Q];
int fa[N];
void init() {
iota(fa, fa + N, 0);
}
int leader(int x) {
while (x != fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
bool unite(int x, int y) {
x = leader(x), y = leader(y);
if (x == y) {
return false;
}
fa[y] = x;
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> e[i][1] >> e[i][2] >> e[i][0];
e[i][1] -= 1, e[i][2] -= 1;
}
sort(e, e + m);
int q;
cin >> q;
vector<int> X(q);
vector<ll> ans(q);
for (int &x : X) {
cin >> x;
}
constexpr int inf = 2e9;
for (int t = 0; t < 2; ++t) {
vector<int> mst;
int pnt = 0;
for (int i = 0; i <= m; ++i) {
if (i > 0) {
mst.insert(mst.begin(), (t == 0 ? m - i : i - 1));
init();
for (int ii = 0; ii < mst.size(); ++ii) {
if (!unite(e[mst[ii]][1], e[mst[ii]][2])) {
mst.erase(mst.begin() + ii);
break;
}
}
if (t == 0) {
save[m - i] = mst;
}
}
if (t == 1) {
vector<int> oth = save[i];
while (pnt < q && X[pnt] < (i == m ? inf : e[i][0])) {
init();
int L = 0, R = 0, cnt = 0;
while (cnt < n - 1) {
int weightL = L == mst.size() ? inf : X[pnt] - e[mst[L]][0];
int weightR = R == oth.size() ? inf : e[oth[R]][0] - X[pnt];
if (weightL < weightR) {
if (unite(e[mst[L]][1], e[mst[L]][2])) {
ans[pnt] += weightL;
cnt += 1;
}
L += 1;
} else {
if (unite(e[oth[R]][1], e[oth[R]][2])) {
ans[pnt] += weightR;
cnt += 1;
}
R += 1;
}
}
pnt += 1;
}
}
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:70:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int ii = 0; ii < mst.size(); ++ii) {
| ~~~^~~~~~~~~~~~
reconstruction.cpp:91:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | int weightL = L == mst.size() ? inf : X[pnt] - e[mst[L]][0];
| ~~^~~~~~~~~~~~~
reconstruction.cpp:92:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | int weightR = R == oth.size() ? inf : e[oth[R]][0] - X[pnt];
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2680 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2680 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1372 ms |
202044 KB |
Output is correct |
21 |
Correct |
668 ms |
191724 KB |
Output is correct |
22 |
Correct |
685 ms |
199920 KB |
Output is correct |
23 |
Correct |
762 ms |
201724 KB |
Output is correct |
24 |
Correct |
1036 ms |
201996 KB |
Output is correct |
25 |
Correct |
1376 ms |
202204 KB |
Output is correct |
26 |
Correct |
1353 ms |
202260 KB |
Output is correct |
27 |
Correct |
1368 ms |
202112 KB |
Output is correct |
28 |
Correct |
1363 ms |
202204 KB |
Output is correct |
29 |
Correct |
629 ms |
195452 KB |
Output is correct |
30 |
Correct |
1554 ms |
202216 KB |
Output is correct |
31 |
Correct |
1454 ms |
202208 KB |
Output is correct |
32 |
Correct |
1378 ms |
202220 KB |
Output is correct |
33 |
Correct |
1359 ms |
202288 KB |
Output is correct |
34 |
Correct |
610 ms |
137332 KB |
Output is correct |
35 |
Correct |
1332 ms |
202248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Execution timed out |
5041 ms |
213520 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2680 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Execution timed out |
5067 ms |
17336 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2680 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1372 ms |
202044 KB |
Output is correct |
21 |
Correct |
668 ms |
191724 KB |
Output is correct |
22 |
Correct |
685 ms |
199920 KB |
Output is correct |
23 |
Correct |
762 ms |
201724 KB |
Output is correct |
24 |
Correct |
1036 ms |
201996 KB |
Output is correct |
25 |
Correct |
1376 ms |
202204 KB |
Output is correct |
26 |
Correct |
1353 ms |
202260 KB |
Output is correct |
27 |
Correct |
1368 ms |
202112 KB |
Output is correct |
28 |
Correct |
1363 ms |
202204 KB |
Output is correct |
29 |
Correct |
629 ms |
195452 KB |
Output is correct |
30 |
Correct |
1554 ms |
202216 KB |
Output is correct |
31 |
Correct |
1454 ms |
202208 KB |
Output is correct |
32 |
Correct |
1378 ms |
202220 KB |
Output is correct |
33 |
Correct |
1359 ms |
202288 KB |
Output is correct |
34 |
Correct |
610 ms |
137332 KB |
Output is correct |
35 |
Correct |
1332 ms |
202248 KB |
Output is correct |
36 |
Correct |
1892 ms |
201260 KB |
Output is correct |
37 |
Correct |
1072 ms |
191864 KB |
Output is correct |
38 |
Correct |
1109 ms |
198732 KB |
Output is correct |
39 |
Correct |
1260 ms |
200636 KB |
Output is correct |
40 |
Correct |
1534 ms |
201036 KB |
Output is correct |
41 |
Correct |
1879 ms |
201140 KB |
Output is correct |
42 |
Correct |
1894 ms |
201172 KB |
Output is correct |
43 |
Correct |
1912 ms |
201128 KB |
Output is correct |
44 |
Correct |
1728 ms |
201128 KB |
Output is correct |
45 |
Correct |
757 ms |
194660 KB |
Output is correct |
46 |
Correct |
2045 ms |
201136 KB |
Output is correct |
47 |
Correct |
2094 ms |
201400 KB |
Output is correct |
48 |
Correct |
2111 ms |
201652 KB |
Output is correct |
49 |
Correct |
2008 ms |
201636 KB |
Output is correct |
50 |
Correct |
779 ms |
136788 KB |
Output is correct |
51 |
Correct |
1761 ms |
201696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2680 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
1372 ms |
202044 KB |
Output is correct |
21 |
Correct |
668 ms |
191724 KB |
Output is correct |
22 |
Correct |
685 ms |
199920 KB |
Output is correct |
23 |
Correct |
762 ms |
201724 KB |
Output is correct |
24 |
Correct |
1036 ms |
201996 KB |
Output is correct |
25 |
Correct |
1376 ms |
202204 KB |
Output is correct |
26 |
Correct |
1353 ms |
202260 KB |
Output is correct |
27 |
Correct |
1368 ms |
202112 KB |
Output is correct |
28 |
Correct |
1363 ms |
202204 KB |
Output is correct |
29 |
Correct |
629 ms |
195452 KB |
Output is correct |
30 |
Correct |
1554 ms |
202216 KB |
Output is correct |
31 |
Correct |
1454 ms |
202208 KB |
Output is correct |
32 |
Correct |
1378 ms |
202220 KB |
Output is correct |
33 |
Correct |
1359 ms |
202288 KB |
Output is correct |
34 |
Correct |
610 ms |
137332 KB |
Output is correct |
35 |
Correct |
1332 ms |
202248 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
2 ms |
2680 KB |
Output is correct |
38 |
Correct |
1 ms |
2644 KB |
Output is correct |
39 |
Execution timed out |
5041 ms |
213520 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |