#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n';
const int M = 1e5+5, MOD = 1e9+7;
int p[M], d[M], loop, vis[M];
vector<pair<int, int>> node[M];
bitset<100005> is;
vector<int> tmp;
double cost[M];
void dfs(int s, int dp = 0) {
vis[s] = 1;
d[s] = dp++;
tmp.push_back(s);
for (auto [i, x]:node[s]) {
if (!vis[i]) {
p[i] = s;
dfs(i, dp);
} else if (!loop && (dp-d[i])%2) {
loop = s;
p[i] = s;
}
}
}
double eval(int s, double a, int cnt = 0) {
double ans = abs(a);
cost[s] = a;
is[s] = 1;
for (auto [i, x]:node[s]) {
if (!is[i]) ans += abs(eval(i, x-a));
} return ans;
}
bool check(vector<int> v) {
for (int y:v) {
for (auto [i, x]:node[y]) {
if (abs(cost[y] + cost[i]-x) >= 1e-6) return 0;
}
} return true;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
map<pair<int, int>, int> ed;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a > b) swap(a, b);
if (ed[{a, b}] && ed[{a, b}] != c) {
cout << "NO" << endl;
return 0;
} else if (ed[{a, b}]) continue;
ed[{a, b}] = ed[{b, a}] = c;
node[a].push_back({b, c});
node[b].push_back({a, c});
}
for (int k = 1; k <= n; k++) {
if (!vis[k] && node[k].size()) {
dfs(k);
if (loop) {
vector<int> lp = {loop};
for (int x = p[loop]; x != loop; x = p[x]) lp.push_back(x);
lp.push_back(loop);
double sum = 0;
for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) {
if (f) sum += ed[{min(lp[i], lp[i+1]), max(lp[i], lp[i+1])}];
else sum -= ed[{min(lp[i], lp[i+1]), max(lp[i], lp[i+1])}];
}
eval(loop, sum/2);
} else {
eval(k, 0);
if (!check(tmp)) {
cout << "NO" << endl;
return 0;
}
int l = -n-1, r = n+1, cnt = 0;
while (cnt++<60) {
int ll = (2*l+r)/3;
int rr = (l+2*r)/3;
is = 0; double x = eval(k, ll);
is = 0; double y = eval(k, rr);
if (x >= y) l = ll;
else r = rr;
} is = 0; eval(k, (l+r)/2);
}
if (!check(tmp)) {
cout << "NO" << endl;
return 0;
}
tmp = {};
loop = 0;
}
}
cout << "YES" << fixed << setprecision(6) << endl;
for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
return 0;
}
/*
3 3
1 1 2
2 2 1
2 3 2
4 5
1 4 2
3 2 1
2 1 2
3 4 1
1 3 2
4 4
1 2 1
2 3 2
1 3 2
3 4 1
*/
Compilation message
Graph.cpp: In function 'int main()':
Graph.cpp:76:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) {
| ~~^~~~~~~~~~~~~
Graph.cpp:111:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
111 | for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
| ^~~
Graph.cpp:111:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
111 | for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
3 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
9 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
2 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
2 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
2 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2696 KB |
answer = YES |
14 |
Correct |
2 ms |
2700 KB |
answer = YES |
15 |
Correct |
2 ms |
2700 KB |
answer = YES |
16 |
Correct |
2 ms |
2644 KB |
answer = YES |
17 |
Correct |
2 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
2 ms |
2700 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
2 ms |
2644 KB |
answer = NO |
23 |
Correct |
2 ms |
2700 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
3 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
9 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
2 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
2 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
2 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2696 KB |
answer = YES |
14 |
Correct |
2 ms |
2700 KB |
answer = YES |
15 |
Correct |
2 ms |
2700 KB |
answer = YES |
16 |
Correct |
2 ms |
2644 KB |
answer = YES |
17 |
Correct |
2 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
2 ms |
2700 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
2 ms |
2644 KB |
answer = NO |
23 |
Correct |
2 ms |
2700 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
2 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2644 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 |
4 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
2 ms |
2644 KB |
answer = YES |
36 |
Correct |
2 ms |
2644 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
3 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
9 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
2 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
2 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
2 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2696 KB |
answer = YES |
14 |
Correct |
2 ms |
2700 KB |
answer = YES |
15 |
Correct |
2 ms |
2700 KB |
answer = YES |
16 |
Correct |
2 ms |
2644 KB |
answer = YES |
17 |
Correct |
2 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
2 ms |
2700 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
2 ms |
2644 KB |
answer = NO |
23 |
Correct |
2 ms |
2700 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
2 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2644 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 |
4 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
2 ms |
2644 KB |
answer = YES |
36 |
Correct |
2 ms |
2644 KB |
answer = YES |
37 |
Correct |
3 ms |
2700 KB |
answer = YES |
38 |
Correct |
2 ms |
2644 KB |
answer = YES |
39 |
Correct |
4 ms |
2772 KB |
answer = YES |
40 |
Correct |
3 ms |
2972 KB |
answer = YES |
41 |
Correct |
3 ms |
2900 KB |
answer = NO |
42 |
Correct |
5 ms |
2900 KB |
answer = YES |
43 |
Correct |
5 ms |
2832 KB |
answer = YES |
44 |
Correct |
6 ms |
2900 KB |
answer = YES |
45 |
Correct |
3 ms |
2900 KB |
answer = YES |
46 |
Correct |
3 ms |
2772 KB |
answer = YES |
47 |
Correct |
3 ms |
2900 KB |
answer = YES |
48 |
Correct |
3 ms |
2900 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
3 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
9 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
2 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
2 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
2 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2696 KB |
answer = YES |
14 |
Correct |
2 ms |
2700 KB |
answer = YES |
15 |
Correct |
2 ms |
2700 KB |
answer = YES |
16 |
Correct |
2 ms |
2644 KB |
answer = YES |
17 |
Correct |
2 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
2 ms |
2700 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
2 ms |
2644 KB |
answer = NO |
23 |
Correct |
2 ms |
2700 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
2 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2644 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 |
4 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
2 ms |
2644 KB |
answer = YES |
36 |
Correct |
2 ms |
2644 KB |
answer = YES |
37 |
Correct |
3 ms |
2700 KB |
answer = YES |
38 |
Correct |
2 ms |
2644 KB |
answer = YES |
39 |
Correct |
4 ms |
2772 KB |
answer = YES |
40 |
Correct |
3 ms |
2972 KB |
answer = YES |
41 |
Correct |
3 ms |
2900 KB |
answer = NO |
42 |
Correct |
5 ms |
2900 KB |
answer = YES |
43 |
Correct |
5 ms |
2832 KB |
answer = YES |
44 |
Correct |
6 ms |
2900 KB |
answer = YES |
45 |
Correct |
3 ms |
2900 KB |
answer = YES |
46 |
Correct |
3 ms |
2772 KB |
answer = YES |
47 |
Correct |
3 ms |
2900 KB |
answer = YES |
48 |
Correct |
3 ms |
2900 KB |
answer = YES |
49 |
Correct |
52 ms |
5120 KB |
answer = YES |
50 |
Correct |
19 ms |
5496 KB |
answer = YES |
51 |
Correct |
40 ms |
5580 KB |
answer = YES |
52 |
Correct |
14 ms |
5448 KB |
answer = NO |
53 |
Correct |
5 ms |
2900 KB |
answer = YES |
54 |
Correct |
11 ms |
3284 KB |
answer = YES |
55 |
Correct |
25 ms |
3928 KB |
answer = YES |
56 |
Correct |
50 ms |
5140 KB |
answer = YES |
57 |
Correct |
63 ms |
4904 KB |
answer = YES |
58 |
Correct |
39 ms |
4884 KB |
answer = YES |
59 |
Correct |
17 ms |
4788 KB |
answer = YES |
60 |
Correct |
49 ms |
5004 KB |
answer = YES |
61 |
Correct |
10 ms |
4052 KB |
answer = YES |
62 |
Correct |
17 ms |
5224 KB |
answer = NO |
63 |
Correct |
567 ms |
39692 KB |
answer = YES |
64 |
Correct |
487 ms |
39468 KB |
answer = NO |
65 |
Correct |
530 ms |
39612 KB |
answer = YES |
66 |
Correct |
9 ms |
3156 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
3 ms |
2644 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Correct |
9 ms |
2644 KB |
answer = YES |
6 |
Correct |
2 ms |
2644 KB |
answer = YES |
7 |
Correct |
2 ms |
2644 KB |
answer = YES |
8 |
Correct |
2 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
2 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
2 ms |
2644 KB |
answer = NO |
13 |
Correct |
2 ms |
2696 KB |
answer = YES |
14 |
Correct |
2 ms |
2700 KB |
answer = YES |
15 |
Correct |
2 ms |
2700 KB |
answer = YES |
16 |
Correct |
2 ms |
2644 KB |
answer = YES |
17 |
Correct |
2 ms |
2644 KB |
answer = YES |
18 |
Correct |
2 ms |
2644 KB |
answer = YES |
19 |
Correct |
2 ms |
2700 KB |
answer = YES |
20 |
Correct |
2 ms |
2644 KB |
answer = YES |
21 |
Correct |
2 ms |
2644 KB |
answer = YES |
22 |
Correct |
2 ms |
2644 KB |
answer = NO |
23 |
Correct |
2 ms |
2700 KB |
answer = NO |
24 |
Correct |
2 ms |
2644 KB |
answer = YES |
25 |
Correct |
2 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
2 ms |
2644 KB |
answer = YES |
28 |
Correct |
2 ms |
2644 KB |
answer = YES |
29 |
Correct |
2 ms |
2644 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 |
4 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
2 ms |
2644 KB |
answer = YES |
36 |
Correct |
2 ms |
2644 KB |
answer = YES |
37 |
Correct |
3 ms |
2700 KB |
answer = YES |
38 |
Correct |
2 ms |
2644 KB |
answer = YES |
39 |
Correct |
4 ms |
2772 KB |
answer = YES |
40 |
Correct |
3 ms |
2972 KB |
answer = YES |
41 |
Correct |
3 ms |
2900 KB |
answer = NO |
42 |
Correct |
5 ms |
2900 KB |
answer = YES |
43 |
Correct |
5 ms |
2832 KB |
answer = YES |
44 |
Correct |
6 ms |
2900 KB |
answer = YES |
45 |
Correct |
3 ms |
2900 KB |
answer = YES |
46 |
Correct |
3 ms |
2772 KB |
answer = YES |
47 |
Correct |
3 ms |
2900 KB |
answer = YES |
48 |
Correct |
3 ms |
2900 KB |
answer = YES |
49 |
Correct |
52 ms |
5120 KB |
answer = YES |
50 |
Correct |
19 ms |
5496 KB |
answer = YES |
51 |
Correct |
40 ms |
5580 KB |
answer = YES |
52 |
Correct |
14 ms |
5448 KB |
answer = NO |
53 |
Correct |
5 ms |
2900 KB |
answer = YES |
54 |
Correct |
11 ms |
3284 KB |
answer = YES |
55 |
Correct |
25 ms |
3928 KB |
answer = YES |
56 |
Correct |
50 ms |
5140 KB |
answer = YES |
57 |
Correct |
63 ms |
4904 KB |
answer = YES |
58 |
Correct |
39 ms |
4884 KB |
answer = YES |
59 |
Correct |
17 ms |
4788 KB |
answer = YES |
60 |
Correct |
49 ms |
5004 KB |
answer = YES |
61 |
Correct |
10 ms |
4052 KB |
answer = YES |
62 |
Correct |
17 ms |
5224 KB |
answer = NO |
63 |
Correct |
567 ms |
39692 KB |
answer = YES |
64 |
Correct |
487 ms |
39468 KB |
answer = NO |
65 |
Correct |
530 ms |
39612 KB |
answer = YES |
66 |
Correct |
9 ms |
3156 KB |
answer = YES |
67 |
Correct |
471 ms |
34792 KB |
answer = YES |
68 |
Correct |
479 ms |
34736 KB |
answer = YES |
69 |
Correct |
154 ms |
34608 KB |
answer = YES |
70 |
Correct |
248 ms |
58868 KB |
answer = YES |
71 |
Correct |
162 ms |
34624 KB |
answer = YES |
72 |
Execution timed out |
1086 ms |
26508 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |