제출 #810852

#제출 시각아이디문제언어결과실행 시간메모리
810852hugo_pmReconstruction Project (JOI22_reconstruction)C++17
3 / 100
1821 ms1048576 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; } auto compZero = [&] (Edge a, Edge b) { return a.p < b.p; }; sort(all(edges), compZero); vector<vi> adjTree(nbNode); vector<int> curPoids; auto add = [&] (int iEdge) { const auto [u,v,p] = edges[iEdge]; curPoids.push_back(p); adjTree[u].push_back(iEdge); adjTree[v].push_back(iEdge); }; auto remove = [&] (int iEdge) { const auto [u,v,p] = edges[iEdge]; curPoids.erase(find(all(curPoids), p)); 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; }; priority_queue<pii, vector<pii>, greater<pii>> pq; vector<pair<int, vi>> times; rep(i, 0, nbEdge) { pq.push({0, i}); } int prevTime = 0; while (!pq.empty()) { auto [curTime, iCur] = pq.top(); pq.pop(); pii ret {1e18, 0}; if (prevTime != curTime) { times.emplace_back(prevTime, curPoids); prevTime = curTime; } if (Dfs(Dfs, edges[iCur].u, -1, edges[iCur].v, ret)) { auto [minTime, argMin] = ret; assert(1 <= minTime && minTime <= edges[iCur].p); int betterTime = (edges[iCur].p + minTime)/2; if (curTime >= betterTime) { assert(curTime == betterTime); remove(argMin); add(iCur); times.emplace_back(betterTime, curPoids); } else { pq.push({betterTime, iCur}); } } else { add(iCur); } } if (times.empty()) { times.emplace_back(0, curPoids); } dbg(sz(times)); times.emplace_back(1e18, curPoids); sort(all(times)); 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; for (auto [X, iReq] : reqs) { while (X >= times[ptr+1].first) { ++ptr; } ans[iReq] = 1e18; rep(d, 0, 1) { int s = 0; for (int p : times[ptr+d].second) s += abs(p-X); chmin(ans[iReq], s); } //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...