Submission #642708

#TimeUsernameProblemLanguageResultExecution timeMemory
642708MKutayBozkurtGraph (BOI20_graph)C++17
5 / 100
1 ms340 KiB
/** * author: kututay * created: 20.09.2022 13:13:02 **/ #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "/Users/kutay/CP/templates/debug.h" #else #define debug(...) void(38) #endif const double epsilon = 1e-6; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<pair<int, int>>> g(n); vector<pair<pair<int, int>, int>> edges(m); for (int i = 0; i < m; i++) { int x, y, z; cin >> x >> y >> z, x--, y--; g[x].emplace_back(y, z); g[y].emplace_back(x, z); } vector<double> ans(n); vector<int> components_vis(n); auto solve = [&](int node) { vector<pair<int, int>> n_vals(n, make_pair(0, 0)); // (first * x + second) - node values vector<int> cycle_vis(n); bool cycle = false; vector<pair<pair<int, int>, int>> edges; set<int> nodes; function<void(int, int, int)> find_cycle = [&](int node, int parent, int color) { nodes.insert(node); if (cycle_vis[node]) { cycle = true; return; } cycle_vis[node] = 1; components_vis[node] = 1; for (auto next : g[node]) { edges.emplace_back(make_pair(node, next.first), next.second); if (parent == -1) n_vals[node] = make_pair(1, 0); else n_vals[node] = make_pair(-n_vals[parent].first, color - n_vals[parent].second); if (next.first != parent) find_cycle(next.first, node, next.second); } }; find_cycle(node, -1, -1); debug(edges, n_vals); double x; if ((int) edges.size() == 0) return; bool flag = false; for (auto [p, color] : edges) { auto a = n_vals[p.first]; auto b = n_vals[p.second]; pair<int, int> should = make_pair(-a.first, color - a.second); if (b == should) continue; pair<int, int> sum = make_pair(b.first + a.first, color - (b.second + a.second)); if (sum.first == 0) { cout << "NO\n"; exit(0); } x = (double) sum.second / sum.first; flag = true; } debug(flag, cycle, x); if (flag == false or cycle == false) { double s1 = 0, s2 = 0; for (int i = 0; i < n; i++) { s1 += abs(n_vals[i].first * 1 + n_vals[i].second); s2 += abs(n_vals[i].first * 0 + n_vals[i].second); } if (s1 < s2) { x = 1; } else { x = 0; } for (int i : nodes) { ans[i] = n_vals[i].first * x + n_vals[i].second; } return; } for (auto [p, color] : edges) { auto a = n_vals[p.first]; auto b = n_vals[p.second]; double sum = a.first * x + a.second + b.first * x + b.second; if (abs(sum - color) > epsilon) { cout << "NO\n"; exit(0); } } for (int i : nodes) { ans[i] = n_vals[i].first * x + n_vals[i].second; } debug(ans); }; for (int i = 0; i < n; i++) { if (components_vis[i]) continue; solve(i); } cout << "YES\n"; for (auto x : ans) cout << x << " "; 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...