#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize("Ofast,fast-math,unroll-loops")
#pragma GCC target("avx2,fma")
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 1e5 + 5;
const long long INF = 1e15;
bool vis[N];
bool vis2[N];
bool color[N];
stack<int> stk;
long long val[N];
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;
}
long long assign(int v, long long c) {
if (c > INF) {
return INF;
}
if (vis2[v]) {
if (val[v] != c) {
return INF;
}
return 0;
}
vis2[v] = true;
val[v] = c;
long long 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];
long long x = assign(rt, sum / 2);
if (x == INF) {
cout << "NO" << '\n';
return 0;
}
odd_cycle.clear();
} else {
int l = -2e5 - 9, r = 2e5 + 9;
while (r - l > 3) {
//int ml = l + (r - l) / 3;
//int mr = r - (r - l) / 3;
int mid = (l + r) / 2;
long long tmp = assign(i, mid);
for (int v : component) {
vis2[v] = false;
}
long long tmp2 = assign(i, mid + 1);
for (int v : component) {
vis2[v] = false;
}
if (tmp < tmp2) {
r = mid + 1;
} else {
l = mid;
}
}
long long mn = INF, uv = -1;
for (int tval = l; tval <= r; tval++) {
long long tmp = assign(i, tval);
if (tmp < mn) {
mn = tmp;
uv = tval;
}
for (int v : component) {
vis2[v] = false;
}
}
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 |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2648 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2648 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2696 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 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 |
2648 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
1 ms |
2652 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2648 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2648 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2696 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 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 |
2648 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
1 ms |
2652 KB |
answer = NO |
24 |
Correct |
1 ms |
2652 KB |
answer = YES |
25 |
Correct |
1 ms |
2652 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
1 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2648 KB |
answer = YES |
29 |
Correct |
1 ms |
2724 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2652 KB |
answer = YES |
32 |
Correct |
1 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2648 KB |
answer = YES |
34 |
Correct |
1 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 |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2648 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2648 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2696 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 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 |
2648 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
1 ms |
2652 KB |
answer = NO |
24 |
Correct |
1 ms |
2652 KB |
answer = YES |
25 |
Correct |
1 ms |
2652 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
1 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2648 KB |
answer = YES |
29 |
Correct |
1 ms |
2724 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2652 KB |
answer = YES |
32 |
Correct |
1 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2648 KB |
answer = YES |
34 |
Correct |
1 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 |
2652 KB |
answer = YES |
38 |
Correct |
1 ms |
2856 KB |
answer = YES |
39 |
Correct |
1 ms |
2652 KB |
answer = YES |
40 |
Correct |
2 ms |
2908 KB |
answer = YES |
41 |
Correct |
2 ms |
2908 KB |
answer = NO |
42 |
Correct |
2 ms |
2908 KB |
answer = YES |
43 |
Correct |
2 ms |
2908 KB |
answer = YES |
44 |
Correct |
2 ms |
2908 KB |
answer = YES |
45 |
Correct |
1 ms |
2908 KB |
answer = YES |
46 |
Correct |
2 ms |
2652 KB |
answer = YES |
47 |
Correct |
2 ms |
2908 KB |
answer = YES |
48 |
Correct |
2 ms |
2904 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2648 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2648 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2696 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 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 |
2648 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
1 ms |
2652 KB |
answer = NO |
24 |
Correct |
1 ms |
2652 KB |
answer = YES |
25 |
Correct |
1 ms |
2652 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
1 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2648 KB |
answer = YES |
29 |
Correct |
1 ms |
2724 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2652 KB |
answer = YES |
32 |
Correct |
1 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2648 KB |
answer = YES |
34 |
Correct |
1 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 |
2652 KB |
answer = YES |
38 |
Correct |
1 ms |
2856 KB |
answer = YES |
39 |
Correct |
1 ms |
2652 KB |
answer = YES |
40 |
Correct |
2 ms |
2908 KB |
answer = YES |
41 |
Correct |
2 ms |
2908 KB |
answer = NO |
42 |
Correct |
2 ms |
2908 KB |
answer = YES |
43 |
Correct |
2 ms |
2908 KB |
answer = YES |
44 |
Correct |
2 ms |
2908 KB |
answer = YES |
45 |
Correct |
1 ms |
2908 KB |
answer = YES |
46 |
Correct |
2 ms |
2652 KB |
answer = YES |
47 |
Correct |
2 ms |
2908 KB |
answer = YES |
48 |
Correct |
2 ms |
2904 KB |
answer = YES |
49 |
Correct |
17 ms |
3932 KB |
answer = YES |
50 |
Correct |
10 ms |
4444 KB |
answer = YES |
51 |
Correct |
18 ms |
4508 KB |
answer = YES |
52 |
Correct |
15 ms |
4508 KB |
answer = NO |
53 |
Correct |
2 ms |
2908 KB |
answer = YES |
54 |
Correct |
3 ms |
2908 KB |
answer = YES |
55 |
Correct |
8 ms |
3420 KB |
answer = YES |
56 |
Correct |
19 ms |
4004 KB |
answer = YES |
57 |
Correct |
16 ms |
3676 KB |
answer = YES |
58 |
Correct |
15 ms |
3672 KB |
answer = YES |
59 |
Correct |
9 ms |
3676 KB |
answer = YES |
60 |
Correct |
17 ms |
3988 KB |
answer = YES |
61 |
Correct |
5 ms |
3420 KB |
answer = YES |
62 |
Correct |
10 ms |
4020 KB |
answer = NO |
63 |
Correct |
168 ms |
20760 KB |
answer = YES |
64 |
Correct |
163 ms |
20564 KB |
answer = NO |
65 |
Correct |
171 ms |
20664 KB |
answer = YES |
66 |
Correct |
3 ms |
2908 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2652 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2648 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2648 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2696 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
1 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 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 |
2648 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
1 ms |
2652 KB |
answer = NO |
24 |
Correct |
1 ms |
2652 KB |
answer = YES |
25 |
Correct |
1 ms |
2652 KB |
answer = YES |
26 |
Correct |
1 ms |
2652 KB |
answer = YES |
27 |
Correct |
1 ms |
2652 KB |
answer = YES |
28 |
Correct |
1 ms |
2648 KB |
answer = YES |
29 |
Correct |
1 ms |
2724 KB |
answer = YES |
30 |
Correct |
1 ms |
2652 KB |
answer = NO |
31 |
Correct |
1 ms |
2652 KB |
answer = YES |
32 |
Correct |
1 ms |
2652 KB |
answer = YES |
33 |
Correct |
1 ms |
2648 KB |
answer = YES |
34 |
Correct |
1 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 |
2652 KB |
answer = YES |
38 |
Correct |
1 ms |
2856 KB |
answer = YES |
39 |
Correct |
1 ms |
2652 KB |
answer = YES |
40 |
Correct |
2 ms |
2908 KB |
answer = YES |
41 |
Correct |
2 ms |
2908 KB |
answer = NO |
42 |
Correct |
2 ms |
2908 KB |
answer = YES |
43 |
Correct |
2 ms |
2908 KB |
answer = YES |
44 |
Correct |
2 ms |
2908 KB |
answer = YES |
45 |
Correct |
1 ms |
2908 KB |
answer = YES |
46 |
Correct |
2 ms |
2652 KB |
answer = YES |
47 |
Correct |
2 ms |
2908 KB |
answer = YES |
48 |
Correct |
2 ms |
2904 KB |
answer = YES |
49 |
Correct |
17 ms |
3932 KB |
answer = YES |
50 |
Correct |
10 ms |
4444 KB |
answer = YES |
51 |
Correct |
18 ms |
4508 KB |
answer = YES |
52 |
Correct |
15 ms |
4508 KB |
answer = NO |
53 |
Correct |
2 ms |
2908 KB |
answer = YES |
54 |
Correct |
3 ms |
2908 KB |
answer = YES |
55 |
Correct |
8 ms |
3420 KB |
answer = YES |
56 |
Correct |
19 ms |
4004 KB |
answer = YES |
57 |
Correct |
16 ms |
3676 KB |
answer = YES |
58 |
Correct |
15 ms |
3672 KB |
answer = YES |
59 |
Correct |
9 ms |
3676 KB |
answer = YES |
60 |
Correct |
17 ms |
3988 KB |
answer = YES |
61 |
Correct |
5 ms |
3420 KB |
answer = YES |
62 |
Correct |
10 ms |
4020 KB |
answer = NO |
63 |
Correct |
168 ms |
20760 KB |
answer = YES |
64 |
Correct |
163 ms |
20564 KB |
answer = NO |
65 |
Correct |
171 ms |
20664 KB |
answer = YES |
66 |
Correct |
3 ms |
2908 KB |
answer = YES |
67 |
Correct |
127 ms |
23624 KB |
answer = YES |
68 |
Correct |
124 ms |
23496 KB |
answer = YES |
69 |
Correct |
93 ms |
24136 KB |
answer = YES |
70 |
Correct |
137 ms |
36184 KB |
answer = YES |
71 |
Correct |
99 ms |
24388 KB |
answer = YES |
72 |
Correct |
225 ms |
14592 KB |
answer = YES |
73 |
Correct |
111 ms |
14536 KB |
answer = YES |
74 |
Correct |
71 ms |
15056 KB |
answer = YES |
75 |
Correct |
122 ms |
14800 KB |
answer = NO |
76 |
Correct |
22 ms |
4076 KB |
answer = YES |
77 |
Correct |
50 ms |
5720 KB |
answer = YES |
78 |
Correct |
88 ms |
7884 KB |
answer = YES |
79 |
Correct |
182 ms |
12744 KB |
answer = YES |
80 |
Correct |
150 ms |
14956 KB |
answer = YES |
81 |
Correct |
97 ms |
19148 KB |
answer = NO |
82 |
Correct |
133 ms |
23120 KB |
answer = YES |
83 |
Correct |
131 ms |
24132 KB |
answer = YES |
84 |
Correct |
367 ms |
23752 KB |
answer = YES |
85 |
Correct |
125 ms |
23748 KB |
answer = YES |
86 |
Correct |
97 ms |
24160 KB |
answer = YES |
87 |
Correct |
147 ms |
16152 KB |
answer = NO |
88 |
Correct |
160 ms |
18192 KB |
answer = YES |
89 |
Correct |
116 ms |
13516 KB |
answer = YES |
90 |
Correct |
110 ms |
13564 KB |
answer = YES |
91 |
Correct |
104 ms |
13396 KB |
answer = YES |
92 |
Correct |
54 ms |
8740 KB |
answer = YES |
93 |
Correct |
63 ms |
8732 KB |
answer = YES |
94 |
Correct |
296 ms |
23412 KB |
answer = NO |
95 |
Correct |
66 ms |
13136 KB |
answer = NO |
96 |
Correct |
234 ms |
29384 KB |
answer = YES |
97 |
Correct |
94 ms |
23240 KB |
answer = NO |