Submission #272383

#TimeUsernameProblemLanguageResultExecution timeMemory
272383arnold518Graph (BOI20_graph)C++14
100 / 100
591 ms19824 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N, M;
vector<pii> adj[MAXN+10];

bool vis[MAXN+10];
pii A[MAXN+10];

pii operator + (const pii &p, const pii &q) { return {p.first+q.first, p.second+q.second}; }
pii operator - (const pii &p, const pii &q) { return {p.first-q.first, p.second-q.second}; }

bool flag=true, chk;
long double val, ans[MAXN+10];
vector<int> V;

long double ffabs(long double x)
{
	if(x<0) return -x;
	return x;
}

void dfs(int now)
{
	vis[now]=true; V.push_back(now);
	for(auto nxt : adj[now])
	{
		if(vis[nxt.first])
		{
			pii t=A[now]+A[nxt.first];
			if(t.first==0)
			{
				if(t.second!=nxt.second) flag=false;
			}
			else
			{
				long double p=((long double)nxt.second-(long double)t.second)/t.first;
				if(!chk) chk=true, val=p;
				else if(ffabs(p-val)>1e-10) flag=false;
			}
		}
		else
		{
			A[nxt.first]=pii(0, nxt.second)-A[now];
			dfs(nxt.first);
		}
	}
}

long double f(long double x)
{
	long double ret=0;
	for(auto it : V) ret+=ffabs(A[it].first*x+A[it].second);
	return ret;
}

int main()
{
	scanf("%d%d", &N, &M);
	for(int i=1; i<=M; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}

	for(int i=1; i<=N; i++)
	{
		if(vis[i]) continue;
		V.clear(); chk=false;
		A[i]=pii(1, 0); dfs(i);
		if(chk)
		{
			for(auto it : V) ans[it]=A[it].first*val+A[it].second;
		}
		else
		{
			long double lo=-1e6, hi=1e6;
			for(int j=0; j<300; j++)
			{
				long double m1=(lo*2+hi)/3, m2=(lo+hi*2)/3;
				if(f(m1)<=f(m2)) hi=m2;
				else lo=m1;
			}

			for(auto it : V) ans[it]=A[it].first*lo+A[it].second;
		}
	}
	if(!flag) printf("NO\n");
	else
	{
		printf("YES\n");
		for(int i=1; i<=N; i++) printf("%.20f ", (double)ans[i]);
		printf("\n");
	}
}

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
Graph.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |   scanf("%d%d%d", &u, &v, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...