제출 #938610

#제출 시각아이디문제언어결과실행 시간메모리
938610Gromp15Graph (BOI20_graph)C++17
58 / 100
1099 ms25856 KiB
#include <bits/stdc++.h> #define ll long long #define ar array #define db double #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define rint(l, r) uniform_int_distribution<int>(l, r)(rng) template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } void test_case() { int n, m; cin >> n >> m; vector<vector<ar<int, 2>>> adj(n+1); vector<ar<int, 3>> E; for (int i = 0; i < m; i++) { int x, y, w; cin >> x >> y >> w; E.push_back({x, y, w}); adj[x].push_back({y, 2*w}); adj[y].push_back({x, 2*w}); } vector<int> ans(n+1); vector<bool> vis2(n+1); vector<int> depth(n+1), val(n+1); for (int v = 1; v <= n; v++) if (!vis2[v]) { ll tot_sum = 0; vector<ar<int, 3>> edges; vector<int> comp; auto dfs = [&](auto&& s, int v, int cur) -> void { vis2[v] = 1; comp.emplace_back(v); val[v] = cur; tot_sum += cur; for (auto [u, w] : adj[v]) { edges.push_back({v, u, w}); if (!vis2[u]) { depth[u] = depth[v] + 1; s(s, u, w - cur); } } }; dfs(dfs, v, 0); // val[x] + val[y] +/- 2x = 2*w // tot_sum + totx = final val int need; bool f = 1; for (auto [x, y, w] : edges) { if ((depth[x] + depth[y]) & 1) { // they must have the same val if (val[x] + val[y] != w) { cout << "NO\n"; return; } } else { // val[x] + val[y] +/- 2x = w int other = (w - val[x] - val[y]) * (depth[x] & 1 ? -1 : 1); assert(other % 2 == 0); other /= 2; if (f) { need = other, f = 0; } else { if (need != other) { cout << "NO\n"; return; } } } } if (f) { // choose x that minimizes tot_sum + tot*x ll best = 1e18; int who = -1; for (int i = -2*sz(comp); i <= 2*sz(comp); i++) { ll sum = 0; for (int u : comp) { sum += abs(val[u] + i * (depth[u] & 1 ? -1 : 1)); } if (ckmin(best, sum)) who = i; } for (int u : comp) { ans[u] = val[u] + who * (depth[u] & 1 ? -1 : 1); } } else { // x is set for (int u : comp) { ans[u] = val[u] + need * (depth[u] & 1 ? -1 : 1); } } } cout << "YES\n"; cout << fixed << setprecision(1); for (int i = 1; i <= n; i++) cout << (db)ans[i] / 2 << " \n"[i==n]; } int main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while (t--) test_case(); }
#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...