This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void test_case() {
int n, m;
cin >> n >> m;
vector<vector<ar<int, 2>>> adj(n+1);
vector<ar<int, 3>> E;
for (int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
E.push_back({x, y, w});
adj[x].push_back({y, 2*w});
adj[y].push_back({x, 2*w});
}
vector<int> ans(n+1);
vector<bool> vis2(n+1);
vector<int> depth(n+1), val(n+1);
for (int v = 1; v <= n; v++) if (!vis2[v]) {
ll tot_sum = 0;
vector<ar<int, 3>> edges;
vector<int> comp;
auto dfs = [&](auto&& s, int v, int cur) -> void {
vis2[v] = 1;
comp.emplace_back(v);
val[v] = cur;
tot_sum += cur;
for (auto [u, w] : adj[v]) {
edges.push_back({v, u, w});
if (!vis2[u]) {
depth[u] = depth[v] + 1;
s(s, u, w - cur);
}
}
};
dfs(dfs, v, 0);
// val[x] + val[y] +/- 2x = 2*w
// tot_sum + totx = final val
int need; bool f = 1;
for (auto [x, y, w] : edges) {
if ((depth[x] + depth[y]) & 1) { // they must have the same val
if (val[x] + val[y] != w) {
cout << "NO\n"; return;
}
}
else {
// val[x] + val[y] +/- 2x = w
int other = (w - val[x] - val[y]) * (depth[x] & 1 ? -1 : 1);
assert(other % 2 == 0);
other /= 2;
if (f) {
need = other, f = 0;
}
else {
if (need != other) {
cout << "NO\n"; return;
}
}
}
}
if (f) {
// choose x that minimizes tot_sum + tot*x
int N = sz(comp);
vector<ll> each(4 * N + 1), each2(4 * N + 1);
auto add = [&](int L, int R, int X, int del) { // a[L...R] += x + del
// X + 0
// X + del
// X + 2*del
// X + ...
each[L+2*N] += X;
if (L+2*N+1 <= 4*N) {
each2[L+2*N+1] += del;
}
if (R+1 <= 2*N) {
each[R+2*N+1] -= X;
each2[R+2*N+1] -= del;
each[R+2*N+1] -= (R - L) * del;
}
/*
for (int i = L + 2*N; i <= R + 2*N; i++) {
each[i] += X + (i - (L + 2*N)) * del;
}
*/
};
for (int u : comp) {
if (depth[u] & 1) {
add(-2*N, val[u], val[u] + 2*N, -1);
if (val[u] + 1 <= 2*N) add(val[u] + 1, 2*N, 1, 1);
}
else {
add(-2*N, -val[u], -val[u] + 2*N, -1);
if (-val[u] + 1 <= 2*N) add(-val[u] + 1, 2*N, 1, 1);
}
}
for (int i = 1; i <= 4*N; i++) each[i] += each[i-1], each2[i] += each2[i-1];
for (int i = 1; i <= 4*N; i++) each2[i] += each2[i-1];
for (int i = 0; i <= 4*N; i++) {
each[i] += each2[i];
}
int who = (min_element(each.begin(), each.end()) - each.begin()) - 2 * N;
for (int u : comp) {
ans[u] = val[u] + who * (depth[u] & 1 ? -1 : 1);
}
}
else {
// x is set
for (int u : comp) {
ans[u] = val[u] + need * (depth[u] & 1 ? -1 : 1);
}
}
}
cout << "YES\n";
cout << fixed << setprecision(1);
for (int i = 1; i <= n; i++) cout << (db)ans[i] / 2 << " \n"[i==n];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |