제출 #1129958

#제출 시각아이디문제언어결과실행 시간메모리
1129958vladiliusGraph (BOI20_graph)C++20
100 / 100
105 ms30044 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define pb push_back #define ff first #define ss second #define arr3 array<int, 3> struct dsu{ vector<int> sz, p; int n; dsu(int ns){ n = ns; sz.resize(n + 1); p.resize(n + 1); for (int i = 1; i <= n; i++){ p[i] = i; sz[i] = 1; } } int get(int v){ if (p[v] != v){ p[v] = get(p[v]); } return p[v]; } bool unite(int x, int y){ x = get(x); y = get(y); if (x == y) return 0; if (sz[x] > sz[y]) swap(x, y); p[x] = y; sz[y] += sz[x]; return 1; } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin>>n>>m; vector<arr3> ed; while (m--){ int x, y, w; cin>>x>>y>>w; ed.pb({x, y, w}); } vector<pii> g[n + 1]; dsu T(n); for (auto [x, y, w]: ed){ if (T.unite(x, y)){ g[x].pb({y, w}); g[y].pb({x, w}); } } vector<int> a(n + 1), b(n + 1); function<void(int, int)> dfs = [&](int v, int pr){ for (auto [i, w]: g[v]){ if (i == pr) continue; b[i] = w - b[v]; a[i] = -a[v]; dfs(i, v); } }; vector<int> all[n + 1]; vector<arr3> e[n + 1]; for (int i = 1; i <= n; i++) all[T.get(i)].pb(i); for (auto p: ed){ e[T.get(p[0])].pb(p); } vector<double> out(n + 1); for (int i = 1; i <= n; i++){ if (T.get(i) != i) continue; a[i] = 1; b[i] = 0; dfs(i, 0); double X = 0; bool ind = 1; for (auto [x, y, w]: e[i]){ int p = a[x] + a[y], q = b[x] + b[y]; if (!p){ if (q != w){ cout<<"NO"<<"\n"; return 0; } continue; } if (ind){ X = 1.0 * (w - q) / p; ind = 0; continue; } if (X != 1.0 * (w - q) / p){ cout<<"NO"<<"\n"; return 0; } } if (ind){ vector<int> vv; for (int i: all[i]){ vv.pb((a[i] == -1) ? b[i] : -b[i]); } sort(vv.begin(), vv.end()); X = vv[vv.size() / 2]; } for (int i: all[i]){ out[i] = a[i] * X + b[i]; } } cout<<"YES"<<"\n"; for (int i = 1; i <= n; i++){ cout<<out[i]<<" "; } cout<<"\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...