Submission #810486

#TimeUsernameProblemLanguageResultExecution timeMemory
810486hugo_pmReconstruction Project (JOI22_reconstruction)C++17
0 / 100
5076 ms66844 KiB
#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; } }; const int LG = 17; static_assert((1<<LG) > (int)(1e5)); 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}; vector<vi> anc(LG, vi(nbEdge, -1)); auto minPoids = anc; vector<bool> taken(nbEdge, false); uf firstMst(nbNode); vector<int> prof(nbNode); vector<vector<pii>> adjTree(nbNode); rep(iEdge, 0, nbEdge) { const auto [u,v,p] = edges[iEdge]; if (firstMst.unite(u, v)) { taken[iEdge] = true; adjTree[u].emplace_back(v, p); adjTree[v].emplace_back(u, p); } } auto Dfs = [&] (auto dfs, int node, int parent, int parPoids) -> void { if (parent != -1) { anc[0][node] = parent; minPoids[0][node] = parPoids; } for (auto [child, w] : adjTree[node]) if (child != parent) { prof[child] = prof[node]+1; dfs(dfs, child, node, w); } }; Dfs(Dfs, 0, -1, -1); rep(lvl, 0, LG-1) { rep(node, 0, nbNode) { int mid = anc[lvl][node]; if (mid != -1) { anc[lvl+1][node] = anc[lvl][mid]; minPoids[lvl+1][node] = min(minPoids[lvl][node], minPoids[lvl][mid]); } } } auto minChemin = [&] (int u, int v) { int ans = 3e18; auto jump = [&] (int lvl, int &node) { chmin(ans, minPoids[lvl][node]); node = anc[lvl][node]; }; if (prof[u] > prof[v]) swap(u, v); int firstJmp = prof[v] - prof[u]; rep(i, 0, LG) if ((1<<i) & firstJmp) { jump(i, v); } assert(prof[u] == prof[v]); if (u == v) return ans; for (int i = LG-1; i >= 0; --i) { int pu = anc[i][u], pv = anc[i][v]; if (pu != -1 && pv != -1 && pu != pv) { jump(i, u), jump(i, v); } } jump(0, u), jump(0, v); assert(u == v); return ans; }; rep(i, 0, nbEdge) { times.push_back(edges[i].p); if (!taken[i]) { int minTime = minChemin(edges[i].u, edges[i].v); times.push_back((edges[i].p + minTime)/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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...