Submission #1017766

#TimeUsernameProblemLanguageResultExecution timeMemory
1017766VMaksimoski008Graph (BOI20_graph)C++17
100 / 100
447 ms51012 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1e9 + 7; const int LOG = 20; const int maxn = 1e5 + 5; int n, m; vector<pii> graph[maxn+5]; vector<int> val(maxn+5), coef(maxn+5), vis(maxn+5); vector<int> bad(maxn+5); vector<double> ans(maxn+5); vector<vector<int> > C; map<pii, int> mp; vector<array<int, 3> > E; void dfs(int u, int p) { C.back().push_back(u); vis[u] = 1; for(auto &[v, w] : graph[u]) { if(v == p) continue; E.push_back({ u, v, w }); if(vis[v]) continue; if(coef[u] == 1) coef[v] = -1; else coef[v] = 1; val[v] = w - val[u]; dfs(v, u); } } //da vidam dali e do NO signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; vector<array<int, 3> > edges; for(int i=0; i<m; i++) { int a, b, c; cin >> a >> b >> c; if(a > b) swap(a, b); if(a == b) bad[a] = c; if(mp[{a, b}] & (1 << (c - 1))) continue; mp[{a, b}] |= (1 << (c - 1)); if(mp[{a, b}] == 3) { cout << "NO\n"; return 0; } graph[a].push_back({ b, c }); graph[b].push_back({ a, c }); edges.push_back({ a, b, c }); } for(int i=1; i<=n; i++) { if(vis[i]) continue; C.push_back({ }); E.clear(); coef[i] = 1; dfs(i, i); // for(auto &x : C.back()) cout << "(" << coef[x] << " , " << val[x] << ") "; // cout << '\n'; set<double> s; for(auto &[a, b, c] : E) { ll sC = coef[a] + coef[b]; ll sV = val[a] + val[b]; if(sC == 0) { if(sV != c) { cout << "NO\n"; return 0; } continue; } double res = double(c - sV) / (double)sC; s.insert(res); } if(s.size() > 1) { cout << "NO\n"; return 0; } int cnt = 0; for(auto &x : C.back()) if(bad[x]) cnt = x; if(s.size() == 1) { double res = *s.begin(); for(int &x : C.back()) { if(bad[x]) { if(2 * coef[x] * res + 2 * val[x] != bad[x]) { cout << "NO\n"; return 0; } } } for(auto &x : C.back()) ans[x] = coef[x] * res + val[x]; for(auto &[a, b, c] : E) { if(ans[a] + ans[b] != c) { cout << "NO\n"; return 0; } } } else { if(cnt) { double V = (double)bad[cnt] / 2; double res = (V - val[cnt]) / coef[cnt]; for(auto &[a, b, c] : E) { if(coef[a] * res + val[a] + coef[b] * res + val[b] != c) { cout << "NO\n"; return 0; } } for(auto &x : C.back()) { ans[x] = coef[x] * res + val[x]; } } else { double V = -1e9, total = 1e18; for(double i=-100; i<=100; i+=0.5) { double total2 = 0; for(int &x : C.back()) total2 += abs(coef[x] * i + val[x]); bool ok = 1; for(auto &[a, b, c] : E) { if(coef[a] * i + val[a] + coef[b] * i + val[b] != c) { ok = 0; break; } } for(auto &x : C.back()) { if(bad[x]) { if(2 * coef[x] * i + 2 * val[x] != bad[x]) { cout << "NO\n"; return 0; } } } // cout << i << ": " << total2 << " " << total << " " << V << '\n'; if(ok && total2 < total) { V = i; total = total2; } } if(V == -1e9) { cout << "NO\n"; return 0; } for(int &x : C.back()) ans[x] = coef[x] * V + val[x]; } } } // for(int i=1; i<=3; i++) cout << coef[i] << " " << val[i] << '\n'; cout << "YES\n"; for(int i=1; i<=n; i++) cout << ans[i] << " "; return 0; }
#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...