#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);
}
}
}
ll range(int l,int r,vector<ll> &arr){
return arr[r]-(l>0 ? arr[l-1]:0);
}
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());
vector<ll> prefpoz;
vector<ll> prefneg;
partial_sum(poz.begin(),poz.end(),back_inserter(prefpoz));
partial_sum(neg.begin(),neg.end(),back_inserter(prefneg));
int ipoz=0;
int ineg=0;
for(int i:kandidati){
while(ipoz<poz.size() && poz[ipoz]+i>=0) ipoz++;
while(ineg<neg.size() && neg[ineg]-i<=0) ineg++;
ll sum=0;
sum+=(ipoz>0 ? prefpoz[ipoz-1]:0)+ipoz*i;
sum-=(ipoz!=poz.size() ? range(ipoz,poz.size()-1,prefpoz):0)+(poz.size()-ipoz)*i;
sum-=(ineg>0 ? prefneg[ineg-1]:0)-ineg*i;
sum+=(ineg!=neg.size() ? range(ineg,neg.size()-1,prefneg):0)-(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
Graph.cpp: In function 'int main()':
Graph.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | while(ipoz<poz.size() && poz[ipoz]+i>=0) ipoz++;
| ~~~~^~~~~~~~~~~
Graph.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | while(ineg<neg.size() && neg[ineg]-i<=0) ineg++;
| ~~~~^~~~~~~~~~~
Graph.cpp:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | sum-=(ipoz!=poz.size() ? range(ipoz,poz.size()-1,prefpoz):0)+(poz.size()-ipoz)*i;
| ~~~~^~~~~~~~~~~~
Graph.cpp:100:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | sum+=(ineg!=neg.size() ? range(ineg,neg.size()-1,prefneg):0)-(neg.size()-ineg)*i;
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
2 ms |
2644 KB |
answer = YES |
4 |
Correct |
1 ms |
2644 KB |
answer = NO |
5 |
Correct |
2 ms |
2732 KB |
answer = YES |
6 |
Correct |
1 ms |
2660 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
1 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
1 ms |
2672 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
1 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
1 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
1 ms |
2644 KB |
answer = YES |
21 |
Correct |
1 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2644 KB |
answer = NO |
23 |
Correct |
1 ms |
2644 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
2 ms |
2644 KB |
answer = YES |
4 |
Correct |
1 ms |
2644 KB |
answer = NO |
5 |
Correct |
2 ms |
2732 KB |
answer = YES |
6 |
Correct |
1 ms |
2660 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
1 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
1 ms |
2672 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
1 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
1 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
1 ms |
2644 KB |
answer = YES |
21 |
Correct |
1 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2644 KB |
answer = NO |
23 |
Correct |
1 ms |
2644 KB |
answer = NO |
24 |
Correct |
1 ms |
2644 KB |
answer = YES |
25 |
Correct |
1 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
1 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
1 ms |
2644 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
1 ms |
2644 KB |
answer = YES |
32 |
Correct |
1 ms |
2644 KB |
answer = YES |
33 |
Correct |
1 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
1 ms |
2644 KB |
answer = YES |
36 |
Correct |
1 ms |
2644 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
2 ms |
2644 KB |
answer = YES |
4 |
Correct |
1 ms |
2644 KB |
answer = NO |
5 |
Correct |
2 ms |
2732 KB |
answer = YES |
6 |
Correct |
1 ms |
2660 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
1 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
1 ms |
2672 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
1 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
1 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
1 ms |
2644 KB |
answer = YES |
21 |
Correct |
1 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2644 KB |
answer = NO |
23 |
Correct |
1 ms |
2644 KB |
answer = NO |
24 |
Correct |
1 ms |
2644 KB |
answer = YES |
25 |
Correct |
1 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
1 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
1 ms |
2644 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
1 ms |
2644 KB |
answer = YES |
32 |
Correct |
1 ms |
2644 KB |
answer = YES |
33 |
Correct |
1 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
1 ms |
2644 KB |
answer = YES |
36 |
Correct |
1 ms |
2644 KB |
answer = YES |
37 |
Correct |
1 ms |
2644 KB |
answer = YES |
38 |
Correct |
2 ms |
2604 KB |
answer = YES |
39 |
Correct |
2 ms |
2644 KB |
answer = YES |
40 |
Correct |
2 ms |
2772 KB |
answer = YES |
41 |
Correct |
2 ms |
2772 KB |
answer = NO |
42 |
Correct |
3 ms |
2772 KB |
answer = YES |
43 |
Correct |
2 ms |
2772 KB |
answer = YES |
44 |
Correct |
2 ms |
2772 KB |
answer = YES |
45 |
Correct |
2 ms |
2772 KB |
answer = YES |
46 |
Correct |
2 ms |
2644 KB |
answer = YES |
47 |
Correct |
3 ms |
2772 KB |
answer = YES |
48 |
Correct |
2 ms |
2772 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
2 ms |
2644 KB |
answer = YES |
4 |
Correct |
1 ms |
2644 KB |
answer = NO |
5 |
Correct |
2 ms |
2732 KB |
answer = YES |
6 |
Correct |
1 ms |
2660 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
1 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
1 ms |
2672 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
1 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
1 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
1 ms |
2644 KB |
answer = YES |
21 |
Correct |
1 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2644 KB |
answer = NO |
23 |
Correct |
1 ms |
2644 KB |
answer = NO |
24 |
Correct |
1 ms |
2644 KB |
answer = YES |
25 |
Correct |
1 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
1 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
1 ms |
2644 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
1 ms |
2644 KB |
answer = YES |
32 |
Correct |
1 ms |
2644 KB |
answer = YES |
33 |
Correct |
1 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
1 ms |
2644 KB |
answer = YES |
36 |
Correct |
1 ms |
2644 KB |
answer = YES |
37 |
Correct |
1 ms |
2644 KB |
answer = YES |
38 |
Correct |
2 ms |
2604 KB |
answer = YES |
39 |
Correct |
2 ms |
2644 KB |
answer = YES |
40 |
Correct |
2 ms |
2772 KB |
answer = YES |
41 |
Correct |
2 ms |
2772 KB |
answer = NO |
42 |
Correct |
3 ms |
2772 KB |
answer = YES |
43 |
Correct |
2 ms |
2772 KB |
answer = YES |
44 |
Correct |
2 ms |
2772 KB |
answer = YES |
45 |
Correct |
2 ms |
2772 KB |
answer = YES |
46 |
Correct |
2 ms |
2644 KB |
answer = YES |
47 |
Correct |
3 ms |
2772 KB |
answer = YES |
48 |
Correct |
2 ms |
2772 KB |
answer = YES |
49 |
Correct |
12 ms |
3772 KB |
answer = YES |
50 |
Correct |
13 ms |
4200 KB |
answer = YES |
51 |
Correct |
14 ms |
4268 KB |
answer = YES |
52 |
Correct |
8 ms |
4180 KB |
answer = NO |
53 |
Correct |
3 ms |
2772 KB |
answer = YES |
54 |
Correct |
4 ms |
2900 KB |
answer = YES |
55 |
Correct |
6 ms |
3280 KB |
answer = YES |
56 |
Correct |
12 ms |
3692 KB |
answer = YES |
57 |
Correct |
12 ms |
3656 KB |
answer = YES |
58 |
Correct |
11 ms |
3668 KB |
answer = YES |
59 |
Correct |
10 ms |
3468 KB |
answer = YES |
60 |
Correct |
12 ms |
3688 KB |
answer = YES |
61 |
Correct |
7 ms |
3156 KB |
answer = YES |
62 |
Correct |
137 ms |
12824 KB |
answer = NO |
63 |
Correct |
117 ms |
12844 KB |
answer = YES |
64 |
Correct |
107 ms |
12748 KB |
answer = NO |
65 |
Correct |
130 ms |
12884 KB |
answer = YES |
66 |
Correct |
3 ms |
2772 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
answer = YES |
2 |
Correct |
2 ms |
2644 KB |
answer = YES |
3 |
Correct |
2 ms |
2644 KB |
answer = YES |
4 |
Correct |
1 ms |
2644 KB |
answer = NO |
5 |
Correct |
2 ms |
2732 KB |
answer = YES |
6 |
Correct |
1 ms |
2660 KB |
answer = YES |
7 |
Correct |
1 ms |
2644 KB |
answer = YES |
8 |
Correct |
1 ms |
2644 KB |
answer = YES |
9 |
Correct |
2 ms |
2644 KB |
answer = NO |
10 |
Correct |
1 ms |
2644 KB |
answer = YES |
11 |
Correct |
2 ms |
2644 KB |
answer = YES |
12 |
Correct |
1 ms |
2644 KB |
answer = NO |
13 |
Correct |
1 ms |
2672 KB |
answer = YES |
14 |
Correct |
1 ms |
2644 KB |
answer = YES |
15 |
Correct |
1 ms |
2644 KB |
answer = YES |
16 |
Correct |
1 ms |
2644 KB |
answer = YES |
17 |
Correct |
1 ms |
2644 KB |
answer = YES |
18 |
Correct |
1 ms |
2644 KB |
answer = YES |
19 |
Correct |
1 ms |
2644 KB |
answer = YES |
20 |
Correct |
1 ms |
2644 KB |
answer = YES |
21 |
Correct |
1 ms |
2644 KB |
answer = YES |
22 |
Correct |
1 ms |
2644 KB |
answer = NO |
23 |
Correct |
1 ms |
2644 KB |
answer = NO |
24 |
Correct |
1 ms |
2644 KB |
answer = YES |
25 |
Correct |
1 ms |
2644 KB |
answer = YES |
26 |
Correct |
2 ms |
2644 KB |
answer = YES |
27 |
Correct |
1 ms |
2644 KB |
answer = YES |
28 |
Correct |
1 ms |
2644 KB |
answer = YES |
29 |
Correct |
1 ms |
2644 KB |
answer = YES |
30 |
Correct |
2 ms |
2644 KB |
answer = NO |
31 |
Correct |
1 ms |
2644 KB |
answer = YES |
32 |
Correct |
1 ms |
2644 KB |
answer = YES |
33 |
Correct |
1 ms |
2644 KB |
answer = YES |
34 |
Correct |
2 ms |
2644 KB |
answer = YES |
35 |
Correct |
1 ms |
2644 KB |
answer = YES |
36 |
Correct |
1 ms |
2644 KB |
answer = YES |
37 |
Correct |
1 ms |
2644 KB |
answer = YES |
38 |
Correct |
2 ms |
2604 KB |
answer = YES |
39 |
Correct |
2 ms |
2644 KB |
answer = YES |
40 |
Correct |
2 ms |
2772 KB |
answer = YES |
41 |
Correct |
2 ms |
2772 KB |
answer = NO |
42 |
Correct |
3 ms |
2772 KB |
answer = YES |
43 |
Correct |
2 ms |
2772 KB |
answer = YES |
44 |
Correct |
2 ms |
2772 KB |
answer = YES |
45 |
Correct |
2 ms |
2772 KB |
answer = YES |
46 |
Correct |
2 ms |
2644 KB |
answer = YES |
47 |
Correct |
3 ms |
2772 KB |
answer = YES |
48 |
Correct |
2 ms |
2772 KB |
answer = YES |
49 |
Correct |
12 ms |
3772 KB |
answer = YES |
50 |
Correct |
13 ms |
4200 KB |
answer = YES |
51 |
Correct |
14 ms |
4268 KB |
answer = YES |
52 |
Correct |
8 ms |
4180 KB |
answer = NO |
53 |
Correct |
3 ms |
2772 KB |
answer = YES |
54 |
Correct |
4 ms |
2900 KB |
answer = YES |
55 |
Correct |
6 ms |
3280 KB |
answer = YES |
56 |
Correct |
12 ms |
3692 KB |
answer = YES |
57 |
Correct |
12 ms |
3656 KB |
answer = YES |
58 |
Correct |
11 ms |
3668 KB |
answer = YES |
59 |
Correct |
10 ms |
3468 KB |
answer = YES |
60 |
Correct |
12 ms |
3688 KB |
answer = YES |
61 |
Correct |
7 ms |
3156 KB |
answer = YES |
62 |
Correct |
137 ms |
12824 KB |
answer = NO |
63 |
Correct |
117 ms |
12844 KB |
answer = YES |
64 |
Correct |
107 ms |
12748 KB |
answer = NO |
65 |
Correct |
130 ms |
12884 KB |
answer = YES |
66 |
Correct |
3 ms |
2772 KB |
answer = YES |
67 |
Correct |
122 ms |
21732 KB |
answer = YES |
68 |
Correct |
137 ms |
23052 KB |
answer = YES |
69 |
Correct |
116 ms |
22836 KB |
answer = YES |
70 |
Correct |
173 ms |
30468 KB |
answer = YES |
71 |
Correct |
107 ms |
22780 KB |
answer = YES |
72 |
Correct |
112 ms |
14144 KB |
answer = YES |
73 |
Correct |
113 ms |
13896 KB |
answer = YES |
74 |
Correct |
91 ms |
14664 KB |
answer = YES |
75 |
Correct |
58 ms |
14548 KB |
answer = NO |
76 |
Correct |
14 ms |
4052 KB |
answer = YES |
77 |
Correct |
29 ms |
5664 KB |
answer = YES |
78 |
Correct |
52 ms |
7824 KB |
answer = YES |
79 |
Correct |
105 ms |
12616 KB |
answer = YES |
80 |
Correct |
98 ms |
15004 KB |
answer = YES |
81 |
Correct |
87 ms |
16496 KB |
answer = NO |
82 |
Correct |
123 ms |
22128 KB |
answer = YES |
83 |
Correct |
138 ms |
23488 KB |
answer = YES |
84 |
Correct |
132 ms |
23500 KB |
answer = YES |
85 |
Correct |
121 ms |
23060 KB |
answer = YES |
86 |
Correct |
123 ms |
22892 KB |
answer = YES |
87 |
Correct |
82 ms |
15052 KB |
answer = NO |
88 |
Correct |
150 ms |
17744 KB |
answer = YES |
89 |
Correct |
107 ms |
12900 KB |
answer = YES |
90 |
Correct |
106 ms |
12840 KB |
answer = YES |
91 |
Correct |
114 ms |
13400 KB |
answer = YES |
92 |
Correct |
55 ms |
8288 KB |
answer = YES |
93 |
Correct |
65 ms |
8320 KB |
answer = YES |
94 |
Correct |
90 ms |
23616 KB |
answer = NO |
95 |
Correct |
73 ms |
11568 KB |
answer = NO |
96 |
Correct |
227 ms |
26060 KB |
answer = YES |
97 |
Correct |
70 ms |
22984 KB |
answer = NO |