Submission #917375

#TimeUsernameProblemLanguageResultExecution timeMemory
917375NK_Graph (BOI20_graph)C++17
100 / 100
183 ms38092 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using db = long double;
 
const int INF = 1e9;
const int B = 1e8;
const db EPS = 1e-9;
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M; cin >> N >> M;
 
	V<vpi> adj(N); for(int i = 0; i < M; i++) {
		int u, v, w; cin >> u >> v >> w; --u, --v;
		adj[u].pb(mp(v, w));
		adj[v].pb(mp(u, w));
	}
 
 
	auto C = [&](pi p, int w) {
		// cout << p.f << " " << p.s << " " << w << endl;
		return mp(w - p.f, -p.s);
	};
 
	cout << fixed << setprecision(10);
 
	vi vis(N), COMP; V<set<pi>> S(N);
	function<void(int)> dfs = [&](int u) {
		vis[u] = 1; COMP.pb(u);
		pi p = *begin(S[u]);
		// cout << u << " => " << p.f << " " << p.s << endl;
		for(auto& e : adj[u]) {
			int v, w; tie(v, w) = e;
			// cout << v << " " << w << endl;
 
			S[v].insert(C(p, w));
			if (!vis[v]) dfs(v);
		}
	};
 
	V<db> ans(N, INF); 
	function<void(int)> get = [&](int u) {
		// cout << u << endl;
		for(auto& e : adj[u]) {
			int v, w; tie(v, w) = e;
			// cout << u << " => " << v << endl;
			// cout << ans[u] << " -> " << ans[v] << endl;
			db x = w - ans[u];
			if (ans[v] > B) {
				ans[v] = x; get(v);
			} else {
				if (abs(ans[v] - x) > EPS) {
					cout << "NO" << nl;
					exit(0-0);
				}
			}	
		}
	};
 
	for(int r = 0; r < N; r++) if (!vis[r]) {
		COMP = {}; S[r].insert(mp(0, 1)); dfs(r);
		// cout << endl << endl;
 
		db x = INF; int R = -1; vi roots;
		for(auto& i : COMP) {
			if (sz(S[i]) >= 2) {
				// cout << i << endl;
				vi pos, neg; for(auto& p : S[i]) {
					// cout << p.f << " " << p.s << endl;
					if (p.s == -1) neg.pb(p.f);
					else pos.pb(p.f);
				}
	 
				if (max(sz(pos), sz(neg)) > 1) {
					cout << "NO" << nl;
					exit(0-0);
				}
	 
				x = db(pos.back() + neg.back()) / db(2);
				// cout << x << endl;
				R = i;
				// cout << "DONE" << endl;
				break;
			} else {
				pi p = *begin(S[i]);
				roots.pb(p.f * p.s * -1);
			}
		}

		if (R == -1) {
			sort(begin(roots), end(roots));
			// for(auto& c : roots) cout << c << " ";
			// cout << endl;
			R = r; x = roots[sz(roots) / 2];
		}
 
		ans[R] = x; get(R);
	}
 
	cout << "YES" << nl;
	for(auto& x : ans) cout << x << " ";
	cout << nl;
	
	exit(0-0);
}
#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...