이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//besmellah
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ers(v, X) adj[v].erase(find(adj[v].begin(), adj[v].end(), (X)))
using namespace std;
using ar = array<int, 2>;
using ar3 = array<int, 3>;
const int M = 1e5 + 5, N = 505, Q = 1e6 + 5;
const long long INF = 1ll << 60;
namespace dsu {
int dpr[N];
void init() {
iota(dpr, dpr + N, 0);
}
int gpr(int x) {
return dpr[x] == x ? x : dpr[x] = gpr(dpr[x]);
}
bool merge(int u, int v) {
u = gpr(u), v = gpr(v);
dpr[u] = v;
return u != v;
}
}
vector<ar> adj[N];
int n, m;
ar3 W[N], E[N];
bitset<N> seen;
void dfs(int v, int p) {
seen[v] = true;
for (auto [u, w]: adj[v]) if (u != p) {
W[u] = max(W[v], {w, u, v});
dfs(u, v);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
FOR(i, 0, m) {
cin >> E[i][1] >> E[i][2] >> E[i][0];
--E[i][1], --E[i][2];
if (E[i][1] > E[i][2]) swap(E[i][1], E[i][2]);
}
sort(E, E + m);
vector<ar3> Es, undo;
for (int i = m - 1; i >= 0; i--) { //ino mishe behtar kard ba DSU vali halesh nist (orderesham ye log kamtar dare)
auto [w, u, v] = E[i];
W[u] = {-INF, u, u};
seen = 0;
dfs(u, u);
if (seen[v]) {
auto [dw, du, dv] = W[v];
if (du > dv) swap(du, dv);
ers(du, (ar{dv, dw}));
ers(dv, (ar{du, dw}));
Es.erase(find(Es.begin(), Es.end(), ar3{dw, du, dv}));
//cout << "DELETING " << du + 1 << ' ' << dv + 1 << " " << dw << endl;
undo.push_back({dw, du, dv});
}
else
undo.push_back({-1, -1, -1});
//cout << "ADDING " << u + 1 << ' ' << v + 1 << ' ' << w << endl;
Es.push_back(E[i]);
adj[v].push_back({u, w});
adj[u].push_back({v, w});
}
//for (auto [w, u, v]: Es) cout << u << ' ' << v << ' ' << w << endl;
//now MST at zero
vector<ar3> jelo;
//be zaman ahamiat nemidaham
int q; cin >> q; int p = 0;
while (q--) {
int x; cin >> x;
while (p < m && E[p][0] < x) { //ino hesesh bood vali log ziad dare
jelo.push_back(E[p]);
vector<ar3> ok;
dsu::init();
sort(jelo.rbegin(), jelo.rend());
for (auto [w, u, v]: jelo) if (dsu::merge(u, v)) ok.push_back({w, u, v});
jelo = ok;
Es.erase(find(Es.begin(), Es.end(), E[p]));
auto akh = undo.back();
undo.pop_back();
if (akh[0] != -1) Es.push_back(akh);
++p;
}
vector<ar3> hame;
hame.reserve(jelo.size() + Es.size());
for (auto [w, u, v]: jelo) hame.push_back({abs(x - w), u, v});
for (auto [w, u, v]: Es) hame.push_back({abs(x - w), u, v});
sort(hame.begin(), hame.end());
dsu::init();
int ans = 0;
for (auto [w, u, v]: hame) ans += dsu::merge(u, v) ? w : 0;
cout << ans << '\n';
}
return 0;
}
# | 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... |