#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
ll best = 1e18; int who = -1;
for (int i = -2*sz(comp); i <= 2*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 |
1 ms |
348 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Correct |
1 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
344 KB |
answer = YES |
9 |
Correct |
1 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
1 ms |
344 KB |
answer = YES |
16 |
Correct |
0 ms |
344 KB |
answer = YES |
17 |
Correct |
1 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
1 ms |
348 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Correct |
1 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
344 KB |
answer = YES |
9 |
Correct |
1 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
1 ms |
344 KB |
answer = YES |
16 |
Correct |
0 ms |
344 KB |
answer = YES |
17 |
Correct |
1 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
1 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
1 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
356 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
1 ms |
348 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Correct |
1 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
344 KB |
answer = YES |
9 |
Correct |
1 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
1 ms |
344 KB |
answer = YES |
16 |
Correct |
0 ms |
344 KB |
answer = YES |
17 |
Correct |
1 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
1 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
1 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
356 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
37 |
Correct |
1 ms |
344 KB |
answer = YES |
38 |
Correct |
1 ms |
348 KB |
answer = YES |
39 |
Correct |
3 ms |
348 KB |
answer = YES |
40 |
Correct |
1 ms |
600 KB |
answer = YES |
41 |
Correct |
1 ms |
604 KB |
answer = NO |
42 |
Correct |
6 ms |
604 KB |
answer = YES |
43 |
Correct |
2 ms |
348 KB |
answer = YES |
44 |
Correct |
2 ms |
348 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
2 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
604 KB |
answer = YES |
48 |
Correct |
1 ms |
604 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
1 ms |
348 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Correct |
1 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
344 KB |
answer = YES |
9 |
Correct |
1 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
1 ms |
344 KB |
answer = YES |
16 |
Correct |
0 ms |
344 KB |
answer = YES |
17 |
Correct |
1 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
1 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
1 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
356 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
37 |
Correct |
1 ms |
344 KB |
answer = YES |
38 |
Correct |
1 ms |
348 KB |
answer = YES |
39 |
Correct |
3 ms |
348 KB |
answer = YES |
40 |
Correct |
1 ms |
600 KB |
answer = YES |
41 |
Correct |
1 ms |
604 KB |
answer = NO |
42 |
Correct |
6 ms |
604 KB |
answer = YES |
43 |
Correct |
2 ms |
348 KB |
answer = YES |
44 |
Correct |
2 ms |
348 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
2 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
604 KB |
answer = YES |
48 |
Correct |
1 ms |
604 KB |
answer = YES |
49 |
Correct |
619 ms |
1736 KB |
answer = YES |
50 |
Correct |
8 ms |
2384 KB |
answer = YES |
51 |
Correct |
658 ms |
2384 KB |
answer = YES |
52 |
Correct |
4 ms |
2384 KB |
answer = NO |
53 |
Correct |
7 ms |
348 KB |
answer = YES |
54 |
Correct |
29 ms |
816 KB |
answer = YES |
55 |
Correct |
140 ms |
1116 KB |
answer = YES |
56 |
Correct |
577 ms |
1876 KB |
answer = YES |
57 |
Correct |
89 ms |
1460 KB |
answer = YES |
58 |
Correct |
194 ms |
1628 KB |
answer = YES |
59 |
Correct |
6 ms |
1608 KB |
answer = YES |
60 |
Correct |
293 ms |
1768 KB |
answer = YES |
61 |
Correct |
3 ms |
1112 KB |
answer = YES |
62 |
Correct |
71 ms |
19080 KB |
answer = NO |
63 |
Correct |
52 ms |
17752 KB |
answer = YES |
64 |
Correct |
45 ms |
18212 KB |
answer = NO |
65 |
Correct |
51 ms |
17848 KB |
answer = YES |
66 |
Correct |
11 ms |
604 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
1 ms |
348 KB |
answer = YES |
4 |
Correct |
1 ms |
348 KB |
answer = NO |
5 |
Correct |
1 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
1 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
344 KB |
answer = YES |
9 |
Correct |
1 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
1 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
1 ms |
344 KB |
answer = YES |
16 |
Correct |
0 ms |
344 KB |
answer = YES |
17 |
Correct |
1 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
1 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
1 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
1 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
356 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
37 |
Correct |
1 ms |
344 KB |
answer = YES |
38 |
Correct |
1 ms |
348 KB |
answer = YES |
39 |
Correct |
3 ms |
348 KB |
answer = YES |
40 |
Correct |
1 ms |
600 KB |
answer = YES |
41 |
Correct |
1 ms |
604 KB |
answer = NO |
42 |
Correct |
6 ms |
604 KB |
answer = YES |
43 |
Correct |
2 ms |
348 KB |
answer = YES |
44 |
Correct |
2 ms |
348 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
2 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
604 KB |
answer = YES |
48 |
Correct |
1 ms |
604 KB |
answer = YES |
49 |
Correct |
619 ms |
1736 KB |
answer = YES |
50 |
Correct |
8 ms |
2384 KB |
answer = YES |
51 |
Correct |
658 ms |
2384 KB |
answer = YES |
52 |
Correct |
4 ms |
2384 KB |
answer = NO |
53 |
Correct |
7 ms |
348 KB |
answer = YES |
54 |
Correct |
29 ms |
816 KB |
answer = YES |
55 |
Correct |
140 ms |
1116 KB |
answer = YES |
56 |
Correct |
577 ms |
1876 KB |
answer = YES |
57 |
Correct |
89 ms |
1460 KB |
answer = YES |
58 |
Correct |
194 ms |
1628 KB |
answer = YES |
59 |
Correct |
6 ms |
1608 KB |
answer = YES |
60 |
Correct |
293 ms |
1768 KB |
answer = YES |
61 |
Correct |
3 ms |
1112 KB |
answer = YES |
62 |
Correct |
71 ms |
19080 KB |
answer = NO |
63 |
Correct |
52 ms |
17752 KB |
answer = YES |
64 |
Correct |
45 ms |
18212 KB |
answer = NO |
65 |
Correct |
51 ms |
17848 KB |
answer = YES |
66 |
Correct |
11 ms |
604 KB |
answer = YES |
67 |
Execution timed out |
1099 ms |
25856 KB |
Time limit exceeded |
68 |
Halted |
0 ms |
0 KB |
- |