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 int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct uf {
int N;
vector<int> repr, taille;
void rebuild() {
iota(repr.begin(), repr.end(), 0);
taille.assign(N, 1);
}
uf(int _N) : N(_N), repr(_N) {
rebuild();
}
int find(int u) {
if (repr[u] == u) return u;
return repr[u] = find(repr[u]);
}
bool unite(int u, int v) {
u = find(u), v = find(v);
if (u == v) return false;
if (taille[u] < taille[v]) swap(u, v);
repr[v] = u;
taille[u] += taille[v];
return true;
}
};
struct Edge {
int u, v, p;
};
int nbNode, nbEdge, nbReq;
vector<Edge> edges;
vector<pii> reqs;
struct MST {
int cntDown = 0, sumDown = 0, cntUp = 0, sumUp = 0, cntEgal = 0, sumEgal = 0;
int egalDown(int X) {
// up - X, X - down
return sumUp - sumDown - sumEgal + (cntEgal+cntDown - cntUp)*X;
}
int egalUp(int X) {
// up - X, X - down
return sumUp - sumDown + sumEgal + (-cntEgal+cntDown - cntUp)*X;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> nbNode >> nbEdge;
edges.resize(nbEdge);
for (auto &[u,v,p] : edges) {
cin >> u >> v >> p;
--u, --v;
p *= 2;
}
sort(all(edges), [&] (Edge a, Edge b) { return a.p < b.p; });
vector<int> times {0, (int)1e18};
rep(i, 0, nbEdge) {
times.push_back(edges[i].p);
// |a - x| = |b - x|, a < b
// il faut a < x < b
// x - a = b - x <-> 2x = a+b <-> x = (a+b)/2
int j = i+nbNode-1;
if (j < nbEdge)
times.push_back((edges[i].p + edges[j].p)/2);
}
sort(all(times));
times.resize(unique(all(times)) - begin(times));
uf mst(nbNode);
vector<int> order(nbEdge);
iota(all(order), 0);
auto getMst = [&] (int X) {
int N = nbEdge;
while (N >= 1) {
int newN = 0;
rep(i, 1, N) {
if (abs(edges[order[i-1]].p - X) > abs(edges[order[i]].p - X)) {
swap(order[i-1], order[i]);
newN = i;
}
}
N = newN;
}
// assert(is_sorted(all(order), [&] (int i, int j) {
// const Edge &a = edges[i];
// const Edge &b = edges[j];
// return abs(a.p-X) < abs(b.p-X);
// }));
mst.rebuild();
MST info;
for (int iEdge : order) {
auto [u, v, p] = edges[iEdge];
if (mst.unite(u,v)) {
if (p < X) info.cntDown++, info.sumDown += p;
else if (p == X) info.cntEgal++, info.sumEgal += p;
else info.cntUp++, info.sumUp += p;
}
}
return info;
};
cin >> nbReq;
reqs.resize(nbReq);
rep(iReq, 0, nbReq) {
cin >> reqs[iReq].first;
reqs[iReq].first *= 2;
reqs[iReq].second = iReq;
}
sort(all(reqs));
vector<int> ans(nbReq);
int ptr = 0;
auto v1 = getMst(times[ptr]), v2 = getMst(times[ptr+1]);
for (auto [X, iReq] : reqs) {
while (X >= times[ptr+1]) {
++ptr;
swap(v1, v2);
v2 = getMst(times[ptr+1]);
}
ans[iReq] = min(v1.egalDown(X), v2.egalUp(X));
//ans[iReq] = getMst(X).calc(X);
assert(ans[iReq] % 2 == 0);
}
rep(i, 0, nbReq) cout << ans[i]/2 << '\n';
}
# | 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... |