Submission #810497

#TimeUsernameProblemLanguageResultExecution timeMemory
810497hugo_pmReconstruction Project (JOI22_reconstruction)C++17
0 / 100
5059 ms27884 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; } }; 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<bool> taken(nbEdge, false); uf firstMst(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 dest, int &ans) -> bool { if (node == dest) { return true; } for (auto [child, w] : adjTree[node]) if (child != parent) { if(dfs(dfs, child, node, dest, ans)) { chmin(ans, w); return true; } } return false; }; rep(i, 0, nbEdge) { times.push_back(edges[i].p); if (!taken[i]) { int minTime = 1e18; Dfs(Dfs, edges[i].u, -1, edges[i].v, minTime); assert(1 <= minTime && minTime <= edges[i].p); times.push_back((edges[i].p + minTime)/2); } } sort(all(times)); times.resize(unique(all(times)) - begin(times)); auto getMst = [&] (int X) { uf mst(nbNode); sort(all(edges), [&] (Edge a, Edge b) { return abs(a.p-X) < abs(b.p-X); }); MST info; for (const auto [u,v,p] : edges) { 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...