/**
* author: kututay
* created: 20.09.2022 13:13:02
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/Users/kutay/CP/templates/debug.h"
#else
#define debug(...) void(38)
#endif
const double epsilon = 1e-6;
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<pair<int, int>>> g(n);
vector<pair<pair<int, int>, int>> edges(m);
for (int i = 0; i < m; i++) {
int x, y, z; cin >> x >> y >> z, x--, y--;
g[x].emplace_back(y, z);
g[y].emplace_back(x, z);
}
vector<double> ans(n);
vector<int> components_vis(n);
auto solve = [&](int node) {
vector<pair<int, int>> n_vals(n, make_pair(0, 0)); // (first * x + second) - node values
vector<int> cycle_vis(n);
bool cycle = false;
vector<pair<pair<int, int>, int>> edges;
set<int> nodes;
function<void(int, int, int)> find_cycle = [&](int node, int parent, int color) {
nodes.insert(node);
if (cycle_vis[node]) {
cycle = true;
return;
}
cycle_vis[node] = 1;
components_vis[node] = 1;
for (auto next : g[node]) {
edges.emplace_back(make_pair(node, next.first), next.second);
if (parent == -1) n_vals[node] = make_pair(1, 0);
else n_vals[node] = make_pair(-n_vals[parent].first, color - n_vals[parent].second);
if (next.first != parent) find_cycle(next.first, node, next.second);
}
};
find_cycle(node, -1, -1);
double x;
if ((int) edges.size() == 0) return;
bool flag = false;
for (auto [p, color] : edges) {
auto a = n_vals[p.first];
auto b = n_vals[p.second];
pair<int, int> should = make_pair(-a.first, color - a.second);
if (b == should) continue;
pair<int, int> sum = make_pair(b.first + a.first, color - (b.second + a.second));
if (sum.first == 0) {
cout << "NO\n";
exit(0);
}
x = (double) sum.second / sum.first;
flag = true;
}
debug(flag, cycle, x);
if (flag == false or cycle == false) {
int N = 100;
vector<double> s(2 * N + 1);
for (int i : nodes) {
for (int j = -N; j < N; j++) {
s[j + N] += abs(n_vals[i].first * j + n_vals[i].second);
}
}
int mn = 1e9;
for (int j = -N; j < N; j++) {
if (s[j + N] < mn) {
x = j;
mn = s[j + N];
}
}
for (int i : nodes) {
ans[i] = n_vals[i].first * x + n_vals[i].second;
}
return;
}
for (auto [p, color] : edges) {
auto a = n_vals[p.first];
auto b = n_vals[p.second];
double sum = a.first * x + a.second + b.first * x + b.second;
if (abs(sum - color) > epsilon) {
cout << "NO\n";
exit(0);
}
}
for (int i : nodes) {
ans[i] = n_vals[i].first * x + n_vals[i].second;
}
debug(ans);
};
for (int i = 0; i < n; i++) {
if (components_vis[i]) continue;
solve(i);
}
cout << "YES\n";
for (auto x : ans) cout << fixed << setprecision(6) << x << " ";
cout << '\n';
double top = 0;
for (auto x : ans) top += abs(x);
debug(ans.size());
debug(top);
}
Compilation message
Graph.cpp: In function 'int32_t main()':
Graph.cpp:84:34: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | ans[i] = n_vals[i].first * x + n_vals[i].second;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
answer = YES |
2 |
Correct |
1 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
212 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
answer = YES |
2 |
Correct |
1 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
212 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
0 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
0 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
340 KB |
answer = YES |
35 |
Correct |
0 ms |
212 KB |
answer = YES |
36 |
Correct |
0 ms |
212 KB |
answer = YES |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
answer = YES |
2 |
Correct |
1 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
212 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
0 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
0 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
340 KB |
answer = YES |
35 |
Correct |
0 ms |
212 KB |
answer = YES |
36 |
Correct |
0 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
340 KB |
answer = YES |
38 |
Correct |
1 ms |
340 KB |
answer = YES |
39 |
Correct |
2 ms |
416 KB |
answer = YES |
40 |
Correct |
1 ms |
468 KB |
answer = YES |
41 |
Correct |
1 ms |
468 KB |
answer = NO |
42 |
Correct |
2 ms |
468 KB |
answer = YES |
43 |
Correct |
1 ms |
340 KB |
answer = YES |
44 |
Correct |
2 ms |
340 KB |
answer = YES |
45 |
Correct |
1 ms |
468 KB |
answer = YES |
46 |
Correct |
1 ms |
340 KB |
answer = YES |
47 |
Correct |
1 ms |
468 KB |
answer = YES |
48 |
Correct |
1 ms |
468 KB |
answer = YES |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
answer = YES |
2 |
Correct |
1 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
212 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
0 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
0 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
340 KB |
answer = YES |
35 |
Correct |
0 ms |
212 KB |
answer = YES |
36 |
Correct |
0 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
340 KB |
answer = YES |
38 |
Correct |
1 ms |
340 KB |
answer = YES |
39 |
Correct |
2 ms |
416 KB |
answer = YES |
40 |
Correct |
1 ms |
468 KB |
answer = YES |
41 |
Correct |
1 ms |
468 KB |
answer = NO |
42 |
Correct |
2 ms |
468 KB |
answer = YES |
43 |
Correct |
1 ms |
340 KB |
answer = YES |
44 |
Correct |
2 ms |
340 KB |
answer = YES |
45 |
Correct |
1 ms |
468 KB |
answer = YES |
46 |
Correct |
1 ms |
340 KB |
answer = YES |
47 |
Correct |
1 ms |
468 KB |
answer = YES |
48 |
Correct |
1 ms |
468 KB |
answer = YES |
49 |
Correct |
20 ms |
2068 KB |
answer = YES |
50 |
Correct |
11 ms |
2704 KB |
answer = YES |
51 |
Correct |
13 ms |
2900 KB |
answer = YES |
52 |
Correct |
6 ms |
2900 KB |
answer = NO |
53 |
Correct |
2 ms |
468 KB |
answer = YES |
54 |
Correct |
4 ms |
724 KB |
answer = YES |
55 |
Correct |
6 ms |
1116 KB |
answer = YES |
56 |
Correct |
12 ms |
2004 KB |
answer = YES |
57 |
Correct |
20 ms |
1496 KB |
answer = YES |
58 |
Correct |
11 ms |
1496 KB |
answer = YES |
59 |
Correct |
10 ms |
1580 KB |
answer = YES |
60 |
Correct |
12 ms |
1624 KB |
answer = YES |
61 |
Correct |
6 ms |
1116 KB |
answer = YES |
62 |
Correct |
101 ms |
15112 KB |
answer = NO |
63 |
Correct |
107 ms |
15156 KB |
answer = YES |
64 |
Correct |
102 ms |
15168 KB |
answer = NO |
65 |
Correct |
130 ms |
15156 KB |
answer = YES |
66 |
Correct |
3 ms |
596 KB |
answer = YES |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
answer = YES |
2 |
Correct |
1 ms |
212 KB |
answer = YES |
3 |
Correct |
1 ms |
212 KB |
answer = YES |
4 |
Correct |
1 ms |
212 KB |
answer = NO |
5 |
Correct |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
0 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
1 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
0 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
0 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
0 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
340 KB |
answer = YES |
35 |
Correct |
0 ms |
212 KB |
answer = YES |
36 |
Correct |
0 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
340 KB |
answer = YES |
38 |
Correct |
1 ms |
340 KB |
answer = YES |
39 |
Correct |
2 ms |
416 KB |
answer = YES |
40 |
Correct |
1 ms |
468 KB |
answer = YES |
41 |
Correct |
1 ms |
468 KB |
answer = NO |
42 |
Correct |
2 ms |
468 KB |
answer = YES |
43 |
Correct |
1 ms |
340 KB |
answer = YES |
44 |
Correct |
2 ms |
340 KB |
answer = YES |
45 |
Correct |
1 ms |
468 KB |
answer = YES |
46 |
Correct |
1 ms |
340 KB |
answer = YES |
47 |
Correct |
1 ms |
468 KB |
answer = YES |
48 |
Correct |
1 ms |
468 KB |
answer = YES |
49 |
Correct |
20 ms |
2068 KB |
answer = YES |
50 |
Correct |
11 ms |
2704 KB |
answer = YES |
51 |
Correct |
13 ms |
2900 KB |
answer = YES |
52 |
Correct |
6 ms |
2900 KB |
answer = NO |
53 |
Correct |
2 ms |
468 KB |
answer = YES |
54 |
Correct |
4 ms |
724 KB |
answer = YES |
55 |
Correct |
6 ms |
1116 KB |
answer = YES |
56 |
Correct |
12 ms |
2004 KB |
answer = YES |
57 |
Correct |
20 ms |
1496 KB |
answer = YES |
58 |
Correct |
11 ms |
1496 KB |
answer = YES |
59 |
Correct |
10 ms |
1580 KB |
answer = YES |
60 |
Correct |
12 ms |
1624 KB |
answer = YES |
61 |
Correct |
6 ms |
1116 KB |
answer = YES |
62 |
Correct |
101 ms |
15112 KB |
answer = NO |
63 |
Correct |
107 ms |
15156 KB |
answer = YES |
64 |
Correct |
102 ms |
15168 KB |
answer = NO |
65 |
Correct |
130 ms |
15156 KB |
answer = YES |
66 |
Correct |
3 ms |
596 KB |
answer = YES |
67 |
Correct |
132 ms |
32000 KB |
answer = YES |
68 |
Correct |
126 ms |
31996 KB |
answer = YES |
69 |
Correct |
106 ms |
31916 KB |
answer = YES |
70 |
Correct |
152 ms |
40248 KB |
answer = YES |
71 |
Correct |
108 ms |
31944 KB |
answer = YES |
72 |
Correct |
152 ms |
16888 KB |
answer = YES |
73 |
Correct |
125 ms |
16892 KB |
answer = YES |
74 |
Correct |
86 ms |
18688 KB |
answer = YES |
75 |
Correct |
46 ms |
18724 KB |
answer = NO |
76 |
Correct |
15 ms |
2320 KB |
answer = YES |
77 |
Correct |
31 ms |
4364 KB |
answer = YES |
78 |
Correct |
56 ms |
7508 KB |
answer = YES |
79 |
Correct |
121 ms |
14564 KB |
answer = YES |
80 |
Correct |
96 ms |
18688 KB |
answer = YES |
81 |
Correct |
74 ms |
24440 KB |
answer = NO |
82 |
Correct |
139 ms |
31032 KB |
answer = YES |
83 |
Correct |
158 ms |
31928 KB |
answer = YES |
84 |
Correct |
170 ms |
32024 KB |
answer = YES |
85 |
Correct |
129 ms |
31980 KB |
answer = YES |
86 |
Correct |
106 ms |
31996 KB |
answer = YES |
87 |
Correct |
52 ms |
15804 KB |
answer = NO |
88 |
Correct |
146 ms |
19368 KB |
answer = YES |
89 |
Execution timed out |
1081 ms |
9292 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |