#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pw(x) (1LL << (x))
using namespace std;
mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 1e5 + 5;
const ld EPS = 1e-10;
bool eq(ld a, ld b) {
return abs(a - b) <= EPS;
}
int n, m;
ve<pii> g[N];
bool vis[N];
int k[N], b[N];
ve<int> st;
ld X = -1e18;
bool found = 0;
ld ans[N];
void dfs(int v, int p) {
vis[v] = 1;
st.pb(v);
// cout << v << " " << k[v] << " " << b[v] << "\n";
fe (to, g[v]) {
if (!vis[to.fi]) {
b[to.fi] = -b[v] + to.se;
k[to.fi] = -k[v];
dfs(to.fi, v);
} else {
int needB = -b[v] + to.se;
int needK = -k[v];
if (mp(b[to.fi], k[to.fi]) == mp(needB, needK)) {
continue;
}
// x * needK + needB = x * k[to.fi] + b[to.fi]
// x * (needK - k[to.fi]) = b[to.fi] - needB
ld nx = ld(b[to.fi] - needB) / (needK - k[to.fi]);
if (found && !eq(nx, X)) {
cout << "NO\n";
exit(0);
}
X = nx;
found = 1;
}
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> m;
fr (i, 1, m) {
int a, b, c;
cin >> a >> b >> c;
g[a].pb({b, c});
g[b].pb({a, c});
}
fr (i, 1, n) {
if (!vis[i]) {
st.clear();
k[i] = 1;
X = -1e18;
found = 0;
dfs(i, 0);
if (found) {
fe (cur, st) {
ans[cur] = k[cur] * X + b[cur];
}
} else {
auto eval = [&](ld x) {
ld sum = 0;
fe (cur, st) {
sum += abs(k[cur] * x + b[cur]);
}
return sum;
};
fe (cur, st) {
assert(k[cur] == 1 || k[cur] == -1);
// assert(abs(b[cur]) <= 2);
}
ld l = -1e9;
ld r = 1e9;
fr (rep, 1, 200) {
ld p1 = l + (r - l) / 3;
ld p2 = r - (r - l) / 3;
if (eval(p1) < eval(p2)) {
r = p2;
} else {
l = p1;
}
}
fe (cur, st) {
ans[cur] = k[cur] * l + b[cur];
}
}
}
}
cout << "YES\n";
cout << fixed << setprecision(14);
fr (i, 1, n) {
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
1 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
1 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
1 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2644 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
2 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
2 ms |
2644 KB |
answer = NO |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
1 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
1 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
1 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2644 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
2 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
2 ms |
2644 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
1 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2628 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
2 ms |
2644 KB |
answer = YES |
32 |
Correct |
2 ms |
2644 KB |
answer = YES |
33 |
Correct |
2 ms |
2644 KB |
answer = YES |
34 |
Correct |
3 ms |
2644 KB |
answer = YES |
35 |
Correct |
3 ms |
2644 KB |
answer = YES |
36 |
Correct |
3 ms |
2644 KB |
answer = YES |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
1 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
1 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
1 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2644 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
2 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
2 ms |
2644 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
1 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2628 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
2 ms |
2644 KB |
answer = YES |
32 |
Correct |
2 ms |
2644 KB |
answer = YES |
33 |
Correct |
2 ms |
2644 KB |
answer = YES |
34 |
Correct |
3 ms |
2644 KB |
answer = YES |
35 |
Correct |
3 ms |
2644 KB |
answer = YES |
36 |
Correct |
3 ms |
2644 KB |
answer = YES |
37 |
Correct |
3 ms |
2644 KB |
answer = YES |
38 |
Correct |
3 ms |
2644 KB |
answer = YES |
39 |
Correct |
3 ms |
2644 KB |
answer = YES |
40 |
Correct |
3 ms |
2772 KB |
answer = YES |
41 |
Correct |
2 ms |
2712 KB |
answer = NO |
42 |
Correct |
4 ms |
2752 KB |
answer = YES |
43 |
Correct |
4 ms |
2756 KB |
answer = YES |
44 |
Correct |
4 ms |
2644 KB |
answer = YES |
45 |
Correct |
4 ms |
2772 KB |
answer = YES |
46 |
Correct |
3 ms |
2644 KB |
answer = YES |
47 |
Correct |
3 ms |
2772 KB |
answer = YES |
48 |
Correct |
3 ms |
2772 KB |
answer = YES |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
1 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
1 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
1 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2644 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
2 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
2 ms |
2644 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
1 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2628 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
2 ms |
2644 KB |
answer = YES |
32 |
Correct |
2 ms |
2644 KB |
answer = YES |
33 |
Correct |
2 ms |
2644 KB |
answer = YES |
34 |
Correct |
3 ms |
2644 KB |
answer = YES |
35 |
Correct |
3 ms |
2644 KB |
answer = YES |
36 |
Correct |
3 ms |
2644 KB |
answer = YES |
37 |
Correct |
3 ms |
2644 KB |
answer = YES |
38 |
Correct |
3 ms |
2644 KB |
answer = YES |
39 |
Correct |
3 ms |
2644 KB |
answer = YES |
40 |
Correct |
3 ms |
2772 KB |
answer = YES |
41 |
Correct |
2 ms |
2712 KB |
answer = NO |
42 |
Correct |
4 ms |
2752 KB |
answer = YES |
43 |
Correct |
4 ms |
2756 KB |
answer = YES |
44 |
Correct |
4 ms |
2644 KB |
answer = YES |
45 |
Correct |
4 ms |
2772 KB |
answer = YES |
46 |
Correct |
3 ms |
2644 KB |
answer = YES |
47 |
Correct |
3 ms |
2772 KB |
answer = YES |
48 |
Correct |
3 ms |
2772 KB |
answer = YES |
49 |
Correct |
17 ms |
3620 KB |
answer = YES |
50 |
Correct |
11 ms |
3892 KB |
answer = YES |
51 |
Correct |
18 ms |
3912 KB |
answer = YES |
52 |
Correct |
5 ms |
3668 KB |
answer = NO |
53 |
Correct |
4 ms |
2788 KB |
answer = YES |
54 |
Correct |
5 ms |
2832 KB |
answer = YES |
55 |
Correct |
12 ms |
3164 KB |
answer = YES |
56 |
Correct |
16 ms |
3672 KB |
answer = YES |
57 |
Correct |
19 ms |
3544 KB |
answer = YES |
58 |
Correct |
21 ms |
3472 KB |
answer = YES |
59 |
Correct |
9 ms |
3540 KB |
answer = YES |
60 |
Correct |
18 ms |
3656 KB |
answer = YES |
61 |
Correct |
6 ms |
3084 KB |
answer = YES |
62 |
Correct |
60 ms |
9676 KB |
answer = NO |
63 |
Correct |
72 ms |
10444 KB |
answer = YES |
64 |
Correct |
56 ms |
10188 KB |
answer = NO |
65 |
Correct |
60 ms |
10396 KB |
answer = YES |
66 |
Correct |
5 ms |
2772 KB |
answer = YES |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
1 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
1 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
1 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2644 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
2 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2652 KB |
answer = NO |
23 |
Correct |
2 ms |
2644 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
1 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2628 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
2 ms |
2644 KB |
answer = YES |
32 |
Correct |
2 ms |
2644 KB |
answer = YES |
33 |
Correct |
2 ms |
2644 KB |
answer = YES |
34 |
Correct |
3 ms |
2644 KB |
answer = YES |
35 |
Correct |
3 ms |
2644 KB |
answer = YES |
36 |
Correct |
3 ms |
2644 KB |
answer = YES |
37 |
Correct |
3 ms |
2644 KB |
answer = YES |
38 |
Correct |
3 ms |
2644 KB |
answer = YES |
39 |
Correct |
3 ms |
2644 KB |
answer = YES |
40 |
Correct |
3 ms |
2772 KB |
answer = YES |
41 |
Correct |
2 ms |
2712 KB |
answer = NO |
42 |
Correct |
4 ms |
2752 KB |
answer = YES |
43 |
Correct |
4 ms |
2756 KB |
answer = YES |
44 |
Correct |
4 ms |
2644 KB |
answer = YES |
45 |
Correct |
4 ms |
2772 KB |
answer = YES |
46 |
Correct |
3 ms |
2644 KB |
answer = YES |
47 |
Correct |
3 ms |
2772 KB |
answer = YES |
48 |
Correct |
3 ms |
2772 KB |
answer = YES |
49 |
Correct |
17 ms |
3620 KB |
answer = YES |
50 |
Correct |
11 ms |
3892 KB |
answer = YES |
51 |
Correct |
18 ms |
3912 KB |
answer = YES |
52 |
Correct |
5 ms |
3668 KB |
answer = NO |
53 |
Correct |
4 ms |
2788 KB |
answer = YES |
54 |
Correct |
5 ms |
2832 KB |
answer = YES |
55 |
Correct |
12 ms |
3164 KB |
answer = YES |
56 |
Correct |
16 ms |
3672 KB |
answer = YES |
57 |
Correct |
19 ms |
3544 KB |
answer = YES |
58 |
Correct |
21 ms |
3472 KB |
answer = YES |
59 |
Correct |
9 ms |
3540 KB |
answer = YES |
60 |
Correct |
18 ms |
3656 KB |
answer = YES |
61 |
Correct |
6 ms |
3084 KB |
answer = YES |
62 |
Correct |
60 ms |
9676 KB |
answer = NO |
63 |
Correct |
72 ms |
10444 KB |
answer = YES |
64 |
Correct |
56 ms |
10188 KB |
answer = NO |
65 |
Correct |
60 ms |
10396 KB |
answer = YES |
66 |
Correct |
5 ms |
2772 KB |
answer = YES |
67 |
Correct |
165 ms |
18096 KB |
answer = YES |
68 |
Correct |
155 ms |
18068 KB |
answer = YES |
69 |
Correct |
107 ms |
17996 KB |
answer = YES |
70 |
Correct |
115 ms |
22512 KB |
answer = YES |
71 |
Correct |
84 ms |
18048 KB |
answer = YES |
72 |
Correct |
160 ms |
12376 KB |
answer = YES |
73 |
Correct |
129 ms |
12392 KB |
answer = YES |
74 |
Correct |
78 ms |
11944 KB |
answer = YES |
75 |
Correct |
26 ms |
9612 KB |
answer = NO |
76 |
Correct |
26 ms |
3812 KB |
answer = YES |
77 |
Correct |
41 ms |
5068 KB |
answer = YES |
78 |
Correct |
85 ms |
6948 KB |
answer = YES |
79 |
Correct |
159 ms |
11040 KB |
answer = YES |
80 |
Correct |
121 ms |
11936 KB |
answer = YES |
81 |
Correct |
45 ms |
11864 KB |
answer = NO |
82 |
Correct |
108 ms |
17644 KB |
answer = YES |
83 |
Correct |
134 ms |
18260 KB |
answer = YES |
84 |
Correct |
224 ms |
18092 KB |
answer = YES |
85 |
Correct |
167 ms |
18144 KB |
answer = YES |
86 |
Correct |
102 ms |
18352 KB |
answer = YES |
87 |
Correct |
54 ms |
11580 KB |
answer = NO |
88 |
Correct |
125 ms |
14228 KB |
answer = YES |
89 |
Correct |
103 ms |
11220 KB |
answer = YES |
90 |
Correct |
105 ms |
11212 KB |
answer = YES |
91 |
Correct |
96 ms |
11184 KB |
answer = YES |
92 |
Correct |
61 ms |
7616 KB |
answer = YES |
93 |
Correct |
45 ms |
7608 KB |
answer = YES |
94 |
Correct |
49 ms |
14752 KB |
answer = NO |
95 |
Correct |
44 ms |
9500 KB |
answer = NO |
96 |
Correct |
179 ms |
20020 KB |
answer = YES |
97 |
Correct |
46 ms |
14732 KB |
answer = NO |