#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);
for (int i = 0; i < m; i++) {
int x, y, w;
cin >> 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, 1, 0);
// 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];
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
ll best = 1e18; int who = -1;
for (int i = -sz(comp); i <= sz(comp); i++) {
ll sum = 0;
for (int u : comp) {
sum += abs(val[u] + i * (depth[u] & 1 ? -1 : 1));
}
if (ckmin(best, sum)) who = i;
}
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
344 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Incorrect |
0 ms |
460 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
344 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Incorrect |
0 ms |
460 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
344 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Incorrect |
0 ms |
460 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
344 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Incorrect |
0 ms |
460 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
344 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Incorrect |
0 ms |
460 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |