답안 #642684

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
642684 2022-09-20T11:12:16 Z MKutayBozkurt Graph (BOI20_graph) C++17
0 / 100
1 ms 212 KB
/**
*  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);
    edges[i] = make_pair(make_pair(x, y), z);
  }
  vector<double> ans(n);
  vector<int> components_vis(n);

  auto solve = [&](int node) {
    vector<pair<int, int>> n_vals(n); // (first * x + second) - node values
    vector<int> cycle_vis(n);
    bool cycle = false;
    function<void(int, int, int)> find_cycle = [&](int node, int parent, int color) {
      debug(node, parent, color);
      if (cycle_vis[node]) {
        cycle = true;
        return;
      }
      cycle_vis[node] = 1;
      components_vis[node] = 1;
      for (auto next : g[node]) {
        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(n_vals);
    double x;
    bool flag = false;
    for (auto [p, color] : edges) {
      debug(p);
      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));
      debug(a, b, should, sum);
      if (sum.first == 0) continue;
      x = (double) sum.second / sum.first;
      flag = true;
    }
    debug(x, flag);
    if (flag == false) {
      x = 0;
      debug(x);
      for (int i = 0; i < n; i++) {
        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;
      debug(a, b, sum);
      if (abs(sum - color) > epsilon) {
        cout << "NO\n";
        exit(0);
      }
    }
    for (int i = 0; i < n; i++) {
      ans[i] = n_vals[i].first * x + n_vals[i].second;
    }
    debug(cycle, n_vals);
  };

  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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Incorrect 0 ms 212 KB Sum of endpoints for edge (1; 2) differs from the expected value 1.
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Incorrect 0 ms 212 KB Sum of endpoints for edge (1; 2) differs from the expected value 1.
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Incorrect 0 ms 212 KB Sum of endpoints for edge (1; 2) differs from the expected value 1.
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Incorrect 0 ms 212 KB Sum of endpoints for edge (1; 2) differs from the expected value 1.
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Incorrect 0 ms 212 KB Sum of endpoints for edge (1; 2) differs from the expected value 1.
6 Halted 0 ms 0 KB -