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;
typedef long long ll;
#ifndef LOCAL
#define cerr if(false)cerr
#endif
const ll MAX_N = 1e4 + 10;
ll n, m;
pair<ll, pair<ll, ll> > edg[MAX_N];
struct {
ll par[MAX_N], sz[MAX_N];
vector<ll> now;
void reset() {
for(ll i = 1; i <= n; i ++) {
par[i] = i;
sz[i] = 1;
}
now.resize(0);
}
ll find(ll x) {
if(par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
bool merge(ll x, ll y, ll c) {
x = find(x); y = find(y);
if(x == y) { return false; }
if(sz[x] < sz[y]) { swap(x, y); }
par[y] = x;
sz[x] += sz[y];
now.push_back(c);
return true;
}
} dsu;
vector<ll> cand[MAX_N];
ll lft[MAX_N], rght[MAX_N];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m;
for(ll i = 0; i < m; i ++) {
cin >> edg[i].second.first >> edg[i].second.second >> edg[i].first;
}
sort(edg, edg + m);
dsu.reset();
for(ll i = 0; i < m; i ++) {
int other = -1;
dsu.reset();
dsu.merge(edg[i].second.first, edg[i].second.second, i);
if(i != 0) for(const auto j : cand[i - 1]) {
if(!dsu.merge(edg[j].second.first, edg[j].second.second, j)) {
other = j;
}
}
cand[i] = dsu.now;
if(other != -1) {
lft[i] = (edg[i].first + edg[other].first + 2ll) / 2ll;
rght[other] = (edg[i].first + edg[other].first) / 2ll;
} else {
lft[i] = -1e12;
}
rght[i] = 1e12;
}
for(int i = 0; i < m; i ++) {
cerr << edg[i].first << " " << edg[i].second.first << " " << edg[i].second.second
<< " " << lft[i] << " " << rght[i] << endl;
}
ll q;
cin >> q;
while(q --) {
ll x;
cin >> x;
ll ans = 0;
for(int i = 0; i < m; i ++) {
if(lft[i] <= x && x <= rght[i]) {
ans += abs(x - edg[i].first);
}
}
cout << ans << endl;
}
}
# | 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... |