#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n';
const int M = 5e5+5, MOD = 1e9+7;
vector<array<int, 3>> v;
int p[M], d[M], loop;
vector<int> node[M];
bitset<500005> vis;
map<int, int> e[M];
double cost[M];
void dfs(int s, int dp = 0) {
vis[s] = 1;
d[s] = dp++;
for (int i:node[s]) {
if (!vis[i]) {
p[i] = s;
dfs(i, dp);
} else if (!loop&&(dp-d[i])%2) {
p[i] = s;
loop = s;
return;
}
}
}
double eval(int s, double a) {
double ans = a;
cost[s] = a;
vis[s] = 1;
for (int i:node[s]) if (!vis[i]) ans += abs(eval(i, e[s][i]-a));
return ans;
}
bool check() {
for (auto [a, b, c]:v) if (abs(cost[a] + cost[b] - c) >= 1e-6) return 0;
return true;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
if (e[a][b] && e[a][b] != c) {
cout << "NO" << endl;
return 0;
}
e[a][b] = c;
e[b][a] = c;
node[a].push_back(b);
node[b].push_back(a);
v.push_back({a, b, c});
}
dfs(1);
if (loop) {
int x = p[loop], y = loop, f = 0;
double sum = e[x][y];
do {
y = x, x = p[x];
if (f) sum += e[x][y];
else sum -= e[x][y];
f ^= 1;
} while (x != loop);
vis = 0;
eval(loop, sum/2);
// cout << "LOOP " << loop << endl;
} else {
vis = 0;
eval(1, 0);
if (!check()) {
cout << "NO" << endl;
return 0;
}
double l = -1e10, r = 1e10, cnt = 0;
while (abs(r-l) >= 1e-6 && cnt++<100) {
double ll = (2*l+r)/3;
double rr = (l+2*r)/3;
vis = 0; double x = eval(1, ll);
vis = 0; double y = eval(1, rr);
if (x >= y) l = ll;
else r = rr;
} vis = 0, eval(1, r);
// cout << "NO LOOP" << endl;
}
if (!check()) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << fixed << setprecision(6) << endl;
for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
return 0;
}
/*
4 4
1 2 1
2 3 2
1 3 2
3 4 1
2 1
1 2 1
3 2
1 2 2
2 3 2
3 4
1 2 2
2 2 1
2 1 1
1 2 2
*/
Compilation message
Graph.cpp: In function 'int main()':
Graph.cpp:104:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
104 | for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
| ^~~
Graph.cpp:104:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
104 | for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
35660 KB |
answer = YES |
2 |
Correct |
25 ms |
35628 KB |
answer = YES |
3 |
Correct |
26 ms |
35668 KB |
answer = YES |
4 |
Correct |
25 ms |
35556 KB |
answer = NO |
5 |
Incorrect |
24 ms |
35668 KB |
jury has the better answer: jans = YES, pans = NO |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
35660 KB |
answer = YES |
2 |
Correct |
25 ms |
35628 KB |
answer = YES |
3 |
Correct |
26 ms |
35668 KB |
answer = YES |
4 |
Correct |
25 ms |
35556 KB |
answer = NO |
5 |
Incorrect |
24 ms |
35668 KB |
jury has the better answer: jans = YES, pans = NO |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
35660 KB |
answer = YES |
2 |
Correct |
25 ms |
35628 KB |
answer = YES |
3 |
Correct |
26 ms |
35668 KB |
answer = YES |
4 |
Correct |
25 ms |
35556 KB |
answer = NO |
5 |
Incorrect |
24 ms |
35668 KB |
jury has the better answer: jans = YES, pans = NO |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
35660 KB |
answer = YES |
2 |
Correct |
25 ms |
35628 KB |
answer = YES |
3 |
Correct |
26 ms |
35668 KB |
answer = YES |
4 |
Correct |
25 ms |
35556 KB |
answer = NO |
5 |
Incorrect |
24 ms |
35668 KB |
jury has the better answer: jans = YES, pans = NO |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
35660 KB |
answer = YES |
2 |
Correct |
25 ms |
35628 KB |
answer = YES |
3 |
Correct |
26 ms |
35668 KB |
answer = YES |
4 |
Correct |
25 ms |
35556 KB |
answer = NO |
5 |
Incorrect |
24 ms |
35668 KB |
jury has the better answer: jans = YES, pans = NO |
6 |
Halted |
0 ms |
0 KB |
- |