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 all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int mmax = 1e5 + 5;
vector<tii> scand[mmax];
struct DSU {
vector<int> dsu;
ll cost;
void init(int n) {
dsu.assign(n, 0);
cost = 0;
iota(all(dsu), 0);
}
int f(int x) { return dsu[x] == x? x : dsu[x] = f(dsu[x]); }
bool unite(int w, int x, int y) {
x = f(x);
y = f(y);
if(x == y) return 0;
dsu[x] = y;
cost += w;
return 1;
}
bool unite(tii a) {
return unite(get<0>(a), get<1>(a), get<2>(a));
}
};
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, m;
cin >> n >> m;
map<int, int> fpozc;
vector<tii> edges(m);
for(auto &[w, x, y] : edges) cin >> x >> y >> w, --x, --y;
sort(all(edges));
auto addTo = [&](tii a, vector<tii>& nv, vector<tii> R) {
R.insert(R.begin(), a);
DSU d;
d.init(n);
nv.clear();
for(auto t : R)
if(d.unite(t)) nv.emplace_back(t);
};
for(int i = m - 1; i >= 0; i--) {
addTo(edges[i], scand[i], scand[i + 1]);
}
vector<tii> pref;
int q, p = 0;
cin >> q;
for(int i = 0; i < q; i++) {
int X;
cin >> X;
while(p < m && get<0>(edges[p]) <= X)
addTo(edges[p], pref, pref),
p++;
vector<tii> L, R, mine;
for(auto [w, x, y] : pref) L.emplace_back(X - w, x, y);
for(auto [w, x, y] : scand[p]) R.emplace_back(w - X, x, y);
merge(all(L), all(R), back_inserter(mine));
DSU d;
d.init(n);
for(auto x : mine)
d.unite(x);
cout << d.cost << '\n';
}
}
/**
Istenem! Nu poate fi real.
-- Surse verificate
*/
# | 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... |