#include <bits/stdc++.h>
using namespace std;
#define ll long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return uniform_int_distribution<ll>(l, r)(rng);
}
const ll INF = 1e18;
const ll mod = 1e9 + 22071997;
ll binpow(ll base, ll exp, ll mod) {
ll ans = 1;
ll mult = base;
while(exp) {
if(exp & 1) ans = (ans * mult) % mod;
mult = (mult * mult) % mod;
exp >>= 1;
}
return ans;
}
ll gcd(ll a, ll b) {
if(b == 0) return a;
return gcd(b, a%b);
}
struct LinearEq {
double x, c;
LinearEq(double _x = 0, double _c = 0) : x(_x), c(_c) {
}
LinearEq operator + (const LinearEq& other) {
return LinearEq(x + other.x, c + other.c);
}
LinearEq operator - (const LinearEq& other) {
return LinearEq(x - other.x, c - other.c);
}
};
const int N = 1e5 + 5;
int n, m;
vector<pair<int, double>> adj[N];
bool vis[N];
LinearEq eq[N];
double val[N];
void gather_nodes(vector<int>& nodes, int node) {
vis[node] = true;
nodes.push_back(node);
for(pair<int, double> v : adj[node]) {
if(vis[v.first]) continue;
gather_nodes(nodes, v.first);
}
}
void spread_equation(int node) {
vis[node] = true;
for(pair<int, double> v : adj[node]) {
if(!vis[v.first]) {
eq[v.first] = LinearEq(0, v.second) - eq[node];
spread_equation(v.first);
}
}
}
void gather_equations(vector<LinearEq>& equations, int node) {
vis[node] = true;
for(pair<int, double> v : adj[node]) {
equations.push_back(eq[node] + eq[v.first] - LinearEq(0, v.second));
if(!vis[v.first]) {
gather_equations(equations, v.first);
}
}
}
void spread_value(double x, int node) {
val[node] = x * eq[node].x + eq[node].c;
vis[node] = true;
for(pair<int, double> v : adj[node]) {
if(!vis[v.first]) spread_value(x, v.first);
}
}
bool valid_for_whole_graph = true;
void solve_component(int node) {
vector<int> nodes;
gather_nodes(nodes, node);
for(int i : nodes) {
vis[i] = false;
}
eq[node] = LinearEq(1, 0);
spread_equation(node);
for(int i : nodes) {
vis[i] = false;
}
vector<LinearEq> equations;
gather_equations(equations, node);
vector<double> solutions;
//cout << "System of equations: \n";
for(LinearEq& e : equations) {
//cout << e.x << "x + " << e.c << " = 0\n";
if(fabs(e.x) <= 1e-9) {
if(fabs(e.c) > 1e-9) {
valid_for_whole_graph = false;
return;
}
}
else {
solutions.push_back(-e.c / e.x);
}
}
if(solutions.size() >= 1) {
sort(solutions.begin(), solutions.end());
solutions.erase(unique(solutions.begin(), solutions.end()), solutions.end());
}
double x;
if(solutions.size() >= 2) {
valid_for_whole_graph = false;
return;
}
else if(solutions.size() == 1) {
x = solutions.front();
} else {
// Get the median of all solutions
vector<double> candidates;
for(int i : nodes) {
candidates.push_back(-eq[i].c / eq[i].x);
}
//cout << "HI\n" << flush;
sort(candidates.begin(), candidates.end());
int id = (int)candidates.size() / 2;
x = candidates[id];
}
for(int i : nodes) {
vis[i] = false;
}
spread_value(x, node);
}
void solve() {
memset(vis, 0, sizeof(vis));
cin >> n >> m;
cout << fixed << setprecision(8);
for(int i = 0; i < m; i++) {
int a, b, t;
cin >> a >> b >> t;
adj[a].push_back({b, t});
adj[b].push_back({a, t});
}
for(int i = 1; i <= n; i++) {
if(vis[i]) continue;
solve_component(i);
if(!valid_for_whole_graph) break;
}
if(!valid_for_whole_graph) {
cout << "NO\n";
return;
}
cout << "YES\n";
for(int i = 1; i <= n; i++) cout << val[i] << " ";
}
int main() {
//freopen("COLLECT.INP", "r", stdin);
//freopen("COLLECT.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tests = 1;
//cin >> tests;
while(tests--) solve();
}
| # | 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... |