Submission #652537

#TimeUsernameProblemLanguageResultExecution timeMemory
6525371zaid1Graph (BOI20_graph)C++17
100 / 100
610 ms58768 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n'; const int M = 1e5+5, MOD = 1e9+7; int p[M], d[M], loop, vis[M], f[M]; vector<pair<int, int>> node[M]; bitset<100005> is; vector<int> tmp; double cost[M]; void dfs(int s, int dp = 0) { vis[s] = 1; d[s] = dp++; tmp.push_back(s); for (auto [i, x]:node[s]) { if (!vis[i]) { p[i] = s; dfs(i, dp); } else if (!loop && (dp-d[i])%2) { loop = s; p[i] = s; } } } double eval(int s, double a, int cnt = 0) { double ans = abs(a); cost[s] = a; is[s] = 1; for (auto [i, x]:node[s]) { if (!is[i]) ans += abs(eval(i, x-a)); } return ans; } bool check(vector<int> v) { for (int y:v) { for (auto [i, x]:node[y]) { if (abs(cost[y] + cost[i]-x) >= 1e-6) return 0; } } return true; } void dfs2(int s, int d = 0) { is[s] = true; for (auto [i, x]:node[s]) { if (!is[i]) { f[i] = f[s]+(d?x:-x); dfs2(i, !d); } } } signed main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; map<pair<int, int>, int> ed; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; if (a > b) swap(a, b); if (ed[{a, b}] && ed[{a, b}] != c) { cout << "NO" << endl; return 0; } else if (ed[{a, b}]) continue; ed[{a, b}] = ed[{b, a}] = c; node[a].push_back({b, c}); node[b].push_back({a, c}); } for (int k = 1; k <= n; k++) { if (!vis[k] && node[k].size()) { dfs(k); if (loop) { vector<int> lp = {loop}; for (int x = p[loop]; x != loop; x = p[x]) lp.push_back(x); lp.push_back(loop); double sum = 0; for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) { if (f) sum += ed[{min(lp[i], lp[i+1]), max(lp[i], lp[i+1])}]; else sum -= ed[{min(lp[i], lp[i+1]), max(lp[i], lp[i+1])}]; } eval(loop, sum/2); } else { eval(k, 0); if (!check(tmp)) { cout << "NO" << endl; return 0; } is = 0; dfs2(k); map<int, int> fr, ir; for (int i:tmp) fr[f[i]]++; for (int i:tmp) ir[f[i]] = i; int sum = 0; for (auto [a, b]:fr) sum += a*b; vector<int> nor; for (int i:tmp) nor.push_back(-f[i]); sort(nor.begin(), nor.end()); double ans; if (nor.size()%2) ans = nor[nor.size()/2]; else ans = (nor[nor.size()/2] + nor[nor.size()/2-1])/2; is = 0; eval(k, ans); } if (!check(tmp)) { cout << "NO" << endl; return 0; } tmp = {}; loop = 0; } } cout << "YES" << fixed << setprecision(6) << endl; for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl; return 0; } /* 3 3 1 1 2 2 2 1 2 3 2 4 5 1 4 2 3 2 1 2 1 2 3 4 1 1 3 2 3 2 2 1 2 3 2 1 4 4 1 2 1 2 3 2 1 3 2 3 4 1 */

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:86:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |                 for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) {
      |                                        ~~^~~~~~~~~~~~~
Graph.cpp:128:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  128 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |     ^~~
Graph.cpp:128:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  128 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |                                                          ^~~~
#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...