This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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, components;
vector<pair<int, int>> G[NMAX + 1];
bool visited[NMAX + 1];
bool ok;
long double unique_solution;
vector<int> component[NMAX + 1];
long double answer[NMAX + 1];
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;
component[components].push_back(node);
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, vector<int> &a) {
long double answer = 0;
for (int node : a) {
answer += abs(values[node].a * x + values[node].b);
}
return answer;
}
void GetAnswer(vector<int> &a) {
if (unique_solution != INF) {
for(int node : a) {
answer[node] = values[node].a * unique_solution + values[node].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, a);
long double value2 = GetValue(mid2, a);
if (value1 > value2) {
left = mid1;
}
else {
right = mid2;
}
}
for(int node : a) {
answer[node] = values[node].a * left + values[node].b;
}
}
}
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 });
}
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
components++;
ok = true;
unique_solution = INF;
DFS(i, make_value(1, 0));
if(!ok) {
cout << "NO" << '\n';
return 0;
}
GetAnswer(component[components]);
}
}
cout << "YES" << '\n';
for(int i = 1; i <= n; i++) {
cout << fixed << setprecision(6) << answer[i] << ' ';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |