Submission #1299349

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

struct Line {
	int a, b;
};

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

const int N = 1e5, S = 1e8;

Line a[N+5];

Edge e[2*N+5];

bool ex[N+5], check;

vector<int> adj[N+5], val[N+5];

int id[N+5], mid[N+5], X[N+5], cur;

void dfs(int v) {
	id[v] = cur;
	
	Line l[2];
	
	for (int x = 1; x <= 2; x++) {
		l[x-1] = {-a[v].a, x*S-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 && !ex[cur]) {
			X[cur] = (e[x].w*S-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);
		
		if (check) {
			cout << "NO\n";
			
			return;
		}
	}
	
	for (int x = 1; x <= n; x++) {
		if (ex[id[x]]) {	
			a[x].b = a[x].a*X[id[x]]+a[x].b;
		}
		else {
			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());
		
		mid[x] = val[x][val[x].size()/2];
	}
	
	for (int x = 1; x <= n; x++) {
		if (!ex[id[x]]) {
			a[x].b += a[x].a*mid[id[x]];
		}
	}
	
	for (int x = 1; x <= m; x++) {
		int s = a[e[x].u].b/100+a[e[x].v].b/100; 
		
		if (abs(s-e[x].w*S/100) > 1) {
			cout << "NO\n";
			
			return;
		}
	}
	
	cout << "YES\n";
	
	for (int x = 1; x <= n; x++) {
		if (a[x].b < 0) {
			a[x].b *= -1;
			
			cout << "-";
		}
		
		cout << a[x].b/S << "." << a[x].b%S << " ";
	}
	
	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:131:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |                 freopen(task".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:132:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |                 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...