이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
auto compZero = [&] (Edge a, Edge b) { return a.p < b.p; };
sort(all(edges), compZero);
vector<vi> adjTree(nbNode);
auto add = [&] (int iEdge) {
const auto [u,v,_] = edges[iEdge];
adjTree[u].push_back(iEdge);
adjTree[v].push_back(iEdge);
};
auto remove = [&] (int iEdge) {
const auto [u,v,_] = edges[iEdge];
for (int c : {u, v})
adjTree[c].erase(find(all(adjTree[c]), iEdge));
};
auto Dfs = [&] (auto dfs, int node, int parent, int dest, pii &ans) -> bool {
if (node == dest) {
return true;
}
for (int iEdge : adjTree[node]) {
const auto [u,v,p] = edges[iEdge];
int child = u^v^node;
if (child == parent) continue;
if(dfs(dfs, child, node, dest, ans)) {
chmin(ans, make_pair(p, iEdge));
return true;
}
}
return false;
};
vector<pii> events;
int realAdds = 0;
rep(iCur, 0, nbEdge) {
auto &cur = edges[iCur];
pii ret {1e18, 0};
if (Dfs(Dfs, cur.u, -1, cur.v, ret)) {
auto [minTime, argMin] = ret;
int betterTime = (cur.p + minTime)/2;
remove(argMin);
add(iCur);
events.emplace_back(betterTime, minTime);
events.emplace_back(betterTime, -cur.p);
} else if (realAdds < nbNode-1) {
++realAdds;
add(iCur);
events.emplace_back(0, -cur.p);
}
}
sort(all(events));
cin >> nbReq;
vector<int> curMst;
int ptr = 0;
rep(iReq, 0, nbReq) {
int X; cin >> X; X *= 2;
while (ptr < sz(events) && events[ptr].first <= X) {
int u = events[ptr].second;
if (u < 0)
curMst.push_back(-u);
else
curMst.erase(find(all(curMst), u));
++ptr;
}
int ans = 0;
for (int p : curMst) {
ans += abs(p-X);
}
cout << ans/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... |