답안 #1024632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1024632 2024-07-16T08:46:45 Z NeroZein Graph (BOI20_graph) C++17
0 / 100
1 ms 2652 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long

#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif

const int N = 1e5 + 5;
const int INF = 1e9 + 9;

int val[N];
bool vis[N];
bool vis2[N];
bool color[N];
stack<int> stk;
vector<int> component;
vector<int> odd_cycle;
vector<pair<int, int>> g[N];

bool check_not_bipartite(int v, int c) {
  if (vis[v]) {
    if (color[v] != c && odd_cycle.empty()) {
      odd_cycle.push_back(v);
      while (true) {
        int x = stk.top();
        stk.pop();
        odd_cycle.push_back(x); 
        if (x == v) {
          break; 
        }
      }
      return true;
    }
    return false; 
  }
  vis[v] = true; 
  stk.push(v);
  color[v] = c; 
  component.push_back(v); 
  bool ret = false; 
  for (auto [u, w] : g[v]) {
    ret |= check_not_bipartite(u, c ^ 1); 
  }
  if (!stk.empty() && stk.top() == v) {
    stk.pop();    
  }
  return ret; 
}

int assign(int v, int c) {
  if (vis2[v]) {
    if (val[v] != c) {
      return INF;
    }
    return 0;
  }
  vis2[v] = true;
  val[v] = c;
  int ret = abs(c); 
  for (auto [u, w] : g[v]) {
    ret += assign(u, w - c);
    ret = min(ret, INF);
  }
  return ret; 
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    w *= 2;
    if (u > v) swap(u, v);
    if (!mp.count({u, v})) {
      mp[{u, v}] = w;
      g[u].push_back({v, w});
      g[v].push_back({u, w});
    }
    if (mp[{u, v}] != w) {
      cout << "NO" << '\n';
      return 0; 
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[i]) {
      continue; 
    }
    while (!stk.empty()) stk.pop();
    bool not_bipartite = check_not_bipartite(i, 0); 
    if (not_bipartite) {
      int sum = 0;
      for (int j = 1; j < (int) odd_cycle.size(); ++j) {
        int u = odd_cycle[j - 1], v = odd_cycle[j];
        if (u > v) swap(u, v);
        sum += mp[{u, v}] * (j % 2 ? 1 : -1);
      }
      int rt = odd_cycle[0];
      int x = assign(rt, sum / 2);
      if (x == INF) {
        cout << "NO" << '\n';
        return 0; 
      }
      odd_cycle.clear();
    } else {
      int l = -1e5, r = 1e5;
      while (r - l > 100) {
        int mid = (l + r) / 2;
        int tmp = assign(i, mid);
        int tmp2 = assign(i, mid + 1);
        if (tmp < tmp2) {
          r = mid;
        } else {
          l = mid;
        }
      }
      int mn = INF, uv = -1;
      for (int tval = l; tval <= r; tval++) {
        int tmp = assign(i, tval);
        if (tmp < mn) {
          mn = tmp;
          uv = tval;
        }
        for (int v : component) {
          vis2[v] = 0;
        }
      }
      if (uv == -1) {
        cout << "NO" << '\n';
        return 0; 
      }
      assign(i, uv); 
    }
    component.clear();
  }
  for (auto [e, w] : mp) {
    if (val[e.first] + val[e.second] != w) {
      cout << "NO" << '\n';
      return 0; 
    }
  }
  cout << "YES" << '\n';
  for (int i = 1; i <= n; ++i) {
    cout << (val[i] / 2.0) << ' ';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 1 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Incorrect 1 ms 2652 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 1 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Incorrect 1 ms 2652 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 1 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Incorrect 1 ms 2652 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 1 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Incorrect 1 ms 2652 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 1 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Incorrect 1 ms 2652 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -