#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
vector<pair<int,int>>adj[maxn],adjt[maxn];
int isr[maxn],vis[maxn],n,m,high[maxn],resf[maxn],resz[maxn],val[maxn];
int res=1,timea=0;
vector<int>now;
void dfs(int u){
if(vis[u]==1){
return ;
}
now.push_back(u);
isr[u]=1;
vis[u]=1;
for(auto xx:adj[u]){
int x=xx.first;
int y=xx.second;
if(vis[x]==0){
adjt[u].push_back(xx);
adjt[x].push_back(make_pair(u,xx.second));
high[x]=high[u]+1;
if(high[u]&1){
resf[x]=resf[u];
resz[x]=resz[u]+y;
}
else{
resf[x]=resf[u]+y;
resz[x]=resz[u];
}
dfs(x);
}
}
}
pair<int,int> checkalle(){
for(auto x:now){
for(auto xx:adj[x]){
int y=xx.first;
if(high[x]<high[y]){
continue;
}
if(high[x]==high[y]+1){
continue;
}
int sf=resf[x]-resf[y],sz=resz[x]-resz[y];
if((high[x]&1)!=(high[y]&1)){
if(high[x]&1){
sz+=xx.second;
}
else{
sf+=xx.second;
}
if(sz==sf){
continue;
}
return make_pair(-2,-2);
}
if(high[x]&1){
sf+=xx.second;
}
else{
sz+=xx.second;
}
if(sz==sf){
return make_pair(x,0);
continue;
}
int ss;
if(high[x]&1){
ss=sf-sz;
}
else{
ss=sz-sf;
}
ss/=2;
//ss=-ss;
//cout<<x<<" "<<ss<<" "<<sz<<" "<<sf<<"wtf\n";
return make_pair(x,ss);
}
}
return make_pair(-1,-1);
}
int por(int u,int w,int par=0){
//cout<<u<<" "<<w<<" "<<par<<" asd\n";
val[u]=w;
int ret=abs(w);
for(auto x:adjt[u]){
if(x.first!=par){
ret+=por(x.first,x.second-w,u);
}
}
return ret;
}
void solve(int u){
now.clear();
dfs(u);
pair<int,int>fixy=checkalle();
if(fixy.first==-2){
res=0;
return ;
}
//cout<<u<<" "<<endl;
if(fixy.first!=-1){
por(fixy.first,fixy.second);
return ;
}
int mina=1e17,w=0;
for(int i=-125;i<125;i++){
int z=por(u,i);
if(z<mina){
mina=z;
w=i;
}
else if(z==mina&&abs(i)<abs(w)){
w=i;
}
}
//cout<<por(u,0)<<" "<<por(u,2)<<" "<<mina<<" "<<w<<"\n";
por(u,w);
}
void checkakh(){
for(int i=1;i<=n;i++){
for(auto x:adj[i]){
int y=x.first;
//cout<<val[i]<<" "<<val[y]<<" "<<x.second<<"\n";
if(val[i]+val[y]!=x.second){
res=0;
return ;
}
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
//cout.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v,w;
cin>>u>>v>>w;
w*=2;
adj[u].push_back(make_pair(v,w));
adj[v].push_back(make_pair(u,w));
}
for(int i=1;i<=n;i++){
if(isr[i]==0){
//cout<<"ha: "<<i<<"\n";
solve(i);
}
}
////cout<<res<<"\n";
checkakh();
if(res==0){
cout<<"NO\n";
return 0;
}
cout<<"YES\n";
cout<<setprecision(2);
for(int i=1;i<=n;i++){
if(val[i]<0){
cout<<"-";
}
val[i]=abs(val[i]);
if((val[i]&1)==1){
val[i]/=2;
cout<<val[i];
cout<<".5";
}
else{
val[i]/=2;
cout<<val[i];
}
cout<<" ";
}
cout<<"\n";
}
Compilation message
Graph.cpp: In function 'void solve(int)':
Graph.cpp:110:11: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+17' to '2147483647' [-Woverflow]
110 | int mina=1e17,w=0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
answer = YES |
2 |
Correct |
3 ms |
4948 KB |
answer = YES |
3 |
Correct |
2 ms |
4948 KB |
answer = YES |
4 |
Correct |
3 ms |
5008 KB |
answer = NO |
5 |
Correct |
2 ms |
5024 KB |
answer = YES |
6 |
Correct |
3 ms |
5020 KB |
answer = YES |
7 |
Correct |
2 ms |
4948 KB |
answer = YES |
8 |
Correct |
3 ms |
5020 KB |
answer = YES |
9 |
Correct |
2 ms |
4948 KB |
answer = NO |
10 |
Correct |
2 ms |
4948 KB |
answer = YES |
11 |
Correct |
4 ms |
5024 KB |
answer = YES |
12 |
Correct |
3 ms |
4948 KB |
answer = NO |
13 |
Correct |
3 ms |
4948 KB |
answer = YES |
14 |
Correct |
2 ms |
4948 KB |
answer = YES |
15 |
Correct |
3 ms |
4948 KB |
answer = YES |
16 |
Correct |
2 ms |
5024 KB |
answer = YES |
17 |
Correct |
3 ms |
4948 KB |
answer = YES |
18 |
Correct |
2 ms |
5028 KB |
answer = YES |
19 |
Correct |
3 ms |
5020 KB |
answer = YES |
20 |
Correct |
3 ms |
5020 KB |
answer = YES |
21 |
Correct |
3 ms |
4948 KB |
answer = YES |
22 |
Correct |
2 ms |
5024 KB |
answer = NO |
23 |
Correct |
2 ms |
4948 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
answer = YES |
2 |
Correct |
3 ms |
4948 KB |
answer = YES |
3 |
Correct |
2 ms |
4948 KB |
answer = YES |
4 |
Correct |
3 ms |
5008 KB |
answer = NO |
5 |
Correct |
2 ms |
5024 KB |
answer = YES |
6 |
Correct |
3 ms |
5020 KB |
answer = YES |
7 |
Correct |
2 ms |
4948 KB |
answer = YES |
8 |
Correct |
3 ms |
5020 KB |
answer = YES |
9 |
Correct |
2 ms |
4948 KB |
answer = NO |
10 |
Correct |
2 ms |
4948 KB |
answer = YES |
11 |
Correct |
4 ms |
5024 KB |
answer = YES |
12 |
Correct |
3 ms |
4948 KB |
answer = NO |
13 |
Correct |
3 ms |
4948 KB |
answer = YES |
14 |
Correct |
2 ms |
4948 KB |
answer = YES |
15 |
Correct |
3 ms |
4948 KB |
answer = YES |
16 |
Correct |
2 ms |
5024 KB |
answer = YES |
17 |
Correct |
3 ms |
4948 KB |
answer = YES |
18 |
Correct |
2 ms |
5028 KB |
answer = YES |
19 |
Correct |
3 ms |
5020 KB |
answer = YES |
20 |
Correct |
3 ms |
5020 KB |
answer = YES |
21 |
Correct |
3 ms |
4948 KB |
answer = YES |
22 |
Correct |
2 ms |
5024 KB |
answer = NO |
23 |
Correct |
2 ms |
4948 KB |
answer = NO |
24 |
Correct |
3 ms |
4948 KB |
answer = YES |
25 |
Correct |
3 ms |
5028 KB |
answer = YES |
26 |
Correct |
3 ms |
5020 KB |
answer = YES |
27 |
Correct |
3 ms |
4948 KB |
answer = YES |
28 |
Correct |
3 ms |
4948 KB |
answer = YES |
29 |
Correct |
3 ms |
5020 KB |
answer = YES |
30 |
Correct |
3 ms |
4948 KB |
answer = NO |
31 |
Correct |
3 ms |
5024 KB |
answer = YES |
32 |
Correct |
3 ms |
5024 KB |
answer = YES |
33 |
Correct |
3 ms |
4948 KB |
answer = YES |
34 |
Correct |
2 ms |
5024 KB |
answer = YES |
35 |
Correct |
2 ms |
5028 KB |
answer = YES |
36 |
Correct |
2 ms |
4948 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
answer = YES |
2 |
Correct |
3 ms |
4948 KB |
answer = YES |
3 |
Correct |
2 ms |
4948 KB |
answer = YES |
4 |
Correct |
3 ms |
5008 KB |
answer = NO |
5 |
Correct |
2 ms |
5024 KB |
answer = YES |
6 |
Correct |
3 ms |
5020 KB |
answer = YES |
7 |
Correct |
2 ms |
4948 KB |
answer = YES |
8 |
Correct |
3 ms |
5020 KB |
answer = YES |
9 |
Correct |
2 ms |
4948 KB |
answer = NO |
10 |
Correct |
2 ms |
4948 KB |
answer = YES |
11 |
Correct |
4 ms |
5024 KB |
answer = YES |
12 |
Correct |
3 ms |
4948 KB |
answer = NO |
13 |
Correct |
3 ms |
4948 KB |
answer = YES |
14 |
Correct |
2 ms |
4948 KB |
answer = YES |
15 |
Correct |
3 ms |
4948 KB |
answer = YES |
16 |
Correct |
2 ms |
5024 KB |
answer = YES |
17 |
Correct |
3 ms |
4948 KB |
answer = YES |
18 |
Correct |
2 ms |
5028 KB |
answer = YES |
19 |
Correct |
3 ms |
5020 KB |
answer = YES |
20 |
Correct |
3 ms |
5020 KB |
answer = YES |
21 |
Correct |
3 ms |
4948 KB |
answer = YES |
22 |
Correct |
2 ms |
5024 KB |
answer = NO |
23 |
Correct |
2 ms |
4948 KB |
answer = NO |
24 |
Correct |
3 ms |
4948 KB |
answer = YES |
25 |
Correct |
3 ms |
5028 KB |
answer = YES |
26 |
Correct |
3 ms |
5020 KB |
answer = YES |
27 |
Correct |
3 ms |
4948 KB |
answer = YES |
28 |
Correct |
3 ms |
4948 KB |
answer = YES |
29 |
Correct |
3 ms |
5020 KB |
answer = YES |
30 |
Correct |
3 ms |
4948 KB |
answer = NO |
31 |
Correct |
3 ms |
5024 KB |
answer = YES |
32 |
Correct |
3 ms |
5024 KB |
answer = YES |
33 |
Correct |
3 ms |
4948 KB |
answer = YES |
34 |
Correct |
2 ms |
5024 KB |
answer = YES |
35 |
Correct |
2 ms |
5028 KB |
answer = YES |
36 |
Correct |
2 ms |
4948 KB |
answer = YES |
37 |
Correct |
3 ms |
4976 KB |
answer = YES |
38 |
Correct |
3 ms |
4948 KB |
answer = YES |
39 |
Correct |
3 ms |
5076 KB |
answer = YES |
40 |
Correct |
3 ms |
5164 KB |
answer = YES |
41 |
Correct |
3 ms |
5076 KB |
answer = NO |
42 |
Correct |
6 ms |
5076 KB |
answer = YES |
43 |
Correct |
4 ms |
5076 KB |
answer = YES |
44 |
Correct |
4 ms |
5076 KB |
answer = YES |
45 |
Correct |
3 ms |
5076 KB |
answer = YES |
46 |
Correct |
4 ms |
5076 KB |
answer = YES |
47 |
Correct |
4 ms |
5076 KB |
answer = YES |
48 |
Correct |
3 ms |
5076 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
answer = YES |
2 |
Correct |
3 ms |
4948 KB |
answer = YES |
3 |
Correct |
2 ms |
4948 KB |
answer = YES |
4 |
Correct |
3 ms |
5008 KB |
answer = NO |
5 |
Correct |
2 ms |
5024 KB |
answer = YES |
6 |
Correct |
3 ms |
5020 KB |
answer = YES |
7 |
Correct |
2 ms |
4948 KB |
answer = YES |
8 |
Correct |
3 ms |
5020 KB |
answer = YES |
9 |
Correct |
2 ms |
4948 KB |
answer = NO |
10 |
Correct |
2 ms |
4948 KB |
answer = YES |
11 |
Correct |
4 ms |
5024 KB |
answer = YES |
12 |
Correct |
3 ms |
4948 KB |
answer = NO |
13 |
Correct |
3 ms |
4948 KB |
answer = YES |
14 |
Correct |
2 ms |
4948 KB |
answer = YES |
15 |
Correct |
3 ms |
4948 KB |
answer = YES |
16 |
Correct |
2 ms |
5024 KB |
answer = YES |
17 |
Correct |
3 ms |
4948 KB |
answer = YES |
18 |
Correct |
2 ms |
5028 KB |
answer = YES |
19 |
Correct |
3 ms |
5020 KB |
answer = YES |
20 |
Correct |
3 ms |
5020 KB |
answer = YES |
21 |
Correct |
3 ms |
4948 KB |
answer = YES |
22 |
Correct |
2 ms |
5024 KB |
answer = NO |
23 |
Correct |
2 ms |
4948 KB |
answer = NO |
24 |
Correct |
3 ms |
4948 KB |
answer = YES |
25 |
Correct |
3 ms |
5028 KB |
answer = YES |
26 |
Correct |
3 ms |
5020 KB |
answer = YES |
27 |
Correct |
3 ms |
4948 KB |
answer = YES |
28 |
Correct |
3 ms |
4948 KB |
answer = YES |
29 |
Correct |
3 ms |
5020 KB |
answer = YES |
30 |
Correct |
3 ms |
4948 KB |
answer = NO |
31 |
Correct |
3 ms |
5024 KB |
answer = YES |
32 |
Correct |
3 ms |
5024 KB |
answer = YES |
33 |
Correct |
3 ms |
4948 KB |
answer = YES |
34 |
Correct |
2 ms |
5024 KB |
answer = YES |
35 |
Correct |
2 ms |
5028 KB |
answer = YES |
36 |
Correct |
2 ms |
4948 KB |
answer = YES |
37 |
Correct |
3 ms |
4976 KB |
answer = YES |
38 |
Correct |
3 ms |
4948 KB |
answer = YES |
39 |
Correct |
3 ms |
5076 KB |
answer = YES |
40 |
Correct |
3 ms |
5164 KB |
answer = YES |
41 |
Correct |
3 ms |
5076 KB |
answer = NO |
42 |
Correct |
6 ms |
5076 KB |
answer = YES |
43 |
Correct |
4 ms |
5076 KB |
answer = YES |
44 |
Correct |
4 ms |
5076 KB |
answer = YES |
45 |
Correct |
3 ms |
5076 KB |
answer = YES |
46 |
Correct |
4 ms |
5076 KB |
answer = YES |
47 |
Correct |
4 ms |
5076 KB |
answer = YES |
48 |
Correct |
3 ms |
5076 KB |
answer = YES |
49 |
Correct |
45 ms |
6172 KB |
answer = YES |
50 |
Correct |
7 ms |
6612 KB |
answer = YES |
51 |
Correct |
35 ms |
6672 KB |
answer = YES |
52 |
Correct |
6 ms |
6540 KB |
answer = NO |
53 |
Correct |
4 ms |
5076 KB |
answer = YES |
54 |
Correct |
7 ms |
5212 KB |
answer = YES |
55 |
Correct |
12 ms |
5548 KB |
answer = YES |
56 |
Correct |
35 ms |
6052 KB |
answer = YES |
57 |
Correct |
19 ms |
6060 KB |
answer = YES |
58 |
Correct |
19 ms |
6052 KB |
answer = YES |
59 |
Correct |
6 ms |
5972 KB |
answer = YES |
60 |
Correct |
24 ms |
6100 KB |
answer = YES |
61 |
Correct |
6 ms |
5588 KB |
answer = YES |
62 |
Correct |
54 ms |
10948 KB |
answer = NO |
63 |
Correct |
48 ms |
10960 KB |
answer = YES |
64 |
Correct |
47 ms |
11112 KB |
answer = NO |
65 |
Correct |
45 ms |
10944 KB |
answer = YES |
66 |
Correct |
6 ms |
5204 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
answer = YES |
2 |
Correct |
3 ms |
4948 KB |
answer = YES |
3 |
Correct |
2 ms |
4948 KB |
answer = YES |
4 |
Correct |
3 ms |
5008 KB |
answer = NO |
5 |
Correct |
2 ms |
5024 KB |
answer = YES |
6 |
Correct |
3 ms |
5020 KB |
answer = YES |
7 |
Correct |
2 ms |
4948 KB |
answer = YES |
8 |
Correct |
3 ms |
5020 KB |
answer = YES |
9 |
Correct |
2 ms |
4948 KB |
answer = NO |
10 |
Correct |
2 ms |
4948 KB |
answer = YES |
11 |
Correct |
4 ms |
5024 KB |
answer = YES |
12 |
Correct |
3 ms |
4948 KB |
answer = NO |
13 |
Correct |
3 ms |
4948 KB |
answer = YES |
14 |
Correct |
2 ms |
4948 KB |
answer = YES |
15 |
Correct |
3 ms |
4948 KB |
answer = YES |
16 |
Correct |
2 ms |
5024 KB |
answer = YES |
17 |
Correct |
3 ms |
4948 KB |
answer = YES |
18 |
Correct |
2 ms |
5028 KB |
answer = YES |
19 |
Correct |
3 ms |
5020 KB |
answer = YES |
20 |
Correct |
3 ms |
5020 KB |
answer = YES |
21 |
Correct |
3 ms |
4948 KB |
answer = YES |
22 |
Correct |
2 ms |
5024 KB |
answer = NO |
23 |
Correct |
2 ms |
4948 KB |
answer = NO |
24 |
Correct |
3 ms |
4948 KB |
answer = YES |
25 |
Correct |
3 ms |
5028 KB |
answer = YES |
26 |
Correct |
3 ms |
5020 KB |
answer = YES |
27 |
Correct |
3 ms |
4948 KB |
answer = YES |
28 |
Correct |
3 ms |
4948 KB |
answer = YES |
29 |
Correct |
3 ms |
5020 KB |
answer = YES |
30 |
Correct |
3 ms |
4948 KB |
answer = NO |
31 |
Correct |
3 ms |
5024 KB |
answer = YES |
32 |
Correct |
3 ms |
5024 KB |
answer = YES |
33 |
Correct |
3 ms |
4948 KB |
answer = YES |
34 |
Correct |
2 ms |
5024 KB |
answer = YES |
35 |
Correct |
2 ms |
5028 KB |
answer = YES |
36 |
Correct |
2 ms |
4948 KB |
answer = YES |
37 |
Correct |
3 ms |
4976 KB |
answer = YES |
38 |
Correct |
3 ms |
4948 KB |
answer = YES |
39 |
Correct |
3 ms |
5076 KB |
answer = YES |
40 |
Correct |
3 ms |
5164 KB |
answer = YES |
41 |
Correct |
3 ms |
5076 KB |
answer = NO |
42 |
Correct |
6 ms |
5076 KB |
answer = YES |
43 |
Correct |
4 ms |
5076 KB |
answer = YES |
44 |
Correct |
4 ms |
5076 KB |
answer = YES |
45 |
Correct |
3 ms |
5076 KB |
answer = YES |
46 |
Correct |
4 ms |
5076 KB |
answer = YES |
47 |
Correct |
4 ms |
5076 KB |
answer = YES |
48 |
Correct |
3 ms |
5076 KB |
answer = YES |
49 |
Correct |
45 ms |
6172 KB |
answer = YES |
50 |
Correct |
7 ms |
6612 KB |
answer = YES |
51 |
Correct |
35 ms |
6672 KB |
answer = YES |
52 |
Correct |
6 ms |
6540 KB |
answer = NO |
53 |
Correct |
4 ms |
5076 KB |
answer = YES |
54 |
Correct |
7 ms |
5212 KB |
answer = YES |
55 |
Correct |
12 ms |
5548 KB |
answer = YES |
56 |
Correct |
35 ms |
6052 KB |
answer = YES |
57 |
Correct |
19 ms |
6060 KB |
answer = YES |
58 |
Correct |
19 ms |
6052 KB |
answer = YES |
59 |
Correct |
6 ms |
5972 KB |
answer = YES |
60 |
Correct |
24 ms |
6100 KB |
answer = YES |
61 |
Correct |
6 ms |
5588 KB |
answer = YES |
62 |
Correct |
54 ms |
10948 KB |
answer = NO |
63 |
Correct |
48 ms |
10960 KB |
answer = YES |
64 |
Correct |
47 ms |
11112 KB |
answer = NO |
65 |
Correct |
45 ms |
10944 KB |
answer = YES |
66 |
Correct |
6 ms |
5204 KB |
answer = YES |
67 |
Correct |
239 ms |
25496 KB |
answer = YES |
68 |
Correct |
214 ms |
25352 KB |
answer = YES |
69 |
Correct |
63 ms |
25408 KB |
answer = YES |
70 |
Correct |
69 ms |
28548 KB |
answer = YES |
71 |
Correct |
55 ms |
25620 KB |
answer = YES |
72 |
Correct |
546 ms |
15620 KB |
answer = YES |
73 |
Correct |
69 ms |
15684 KB |
answer = YES |
74 |
Correct |
34 ms |
16964 KB |
answer = YES |
75 |
Correct |
33 ms |
16324 KB |
answer = NO |
76 |
Correct |
47 ms |
6404 KB |
answer = YES |
77 |
Correct |
109 ms |
7688 KB |
answer = YES |
78 |
Correct |
212 ms |
9548 KB |
answer = YES |
79 |
Correct |
417 ms |
14000 KB |
answer = YES |
80 |
Correct |
370 ms |
16764 KB |
answer = YES |
81 |
Correct |
48 ms |
19780 KB |
answer = NO |
82 |
Correct |
66 ms |
24564 KB |
answer = YES |
83 |
Correct |
120 ms |
25372 KB |
answer = YES |
84 |
Incorrect |
558 ms |
25412 KB |
participant answer is larger than the answer of jury |
85 |
Halted |
0 ms |
0 KB |
- |