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;
using ll = long long;
const ll maxn=1e5+10;
vector<pair<ll,ll> > g[maxn];
bool moze=true;
bool vis[maxn];
ll val[maxn];
ll sgn[maxn];
double res[maxn];
vector<int> comp;
set<ll> a;
void dfs(ll node){
comp.push_back(node);
vis[node]=true;
for(auto i:g[node]){
if(vis[i.first]){
if(sgn[i.first]!=sgn[node] && val[i.first]+val[node]!=i.second) moze=false;
if(sgn[i.first]==sgn[node]){
if(sgn[i.first]==1) a.emplace(i.second-val[node]-val[i.first]);
else a.emplace(val[i.first]+val[node]-i.second);
}
}else{
sgn[i.first]=-sgn[node];
val[i.first]=i.second-val[node];
dfs(i.first);
}
}
}
int main(){
ll n,m;
cin>>n>>m;
for(ll i=0;i<m;i++){
ll u,v,c;
cin>>u>>v>>c;
g[u].push_back({v,c});
g[v].push_back({u,c});
}
for(int i=1;i<=n;i++){
if(vis[i]) continue;
sgn[i]=1;
val[i]=0;
comp.clear();
a.clear();
dfs(i);
if(a.size()>1) moze=false;
if(a.size()==1){
double aa=*a.begin();
for(ll i:comp){
res[i]=aa*sgn[i]/2+val[i];
}
continue;
}
pair<ll,int> best={LLONG_MAX,0};
vector<int> kandidati;
for(ll i:comp) kandidati.push_back(-val[i]*sgn[i]);
sort(kandidati.begin(),kandidati.end());
vector<int> poz;
vector<int> neg;
for(int i:comp){
if(sgn[i]>0) poz.push_back(val[i]);
else neg.push_back(val[i]);
}
sort(poz.begin(),poz.end(),greater<int>());
sort(neg.begin(),neg.end());
ll pozl=0;
ll sumpoz=0;
ll negl=0;
ll sumneg=0;
for(int i:poz) sumpoz+=i;
for(int i:neg) sumneg+=i;
int ipoz=0;
int ineg=0;
for(int i:kandidati){
while(ipoz<poz.size() && poz[ipoz]+i>=0) {pozl+=poz[ipoz]; ipoz++;}
while(ineg<neg.size() && neg[ineg]-i<=0) {negl+=neg[ineg]; ineg++;}
ll sum=0;
sum+=pozl+ipoz*i;
sum-=(sumpoz-pozl)+(poz.size()-ipoz)*i;
sum-=negl-ineg*i;
sum+=(sumneg-negl)-(neg.size()-ineg)*i;
best=min(best,{sum,i});
}
for(ll i:comp){
res[i]=val[i]+best.second*sgn[i];
}
}
if(moze){
cout<<"YES\n";
cout<<fixed<<showpoint<<setprecision(15);
for(int i=1;i<=n;i++){
cout<<res[i];
cout<<" ";
}
}else cout<<"NO\n";
return 0;
}
Compilation message (stderr)
Graph.cpp: In function 'int main()':
Graph.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | while(ipoz<poz.size() && poz[ipoz]+i>=0) {pozl+=poz[ipoz]; ipoz++;}
| ~~~~^~~~~~~~~~~
Graph.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | while(ineg<neg.size() && neg[ineg]-i<=0) {negl+=neg[ineg]; ineg++;}
| ~~~~^~~~~~~~~~~
# | 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... |