This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
using namespace std;
const int MAXN=1e5;
const double INF=1e9;
vector<pair<int, int> > adj[MAXN];
vector<int> seg;
double val[MAXN], ans[MAXN];
bool v[MAXN], s[MAXN];
void DFS(int idx)
{
v[idx]=true;
seg.push_back(idx);
for(pair<int, int> node:adj[idx])
{
if(!v[node.first])
{
val[node.first]=node.second-val[idx];
s[node.first]=!s[idx];
DFS(node.first);
}
}
return;
}
double GetValue(double x1, bool s1, double x2, bool s2)
{
if(s1==s2)
{
if(x1-x2==0)
{
return INF;
}
return -INF;
}
if(s1)
{
return -(x1-x2)/2;
}
return (x1-x2)/2;
}
double GetSum(double x)
{
double res=0;
for(int node:seg)
{
if(s[node])
{
res+=abs(val[node]+x);
}
else
{
res+=abs(val[node]-x);
}
}
return res;
}
bool AssignValues(int idx)
{
seg.clear();
val[idx]=0;
s[idx]=true;
DFS(idx);
double x=INF;
for(int node:seg)
{
for(pair<int, int> u:adj[node])
{
double temp=GetValue(val[u.first], s[u.first], u.second-val[node], !s[node]);
if(temp==-INF)
{
return false;
}
if(temp==INF)
{
continue;
}
if(x==INF)
{
x=temp;
}
else if(x!=temp)
{
return false;
}
}
}
if(x==INF)
{
double res=INF;
for(double i=-100; i<100; i+=0.5)
{
if(GetSum(i)<res)
{
x=i;
res=GetSum(i);
}
}
}
for(int node:seg)
{
if(s[node])
{
ans[node]=val[node]+x;
}
else
{
ans[node]=val[node]-x;
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i=0; i<m; i++)
{
int a, b, x;
cin >> a >> b >> x;
a--, b--;
adj[a].push_back({b, x});
adj[b].push_back({a, x});
}
for(int i=0; i<n; i++)
{
val[i]=0;
v[i]=false;
}
for(int i=0; i<n; i++)
{
if(!v[i] && !AssignValues(i))
{
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
for(int i=0; i<n; i++)
{
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |