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>
using namespace std;
const int N = 505;
const int M = 100005;
struct edge {
int u, v, w;
bool operator < (const edge &o) const {
return w < o.w;
}
};
struct disjoint {
int par[N];
void reset() {
iota(par, par + N, 0);
}
int find(int u) {
if (par[u] == u) return u;
return par[u] = find(par[u]);
}
void unite(int u, int v) {
par[find(v)] = find(u);
}
bool same(int u, int v) {
return find(u) == find(v);
}
} dsu;
edge grp[M], event[M * 3];
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++)
cin >> grp[i].u >> grp[i].v >> grp[i].w;
sort(grp, grp + m);
for (int i = 0; i < m; i++) {
int l = i - 1, r = i + 1;
dsu.reset();
while (l >= 0) {
dsu.unite(grp[l].u, grp[l].v);
if (dsu.same(grp[i].u, grp[i].v)) break;
l--;
}
dsu.reset();
while (r < m) {
dsu.unite(grp[r].u, grp[r].v);
if (dsu.same(grp[i].u, grp[i].v)) break;
r++;
}
event[i * 3] = {grp[i].w, -1, l < 0 ?
-1 : (grp[l].w + grp[i].w + 1) / 2};
event[i * 3 + 1] = {-2 * grp[i].w, 2, grp[i].w};
event[i * 3 + 2] = {grp[i].w, -1, r == m ?
1e9 + 1 : (grp[r].w + grp[i].w + 1) / 2};
}
sort(event, event + m * 3);
int q; cin >> q;
int mul = 0; long long add = 0;
for (int i = 0, j = 0; i < q; i++) {
long long cur; cin >> cur;
while (j < 3 * m && event[j].w <= cur) {
add += event[j].u;
mul += event[j++].v;
}
cout << add + cur * mul << '\n';
}
}
Compilation message (stderr)
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:62:50: warning: narrowing conversion of '((r == m) ? (1.0e+9 + (double)1) : (double)((grp[r].edge::w + grp[i].edge::w + 1) / 2))' from 'double' to 'int' [-Wnarrowing]
62 | event[i * 3 + 2] = {grp[i].w, -1, r == m ?
| ~~~~~~~^
63 | 1e9 + 1 : (grp[r].w + grp[i].w + 1) / 2};
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |