#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
struct A {
int v, w;
};
vector<A> adj[N];
double ans[N];
bool ch[N], vis[N];
int col[N], bp = 0;
mt19937 rnd(time(NULL));
vector<pair<int, int>> cycle;
void dfs(int v, vector<int>& node) {
node.push_back(v);
vis[v] = 1;
for (auto& x : adj[v]) {
if (!vis[x.v]) dfs(x.v, node);
}
}
void dfs_ans(int v) {
for (auto& x : adj[v]) {
if (!ch[x.v]) {
ch[x.v] = 1;
ans[x.v] = x.w - ans[v];
dfs_ans(x.v);
}
}
}
bool bipartite(int v, int c) {
if (col[v]) {
if (col[v] != c) return bp = v, false;
else return true;
}
col[v] = c;
for (auto& x : adj[v]) {
if (!bipartite(x.v, 3 - c)) {
if (bp != 0) cycle.push_back({ v, x.w });
if (v == bp) bp = 0;
return false;
}
}
return true;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({ v, w });
adj[v].push_back({ u, w });
}
for (int i = 1;i <= n;i++) {
if (vis[i]) continue;
vector<int> node;
dfs(i, node);
bool bipar = bipartite(i, 1);
if (!bipar) {
vector<pair<int, int>> c = cycle;
cycle.clear();
int root = c.back().first;
int val = 0;
for (int i = 0;i < (int)c.size();i++) {
if (i & 1) val -= c[i].second;
else val += c[i].second;
}
// value at node (root) = val / 2
ch[root] = 1;
ans[root] = (double)val / 2;
dfs_ans(root);
continue;
}
int sz = node.size();
if (sz == 1) {
continue;
}
sz = min(sz * 2, 50);
int now = node[rnd() % (node.size())];
double mn = 1e9;
int idx = -100;
for (int V = -sz;V <= sz;V++) {
ch[now] = 1;
ans[now] = (double)V;
dfs_ans(now);
double sum = 0;
bool can = 1;
for (auto& i : node) {
ch[i] = 0;
sum += abs(ans[i]);
for (auto& x : adj[i]) {
if (ans[i] + ans[x.v] != x.w) {
can = 0;
break;
}
}
if (!can) break;
}
if (can) {
if (sum < mn) {
mn = sum;
idx = V;
}
}
}
if (mn == 1e9) {
cout << "NO";
return 0;
}
ch[now] = 1;
ans[now] = (double)idx;
dfs_ans(now);
}
for (int i = 1;i <= n;i++) {
for (auto& x : adj[i]) {
if (ans[i] + ans[x.v] != x.w) {
cout << "NO";
return 0;
}
}
}
cout << "YES\n";
for (int i = 1;i <= n;i++) {
cout << ans[i] << ' ';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
3932 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
2 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
3932 KB |
answer = YES |
8 |
Correct |
1 ms |
4016 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
3932 KB |
answer = YES |
11 |
Correct |
1 ms |
3932 KB |
answer = YES |
12 |
Correct |
2 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2652 KB |
answer = YES |
14 |
Correct |
2 ms |
2648 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Correct |
1 ms |
2652 KB |
answer = YES |
20 |
Correct |
1 ms |
2652 KB |
answer = YES |
21 |
Correct |
1 ms |
2652 KB |
answer = YES |
22 |
Correct |
1 ms |
2820 KB |
answer = NO |
23 |
Correct |
1 ms |
3932 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
3932 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
2 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
3932 KB |
answer = YES |
8 |
Correct |
1 ms |
4016 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
3932 KB |
answer = YES |
11 |
Correct |
1 ms |
3932 KB |
answer = YES |
12 |
Correct |
2 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2652 KB |
answer = YES |
14 |
Correct |
2 ms |
2648 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Correct |
1 ms |
2652 KB |
answer = YES |
20 |
Correct |
1 ms |
2652 KB |
answer = YES |
21 |
Correct |
1 ms |
2652 KB |
answer = YES |
22 |
Correct |
1 ms |
2820 KB |
answer = NO |
23 |
Correct |
1 ms |
3932 KB |
answer = NO |
24 |
Correct |
2 ms |
2648 KB |
answer = YES |
25 |
Correct |
1 ms |
2648 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
2 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2816 KB |
answer = YES |
29 |
Correct |
1 ms |
2652 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2608 KB |
answer = YES |
32 |
Correct |
2 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2652 KB |
answer = YES |
34 |
Correct |
2 ms |
2652 KB |
answer = YES |
35 |
Correct |
1 ms |
2652 KB |
answer = YES |
36 |
Correct |
1 ms |
2652 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
3932 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
2 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
3932 KB |
answer = YES |
8 |
Correct |
1 ms |
4016 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
3932 KB |
answer = YES |
11 |
Correct |
1 ms |
3932 KB |
answer = YES |
12 |
Correct |
2 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2652 KB |
answer = YES |
14 |
Correct |
2 ms |
2648 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Correct |
1 ms |
2652 KB |
answer = YES |
20 |
Correct |
1 ms |
2652 KB |
answer = YES |
21 |
Correct |
1 ms |
2652 KB |
answer = YES |
22 |
Correct |
1 ms |
2820 KB |
answer = NO |
23 |
Correct |
1 ms |
3932 KB |
answer = NO |
24 |
Correct |
2 ms |
2648 KB |
answer = YES |
25 |
Correct |
1 ms |
2648 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
2 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2816 KB |
answer = YES |
29 |
Correct |
1 ms |
2652 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2608 KB |
answer = YES |
32 |
Correct |
2 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2652 KB |
answer = YES |
34 |
Correct |
2 ms |
2652 KB |
answer = YES |
35 |
Correct |
1 ms |
2652 KB |
answer = YES |
36 |
Correct |
1 ms |
2652 KB |
answer = YES |
37 |
Correct |
1 ms |
2648 KB |
answer = YES |
38 |
Correct |
1 ms |
2652 KB |
answer = YES |
39 |
Correct |
2 ms |
2648 KB |
answer = YES |
40 |
Correct |
2 ms |
2908 KB |
answer = YES |
41 |
Correct |
1 ms |
2652 KB |
answer = NO |
42 |
Correct |
3 ms |
2908 KB |
answer = YES |
43 |
Correct |
3 ms |
2652 KB |
answer = YES |
44 |
Correct |
2 ms |
3932 KB |
answer = YES |
45 |
Correct |
2 ms |
2648 KB |
answer = YES |
46 |
Correct |
2 ms |
2820 KB |
answer = YES |
47 |
Correct |
2 ms |
2908 KB |
answer = YES |
48 |
Correct |
2 ms |
3160 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
3932 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
2 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
3932 KB |
answer = YES |
8 |
Correct |
1 ms |
4016 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
3932 KB |
answer = YES |
11 |
Correct |
1 ms |
3932 KB |
answer = YES |
12 |
Correct |
2 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2652 KB |
answer = YES |
14 |
Correct |
2 ms |
2648 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Correct |
1 ms |
2652 KB |
answer = YES |
20 |
Correct |
1 ms |
2652 KB |
answer = YES |
21 |
Correct |
1 ms |
2652 KB |
answer = YES |
22 |
Correct |
1 ms |
2820 KB |
answer = NO |
23 |
Correct |
1 ms |
3932 KB |
answer = NO |
24 |
Correct |
2 ms |
2648 KB |
answer = YES |
25 |
Correct |
1 ms |
2648 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
2 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2816 KB |
answer = YES |
29 |
Correct |
1 ms |
2652 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2608 KB |
answer = YES |
32 |
Correct |
2 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2652 KB |
answer = YES |
34 |
Correct |
2 ms |
2652 KB |
answer = YES |
35 |
Correct |
1 ms |
2652 KB |
answer = YES |
36 |
Correct |
1 ms |
2652 KB |
answer = YES |
37 |
Correct |
1 ms |
2648 KB |
answer = YES |
38 |
Correct |
1 ms |
2652 KB |
answer = YES |
39 |
Correct |
2 ms |
2648 KB |
answer = YES |
40 |
Correct |
2 ms |
2908 KB |
answer = YES |
41 |
Correct |
1 ms |
2652 KB |
answer = NO |
42 |
Correct |
3 ms |
2908 KB |
answer = YES |
43 |
Correct |
3 ms |
2652 KB |
answer = YES |
44 |
Correct |
2 ms |
3932 KB |
answer = YES |
45 |
Correct |
2 ms |
2648 KB |
answer = YES |
46 |
Correct |
2 ms |
2820 KB |
answer = YES |
47 |
Correct |
2 ms |
2908 KB |
answer = YES |
48 |
Correct |
2 ms |
3160 KB |
answer = YES |
49 |
Correct |
33 ms |
3552 KB |
answer = YES |
50 |
Correct |
7 ms |
3672 KB |
answer = YES |
51 |
Correct |
23 ms |
3856 KB |
answer = YES |
52 |
Correct |
4 ms |
4696 KB |
answer = NO |
53 |
Correct |
3 ms |
3932 KB |
answer = YES |
54 |
Correct |
7 ms |
3164 KB |
answer = YES |
55 |
Correct |
14 ms |
3204 KB |
answer = YES |
56 |
Correct |
34 ms |
3548 KB |
answer = YES |
57 |
Correct |
16 ms |
4440 KB |
answer = YES |
58 |
Correct |
19 ms |
4564 KB |
answer = YES |
59 |
Correct |
5 ms |
3416 KB |
answer = YES |
60 |
Correct |
28 ms |
3420 KB |
answer = YES |
61 |
Correct |
4 ms |
3160 KB |
answer = YES |
62 |
Correct |
36 ms |
10100 KB |
answer = NO |
63 |
Correct |
40 ms |
9988 KB |
answer = YES |
64 |
Correct |
41 ms |
11092 KB |
answer = NO |
65 |
Correct |
39 ms |
11200 KB |
answer = YES |
66 |
Correct |
4 ms |
3932 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
3932 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
2 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
3932 KB |
answer = YES |
8 |
Correct |
1 ms |
4016 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
3932 KB |
answer = YES |
11 |
Correct |
1 ms |
3932 KB |
answer = YES |
12 |
Correct |
2 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2652 KB |
answer = YES |
14 |
Correct |
2 ms |
2648 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Correct |
1 ms |
2652 KB |
answer = YES |
20 |
Correct |
1 ms |
2652 KB |
answer = YES |
21 |
Correct |
1 ms |
2652 KB |
answer = YES |
22 |
Correct |
1 ms |
2820 KB |
answer = NO |
23 |
Correct |
1 ms |
3932 KB |
answer = NO |
24 |
Correct |
2 ms |
2648 KB |
answer = YES |
25 |
Correct |
1 ms |
2648 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
2 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2816 KB |
answer = YES |
29 |
Correct |
1 ms |
2652 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2608 KB |
answer = YES |
32 |
Correct |
2 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2652 KB |
answer = YES |
34 |
Correct |
2 ms |
2652 KB |
answer = YES |
35 |
Correct |
1 ms |
2652 KB |
answer = YES |
36 |
Correct |
1 ms |
2652 KB |
answer = YES |
37 |
Correct |
1 ms |
2648 KB |
answer = YES |
38 |
Correct |
1 ms |
2652 KB |
answer = YES |
39 |
Correct |
2 ms |
2648 KB |
answer = YES |
40 |
Correct |
2 ms |
2908 KB |
answer = YES |
41 |
Correct |
1 ms |
2652 KB |
answer = NO |
42 |
Correct |
3 ms |
2908 KB |
answer = YES |
43 |
Correct |
3 ms |
2652 KB |
answer = YES |
44 |
Correct |
2 ms |
3932 KB |
answer = YES |
45 |
Correct |
2 ms |
2648 KB |
answer = YES |
46 |
Correct |
2 ms |
2820 KB |
answer = YES |
47 |
Correct |
2 ms |
2908 KB |
answer = YES |
48 |
Correct |
2 ms |
3160 KB |
answer = YES |
49 |
Correct |
33 ms |
3552 KB |
answer = YES |
50 |
Correct |
7 ms |
3672 KB |
answer = YES |
51 |
Correct |
23 ms |
3856 KB |
answer = YES |
52 |
Correct |
4 ms |
4696 KB |
answer = NO |
53 |
Correct |
3 ms |
3932 KB |
answer = YES |
54 |
Correct |
7 ms |
3164 KB |
answer = YES |
55 |
Correct |
14 ms |
3204 KB |
answer = YES |
56 |
Correct |
34 ms |
3548 KB |
answer = YES |
57 |
Correct |
16 ms |
4440 KB |
answer = YES |
58 |
Correct |
19 ms |
4564 KB |
answer = YES |
59 |
Correct |
5 ms |
3416 KB |
answer = YES |
60 |
Correct |
28 ms |
3420 KB |
answer = YES |
61 |
Correct |
4 ms |
3160 KB |
answer = YES |
62 |
Correct |
36 ms |
10100 KB |
answer = NO |
63 |
Correct |
40 ms |
9988 KB |
answer = YES |
64 |
Correct |
41 ms |
11092 KB |
answer = NO |
65 |
Correct |
39 ms |
11200 KB |
answer = YES |
66 |
Correct |
4 ms |
3932 KB |
answer = YES |
67 |
Correct |
164 ms |
15308 KB |
answer = YES |
68 |
Correct |
141 ms |
15316 KB |
answer = YES |
69 |
Correct |
51 ms |
16972 KB |
answer = YES |
70 |
Correct |
77 ms |
19916 KB |
answer = YES |
71 |
Correct |
52 ms |
16968 KB |
answer = YES |
72 |
Correct |
375 ms |
9680 KB |
answer = YES |
73 |
Correct |
50 ms |
9680 KB |
answer = YES |
74 |
Correct |
36 ms |
10632 KB |
answer = YES |
75 |
Correct |
22 ms |
10456 KB |
answer = NO |
76 |
Correct |
42 ms |
4696 KB |
answer = YES |
77 |
Correct |
97 ms |
5464 KB |
answer = YES |
78 |
Correct |
165 ms |
5852 KB |
answer = YES |
79 |
Correct |
334 ms |
8928 KB |
answer = YES |
80 |
Correct |
217 ms |
10448 KB |
answer = YES |
81 |
Correct |
29 ms |
13008 KB |
answer = NO |
82 |
Correct |
65 ms |
15704 KB |
answer = YES |
83 |
Correct |
69 ms |
16968 KB |
answer = YES |
84 |
Correct |
619 ms |
15316 KB |
answer = YES |
85 |
Correct |
164 ms |
15316 KB |
answer = YES |
86 |
Correct |
68 ms |
16852 KB |
answer = YES |
87 |
Correct |
32 ms |
10836 KB |
answer = NO |
88 |
Correct |
111 ms |
12248 KB |
answer = YES |
89 |
Correct |
53 ms |
9040 KB |
answer = YES |
90 |
Correct |
67 ms |
8748 KB |
answer = YES |
91 |
Correct |
52 ms |
8788 KB |
answer = YES |
92 |
Correct |
28 ms |
6940 KB |
answer = YES |
93 |
Correct |
32 ms |
6936 KB |
answer = YES |
94 |
Correct |
49 ms |
15308 KB |
answer = NO |
95 |
Correct |
33 ms |
8496 KB |
answer = NO |
96 |
Correct |
87 ms |
17244 KB |
answer = YES |
97 |
Correct |
26 ms |
15316 KB |
answer = NO |