Submission #1299352

#TimeUsernameProblemLanguageResultExecution timeMemory
1299352floGraph (BOI20_graph)C++20
100 / 100
95 ms25472 KiB
#include <bits/stdc++.h>
#define task "testing"
#define ll long long
#define multitest 0
using namespace std;

struct Line {
	double a, b;
};

struct Edge {
	int u, v, w;
	
	bool vis;
}; 

const int N = 1e5;

const double eps = 1e-6;

Line a[N+5];

bool ex[N+5];

Edge e[2*N+5];

double X[N+5];

int id[N+5], cur;

vector<int> adj[N+5];

vector<double> val[N+5];

void dfs(int v) {
	id[v] = cur;
	
	Line l[2];
	
	for (int x = 1; x <= 2; x++) {
		l[x-1] = {-a[v].a, 1.0*x-a[v].b};
	}
	
	for (auto x : adj[v]) {
		if (e[x].vis) continue;
		
		e[x].vis = 1;
		
		int u = e[x].u^e[x].v^v;
		
		if (!id[u]) {
			a[u] = l[e[x].w-1], dfs(u);
		}
		else if (a[v].a*a[u].a == 1) {
			X[cur] = (1.0*e[x].w-a[u].b-a[v].b)/(2*a[v].a), ex[cur] = 1;
		}
	}
}

void flo(int ID) {
	int n, m; cin >> n >> m;
	
	for (int x = 1; x <= m; x++) {
		cin >> e[x].u >> e[x].v >> e[x].w;
		
		adj[e[x].u].push_back(x);
		adj[e[x].v].push_back(x);
		
		e[x].vis = 0;
	}
	
	for (int x = 1; x <= n; x++) {
		if (id[x]) continue;
		
		cur++, a[x] = {1, 0}, dfs(x);
	}
	
	for (int x = 1; x <= n; x++) {
		if (!ex[id[x]]) {
			val[id[x]].push_back(-a[x].a*a[x].b);
		}
	}
	
	for (int x = 1; x <= cur; x++) {
		if (val[x].empty()) continue;
		
		sort(val[x].begin(), val[x].end());
		
		X[x] = val[x][val[x].size()/2];
	}
	
	for (int x = 1; x <= n; x++) {
		a[x].b += a[x].a*X[id[x]];
	}
	
	for (int x = 1; x <= m; x++) {
		if (abs(a[e[x].u].b+a[e[x].v].b-e[x].w) > eps) {
			cout << "NO\n";
			
			return;
		}
	}
	
	cout << "YES\n";
	
	for (int x = 1; x <= n; x++) {
		cout << setprecision(10) << fixed << a[x].b << " ";
	}
	
	cout << "\n";
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

	int TCS = 1, ID = 1;

	if (multitest) {
		cin >> TCS;
	}

	while (TCS--) flo(ID++);

	return 0;
}

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:118:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |                 freopen(task".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:119:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |                 freopen(task".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...