#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
vector<vector<pair<int,int>>> adj;
vector<bool> processed;
double merge(pair<int,int> a, pair<int,int> b) {
double nb = a.fi - b.fi;
double nbX = b.se - a.se;
if (!nbX) {
if (!nb) return INT_MAX;
else return INT_MIN;
}
return nb / nbX;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M; cin >> N >> M;
adj.resize(N);
for (int i = 0; i < M; i++) {
int a, b, c; cin >> a >> b >> c;
a--, b--;;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
vector<double> ans(N, INT_MAX);
vector<pair<int,int>> value(N, {INT_MAX, INT_MAX});
processed.resize(N, false);
for (int i = 0; i < N; i++) {
if (processed[i]) continue;
vector<int> in = {i};
double equation = INT_MAX;
queue<int> q; q.push(i);
processed[i] = true;
value[i] = {0, 1};
while (!q.empty()) {
int x = q.front(); q.pop();
for (auto [y, c] : adj[x]) {
if (processed[y]) {
double sol = merge(value[y], {c - value[x].fi, -value[x].se});
if (sol == INT_MIN || (sol != INT_MAX && equation != sol && equation != INT_MAX)) {
cout << "NO\n";
return 0;
}
else if (sol != INT_MAX) equation = sol;
}
else {
processed[y] = true;
in.push_back(y);
value[y] = {c - value[x].fi, -value[x].se};
q.push(y);
}
}
}
if (equation == INT_MAX) {
double l = -1000000.0, r = 1000000.0;
double best, sum = INT_MAX;
while (r - l > 0.000001) {
double mid1 = l + (r - l) / 3.0, mid2 = r - (r - l) / 3.0;
double res1 = 0.0, res2 = 0.0;
for (int x : in) {
res1 += abs(value[x].fi + value[x].se * mid1);
res2 += abs(value[x].fi + value[x].se * mid2);
}
if (res1 < res2) {
r = mid2;
if (res1 < sum) {best = mid1; sum = res1;}
}
else {
l = mid1;
if (res2 < sum) {best = mid2;sum = res2;}
}
}
equation = best;
}
for (int x : in) ans[x] = value[x].fi + equation * value[x].se;
}
cout << "YES\n";
for (double x : ans) {
cout << setprecision(6) << fixed << x << " ";
}
return 0;
}
Compilation message
Graph.cpp: In function 'int main()':
Graph.cpp:87:58: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | for (int x : in) ans[x] = value[x].fi + equation * value[x].se;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
452 KB |
answer = YES |
4 |
Correct |
0 ms |
348 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
0 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
1 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
452 KB |
answer = YES |
4 |
Correct |
0 ms |
348 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
0 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
1 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
0 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
0 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
384 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
452 KB |
answer = YES |
4 |
Correct |
0 ms |
348 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
0 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
1 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
0 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
0 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
384 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
37 |
Correct |
1 ms |
348 KB |
answer = YES |
38 |
Correct |
0 ms |
348 KB |
answer = YES |
39 |
Correct |
1 ms |
344 KB |
answer = YES |
40 |
Correct |
1 ms |
348 KB |
answer = YES |
41 |
Correct |
0 ms |
348 KB |
answer = NO |
42 |
Correct |
1 ms |
348 KB |
answer = YES |
43 |
Correct |
1 ms |
348 KB |
answer = YES |
44 |
Correct |
1 ms |
348 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
1 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
348 KB |
answer = YES |
48 |
Correct |
1 ms |
348 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
452 KB |
answer = YES |
4 |
Correct |
0 ms |
348 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
0 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
1 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
0 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
0 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
384 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
37 |
Correct |
1 ms |
348 KB |
answer = YES |
38 |
Correct |
0 ms |
348 KB |
answer = YES |
39 |
Correct |
1 ms |
344 KB |
answer = YES |
40 |
Correct |
1 ms |
348 KB |
answer = YES |
41 |
Correct |
0 ms |
348 KB |
answer = NO |
42 |
Correct |
1 ms |
348 KB |
answer = YES |
43 |
Correct |
1 ms |
348 KB |
answer = YES |
44 |
Correct |
1 ms |
348 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
1 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
348 KB |
answer = YES |
48 |
Correct |
1 ms |
348 KB |
answer = YES |
49 |
Correct |
7 ms |
1372 KB |
answer = YES |
50 |
Correct |
10 ms |
1372 KB |
answer = YES |
51 |
Correct |
6 ms |
1400 KB |
answer = YES |
52 |
Correct |
3 ms |
1116 KB |
answer = NO |
53 |
Correct |
2 ms |
348 KB |
answer = YES |
54 |
Correct |
2 ms |
604 KB |
answer = YES |
55 |
Correct |
3 ms |
860 KB |
answer = YES |
56 |
Correct |
8 ms |
1440 KB |
answer = YES |
57 |
Correct |
7 ms |
1444 KB |
answer = YES |
58 |
Correct |
11 ms |
1372 KB |
answer = YES |
59 |
Correct |
9 ms |
1368 KB |
answer = YES |
60 |
Correct |
7 ms |
1372 KB |
answer = YES |
61 |
Correct |
5 ms |
860 KB |
answer = YES |
62 |
Correct |
35 ms |
7252 KB |
answer = NO |
63 |
Correct |
44 ms |
7788 KB |
answer = YES |
64 |
Correct |
37 ms |
7740 KB |
answer = NO |
65 |
Correct |
40 ms |
7760 KB |
answer = YES |
66 |
Correct |
2 ms |
604 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
answer = YES |
2 |
Correct |
0 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
452 KB |
answer = YES |
4 |
Correct |
0 ms |
348 KB |
answer = NO |
5 |
Correct |
0 ms |
348 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
1 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
0 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
348 KB |
answer = YES |
17 |
Correct |
0 ms |
348 KB |
answer = YES |
18 |
Correct |
0 ms |
348 KB |
answer = YES |
19 |
Correct |
0 ms |
348 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
1 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
348 KB |
answer = NO |
24 |
Correct |
0 ms |
344 KB |
answer = YES |
25 |
Correct |
1 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
0 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
348 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
1 ms |
348 KB |
answer = YES |
34 |
Correct |
1 ms |
384 KB |
answer = YES |
35 |
Correct |
1 ms |
348 KB |
answer = YES |
36 |
Correct |
0 ms |
348 KB |
answer = YES |
37 |
Correct |
1 ms |
348 KB |
answer = YES |
38 |
Correct |
0 ms |
348 KB |
answer = YES |
39 |
Correct |
1 ms |
344 KB |
answer = YES |
40 |
Correct |
1 ms |
348 KB |
answer = YES |
41 |
Correct |
0 ms |
348 KB |
answer = NO |
42 |
Correct |
1 ms |
348 KB |
answer = YES |
43 |
Correct |
1 ms |
348 KB |
answer = YES |
44 |
Correct |
1 ms |
348 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
1 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
348 KB |
answer = YES |
48 |
Correct |
1 ms |
348 KB |
answer = YES |
49 |
Correct |
7 ms |
1372 KB |
answer = YES |
50 |
Correct |
10 ms |
1372 KB |
answer = YES |
51 |
Correct |
6 ms |
1400 KB |
answer = YES |
52 |
Correct |
3 ms |
1116 KB |
answer = NO |
53 |
Correct |
2 ms |
348 KB |
answer = YES |
54 |
Correct |
2 ms |
604 KB |
answer = YES |
55 |
Correct |
3 ms |
860 KB |
answer = YES |
56 |
Correct |
8 ms |
1440 KB |
answer = YES |
57 |
Correct |
7 ms |
1444 KB |
answer = YES |
58 |
Correct |
11 ms |
1372 KB |
answer = YES |
59 |
Correct |
9 ms |
1368 KB |
answer = YES |
60 |
Correct |
7 ms |
1372 KB |
answer = YES |
61 |
Correct |
5 ms |
860 KB |
answer = YES |
62 |
Correct |
35 ms |
7252 KB |
answer = NO |
63 |
Correct |
44 ms |
7788 KB |
answer = YES |
64 |
Correct |
37 ms |
7740 KB |
answer = NO |
65 |
Correct |
40 ms |
7760 KB |
answer = YES |
66 |
Correct |
2 ms |
604 KB |
answer = YES |
67 |
Correct |
64 ms |
9944 KB |
answer = YES |
68 |
Correct |
65 ms |
9756 KB |
answer = YES |
69 |
Correct |
48 ms |
9860 KB |
answer = YES |
70 |
Correct |
65 ms |
14280 KB |
answer = YES |
71 |
Correct |
50 ms |
9688 KB |
answer = YES |
72 |
Correct |
76 ms |
10304 KB |
answer = YES |
73 |
Correct |
61 ms |
10484 KB |
answer = YES |
74 |
Correct |
41 ms |
6516 KB |
answer = YES |
75 |
Correct |
22 ms |
6224 KB |
answer = NO |
76 |
Correct |
12 ms |
1684 KB |
answer = YES |
77 |
Correct |
16 ms |
2968 KB |
answer = YES |
78 |
Correct |
48 ms |
4740 KB |
answer = YES |
79 |
Correct |
87 ms |
9068 KB |
answer = YES |
80 |
Correct |
84 ms |
6604 KB |
answer = YES |
81 |
Correct |
25 ms |
8856 KB |
answer = NO |
82 |
Correct |
59 ms |
9800 KB |
answer = YES |
83 |
Correct |
63 ms |
10052 KB |
answer = YES |
84 |
Correct |
113 ms |
9796 KB |
answer = YES |
85 |
Correct |
66 ms |
9800 KB |
answer = YES |
86 |
Correct |
56 ms |
9804 KB |
answer = YES |
87 |
Correct |
29 ms |
9048 KB |
answer = NO |
88 |
Correct |
65 ms |
10008 KB |
answer = YES |
89 |
Correct |
55 ms |
9548 KB |
answer = YES |
90 |
Correct |
95 ms |
9624 KB |
answer = YES |
91 |
Correct |
56 ms |
9560 KB |
answer = YES |
92 |
Correct |
33 ms |
5508 KB |
answer = YES |
93 |
Correct |
27 ms |
5548 KB |
answer = YES |
94 |
Correct |
29 ms |
9428 KB |
answer = NO |
95 |
Correct |
27 ms |
8796 KB |
answer = NO |
96 |
Correct |
105 ms |
14244 KB |
answer = YES |
97 |
Correct |
29 ms |
9344 KB |
answer = NO |