제출 #722741

#제출 시각아이디문제언어결과실행 시간메모리
722741gagik_2007Graph (BOI20_graph)C++17
100 / 100
308 ms29300 KiB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;

typedef long long ll;
typedef long double ld;

#define ff first
#define ss second

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 200007;
const ll M = 207;
const ll LG = 31;
ll n, m, k;
vector<pair<int, int>>g[N];
bool used[N];
bool neg[N]; // if the x in the value formula has the negative sign
ll val[N]; // if neg[v]: value = val[v] - x, else: value = x + val[v]
bool defined_value[N];
ll vlx2[N];
bool is_root[N];
vector<int>cmp[N];

void dfs(int v, int par, int root) {
	//cout << v << endl;
	cmp[root].push_back(v);
	for (auto e : g[v]) {
		int to = e.ff;
		int c = e.ss;
		if (to != par) {
			if (!used[to]) {
				if (neg[v]) {
					val[to] = c - val[v];
				}
				else {
					neg[to] = true;
					val[to] = c - val[v];
				}
				used[to] = true;
				dfs(to, v, root);
			}
			else {
				if (neg[v] == neg[to]) {
					if (neg[v]) {
						if (!defined_value[root]) {
							vlx2[root] = val[v] + val[to] - c;
							defined_value[root] = true;
							//cout << "Defined: " << vlx2[root] << endl;
						}
						else if (vlx2[root] != val[v] + val[to] - c) {
							cout << "NO\n";
							exit(0);
						}
					}
					else {
						if (!defined_value[root]) {
							vlx2[root] = c - val[v] - val[to];
							defined_value[root] = true;
							//cout << "Defined: " << vlx2[root] << endl;
						}
						else if (vlx2[root] != c - val[v] - val[to]) {
							cout << "NO\n";
							exit(0);
						}
					}
				}
				else {
					if (val[v] + val[to] != c) {
						cout << "NO\n";
						exit(0);
					}
				}
			}
		}
	}
}

ll get_sum(ll curvlx2, int root) {
	//cout << "PROCESSING: " << curvlx2 << endl;
	ll sum = 0;
	for (int v : cmp[root]) {
		ll vl = 0;
		if (neg[v]) {
			vl = val[v] * 2 - curvlx2;
		}
		else {
			vl = val[v] * 2 + curvlx2;
		}
		//cout << vl << " ";
		sum += abs(vl);
	}
	//cout << endl;
	return sum;
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y;
		int tp;
		cin >> x >> y >> tp;
		g[x].push_back({ y,tp });
		g[y].push_back({ x,tp });
	}
	for (int v = 1; v <= n; v++) {
		if (!used[v]) {
			used[v] = true;
			is_root[v] = true;
			dfs(v, -1, v);
		}
	}
	cout << setprecision(12) << fixed;
	cout << "YES\n";
	vector<ld>ans(n + 1, 0);
	for (int u = 1; u <= n; u++) {
		if (is_root[u]) {
			if (defined_value[u]) {
				for (int v : cmp[u]) {
					ld vl = 0;
					if (neg[v]) {
						vl = val[v] - ld(vlx2[u]) / 2.0;
					}
					else {
						vl = val[v] + ld(vlx2[u]) / 2.0;
					}
					ans[v] = vl;
				}
			}
			else {
				pair<ll, ll>mn = { INF,INF };
				for (ll cur = -M; cur <= M; cur++) {
					mn = min(mn, { get_sum(cur,u),cur });
				}
				vlx2[u] = mn.ss;
				for (int v : cmp[u]) {
					ld vl = 0;
					if (neg[v]) {
						vl = val[v] - ld(vlx2[u]) / 2.0;
					}
					else {
						vl = val[v] + ld(vlx2[u]) / 2.0;
					}
					ans[v] = vl;
				}
			}
		}
	}
	for (int v = 1; v <= n; v++) {
		cout << ans[v] << " ";
	}
	cout << endl;
}

/// ---- - --------  ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - --------  ------ -------- -- - - -
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...