This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7;
struct DSU {
int n;
vi p, sz;
DSU(int _n) {
n = _n;
p = vi(n), sz = vi(n, 1);
iota(ALL(p), 0);
}
int find(int u) {
return p[u] == u ? u : p[u] = find(p[u]);
}
void unite(int u, int v) {
u = find(u), v = find(v);
assert(u != v);
if(sz[u] > sz[v]) swap(u, v);
p[u] = v;
sz[v] += sz[u];
}
};
signed main()
{
IO_OP;
int n, m;
cin >> n >> m;
V<array<int, 3>> es;
for(int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w, u--, v--;
es.PB({u, v, w});
}
sort(ALL(es), [&] (array<int, 3> a, array<int, 3> b) {
return a[2] < b[2];
});
V<array<int, 3>> ev;
for(int _ = 0; _ < 2; _++) {
V<array<int, 3>> tree;
for(int i = 0; i < m; i++) {
reverse(ALL(tree));
tree.PB(es[i]);
V<array<int, 3>> ntree;
DSU dsu(n);
array<int, 3> bye = {-1, -1, -1};
for(int j = SZ(tree) - 1; j >= 0; j--) {
auto[u, v, w] = tree[j];
if(dsu.find(u) == dsu.find(v)) {
bye = tree[j];
} else {
ntree.PB(tree[j]);
dsu.unite(u, v);
}
}
if(bye[0] != -1) {
if(_ == 0) {
int mid = bye[2] + es[i][2];
ev.PB({mid + 1, es[i][2], _});
ev.PB({es[i][2] * 2 + 1, -es[i][2], -_});
} else {
int mid = bye[2] + es[i][2];
ev.PB({es[i][2] * 2 + 1, -es[i][2], _});
ev.PB({mid + 1, es[i][2], -_});
}
} else {
if(_ == 0) {
ev.PB({0, es[i][2], _});
ev.PB({es[i][2] * 2 + 1, -es[i][2], -_});
} else {
ev.PB({es[i][2] * 2 + 1, -es[i][2], _});
}
}
tree = ntree;
}
reverse(ALL(es));
}
sort(ALL(ev));
int q;
cin >> q;
ll sum = 0, cnt = 0;
for(int i = 0, j = 0; i < q; i++) {
int x;
cin >> x;
while(j < SZ(ev) && ev[j][0] <= x * 2) {
cnt += ev[j][2], sum += ev[j++][1];
}
cout << sum + 1LL * x * cnt - 1LL * x * (n - 1 - cnt) << '\n';
}
}
Compilation message (stderr)
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:80:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | auto[u, v, w] = tree[j];
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |