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 <bits/stdc++.h>
using namespace std;
int n,m;
vector<pair<int,double> > v[100001];
void read()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
int a,b,c;
cin>>a>>b>>c;
v[a].push_back({b,c});
v[b].push_back({a,c});
}
}
vector<int> curr;
double sum,ans;
int pos;
double c[200001];
int used[200001];
void dfs(int i)
{
//cout<<i<<" "<<c[i]<<endl;
used[i]=1;
curr.push_back(i);
sum+=c[i];
for(int j=0; j<v[i].size(); j++)
{
pair<int,int> nb=v[i][j];
int x=nb.first;
double y=nb.second;
if(used[x]&&c[x]+c[i]!=y)pos=0;
else if(!used[x])
{
if(y==c[i])c[x]=0;
else
{
if(y-c[i]<0.000001&&y-c[i]>-0.000001)c[x]=0;
else c[x]=y-c[i];
}
dfs(x);
}
}
}
double f[100001];
void try_(int i,double val)
{
//cout<<endl;
for(int j=0; j<curr.size(); j++)
{
used[curr[j]]=0;
}
curr.clear();
//cout<<val<<endl;
pos=1;
sum=0;
c[i]=val;
dfs(i);
if(pos)ans=min(ans,sum);
for(int j=0; j<curr.size(); j++)
{
if(ans==sum&&pos)f[curr[j]]=c[curr[j]];
}
}
void solve()
{
for(int i=1; i<=n; i++)
{
if(!used[i])
{
ans=1000000000;
double x=0.000001;
for(double val=-2;val<0;val=val+x)
{
try_(i,val);
}
try_(i,0);
for(double val=0.000001;val<=2;val=val+x)
{
try_(i,val);
}
if(ans==1000000000)
{
cout<<"NO"<<endl;
exit(0);
}
}
}
//cout<<ans<<endl;
cout<<"YES"<<endl;
for(int i=1; i<=n; i++)
cout<<f[i]<<" ";
cout<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
read();
solve();
return 0;
}
Compilation message (stderr)
Graph.cpp: In function 'void dfs(int)':
Graph.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, double> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int j=0; j<v[i].size(); j++)
| ~^~~~~~~~~~~~
Graph.cpp: In function 'void try_(int, double)':
Graph.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int j=0; j<curr.size(); j++)
| ~^~~~~~~~~~~~
Graph.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int j=0; j<curr.size(); j++)
| ~^~~~~~~~~~~~
# | 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... |