#include <iostream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#define ll long long
using namespace std;
const int NMAX = 1e5;
const int INF = 1e9;
const long double EPS = 1e-6;
struct Value {
int a, b;
}values[NMAX + 1];
int n, m;
vector<pair<int, int>> G[NMAX + 1];
bool visited[NMAX + 1];
bool ok;
long double unique_solution;
int GCD(int a, int b) {
while (b) {
int r = a % b;
a = b;
b = r;
}
return a;
}
Value make_value(int a, int b) {
Value answer;
answer.a = a;
answer.b = b;
return answer;
}
void Simplify(pair<int, int> &a) {
int gcd = GCD(abs(a.first), abs(a.second));
a.first /= gcd;
a.second /= gcd;
}
bool GetSolution(Value x, Value y, int which) {
if (x.a + y.a == 0) {
return x.b + y.b == which;
}
pair<int, int> solution_x = { which - x.b - y.b, x.a + y.a };
if ((solution_x.first <= 0 && solution_x.second <= 0) || (solution_x.first >= 0 && solution_x.second <= 0)) {
solution_x.first = -solution_x.first;
solution_x.second = -solution_x.second;
}
Simplify(solution_x);
long double real_solution_x = (long double) solution_x.first / (long double) solution_x.second;
if(unique_solution == INF) {
unique_solution = real_solution_x;
return true;
}
return abs(unique_solution - real_solution_x) <= EPS;
}
void DFS(int node, Value curent_value) {
values[node] = curent_value;
visited[node] = 1;
for (auto [next_node, which] : G[node]) {
if (!visited[next_node]) {
DFS(next_node, make_value(-curent_value.a, which - curent_value.b));
}
else {
if(!GetSolution(values[node], values[next_node], which)) {
ok = false;
}
}
}
}
long double GetValue(long double x) {
long double answer = 0;
for (int i = 1; i <= n; i++) {
answer += abs(values[i].a * x + values[i].b);
}
return answer;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back({ b, c });
G[b].push_back({ a, c });
}
ok = true;
unique_solution = INF;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
DFS(i, make_value(1, 0));
}
}
if (ok) {
cout << "YES" << '\n';
if (unique_solution != INF) {
for (int i = 1; i <= n; i++) {
cout << fixed << setprecision(6) << values[i].a * unique_solution + values[i].b << ' ';
}
}
else {
long double left = -INF, right = INF;
int iterations = 400;
while (iterations--) {
long double mid1 = left + (right - left) / 3.0;
long double mid2 = right - (right - left) / 3.0;
long double value1 = GetValue(mid1);
long double value2 = GetValue(mid2);
if (value1 > value2) {
left = mid1;
}
else {
right = mid2;
}
}
for (int i = 1; i <= n; i++) {
cout << fixed << setprecision(6) << values[i].a * left + values[i].b << ' ';
}
}
}
else {
// assert(false);
cout << "NO" << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2656 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2652 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2652 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2788 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
2 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Incorrect |
1 ms |
2648 KB |
jury has the better answer: jans = YES, pans = NO |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2656 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2652 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2652 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2788 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
2 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Incorrect |
1 ms |
2648 KB |
jury has the better answer: jans = YES, pans = NO |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2656 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2652 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2652 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2788 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
2 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Incorrect |
1 ms |
2648 KB |
jury has the better answer: jans = YES, pans = NO |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2656 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2652 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2652 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2788 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
2 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Incorrect |
1 ms |
2648 KB |
jury has the better answer: jans = YES, pans = NO |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
answer = YES |
2 |
Correct |
1 ms |
2656 KB |
answer = YES |
3 |
Correct |
1 ms |
2652 KB |
answer = YES |
4 |
Correct |
1 ms |
2652 KB |
answer = NO |
5 |
Correct |
1 ms |
2652 KB |
answer = YES |
6 |
Correct |
1 ms |
2652 KB |
answer = YES |
7 |
Correct |
1 ms |
2652 KB |
answer = YES |
8 |
Correct |
1 ms |
2652 KB |
answer = YES |
9 |
Correct |
1 ms |
2652 KB |
answer = NO |
10 |
Correct |
1 ms |
2652 KB |
answer = YES |
11 |
Correct |
1 ms |
2788 KB |
answer = YES |
12 |
Correct |
1 ms |
2652 KB |
answer = NO |
13 |
Correct |
2 ms |
2648 KB |
answer = YES |
14 |
Correct |
1 ms |
2652 KB |
answer = YES |
15 |
Correct |
1 ms |
2652 KB |
answer = YES |
16 |
Correct |
1 ms |
2652 KB |
answer = YES |
17 |
Correct |
1 ms |
2652 KB |
answer = YES |
18 |
Correct |
1 ms |
2652 KB |
answer = YES |
19 |
Incorrect |
1 ms |
2648 KB |
jury has the better answer: jans = YES, pans = NO |
20 |
Halted |
0 ms |
0 KB |
- |