Submission #745424

#TimeUsernameProblemLanguageResultExecution timeMemory
745424MilosMilutinovicGraph (BOI20_graph)C++14
100 / 100
144 ms23124 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, m, coeff[N], delta[N]; double ans[N], val; vector<pair<int, int>> g[N]; vector<int> comp; bool ok = true, found, was[N]; void dfs(int x) { was[x] = true; comp.push_back(x); for (auto& p : g[x]) { int y = p.first; int w = p.second; if (was[y]) { if (-coeff[y] == coeff[x]) { ok = (ok & ((w - delta[y]) == delta[x])); } else { found = true; val = (w - delta[x] - delta[y]) * 1.0 / (coeff[y] + coeff[x]); } continue; } coeff[y] = -coeff[x]; delta[y] = w - delta[x]; dfs(y); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); g[a].emplace_back(b, c); g[b].emplace_back(a, c); } for (int i = 1; i <= n; i++) if (!was[i]) { coeff[i] = 1; delta[i] = 0; comp.clear(); found = false; dfs(i); if (!ok) break; if (found) { for (int x : comp) ans[x] = val * coeff[x] + delta[x]; } else { vector<int> f; for (int x : comp) f.push_back(delta[x] * coeff[x]); sort(f.begin(), f.end()); val = -f[(int) f.size() / 2]; for (int x : comp) ans[x] = val * coeff[x] + delta[x]; } } for (int i = 1; i <= n; i++) { for (auto& p : g[i]) { if (ans[i] + ans[p.first] != p.second * 1.0) ok = false; } } if (!ok) { printf("NO"); return 0; } printf("YES\n"); for (int i = 1; i <= n; i++) printf("%0.5lf ", ans[i]); return 0; }

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Graph.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         scanf("%d%d%d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...