#include <bits/stdc++.h>
using namespace std;
#define int long long
#define speedIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define f first
#define s second
#define pii pair<int,int>
#define pb push_back
vector<vector<pii>> graph;
vector<pair<int,float>> value;
vector<bool> visited;
vector<int> conectedcomp;
bool possible;
float thex;
void dfs(int u){
if(visited[u]) return;
visited[u]=true;
conectedcomp.pb(u);
//cout<<u<<'\n';
for(auto x:graph[u]){
int v=x.f; int c=x.s;
//cout<<"hi "<<u<<' '<<v<<'\n';
if(visited[v]){
if(value[u].f==-value[v].f){
if(value[u].s!=c-value[v].s){
possible=false;
}
}else{
//cout<<"buhhuuhuh "<<u<<' '<<v<<'\n';
float tmp=c-value[u].s-value[v].s;
tmp/=(value[v].f+value[u].f);
if(thex==1e10){
thex=tmp;
}else if(thex!=tmp){
possible=false;
}
value[u]={0,tmp*(value[u].f)+value[u].s};
value[v]={0,tmp*(value[v].f)+value[v].s};
}
}else{
value[v]={-value[u].f,c-value[u].s};
dfs(v);
}
}
}
int32_t main(){
speedIO;
int t,n,m,k;
//cin>>t;
t=1;
while(t--){
//string s;
cin>>n>>m;
graph.resize(n+1);
value.resize(n+1);
conectedcomp.clear();
visited.resize(n+1,false);
possible=true;
thex=1e10;
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
if(a==b){
value[a]={0,c/2};
}
graph[a].pb({b,c});
graph[b].pb({a,c});
}
//value[1]={1,0};
//dfs(1);
vector<float> ans(n+1);
for(int i=1;i<=n;i++){
//cout<<value[i].f<<' '<<value[i].s<<'\n';
if(visited[i]==false){
conectedcomp.clear();
thex=1e10;
//visited[i]=true;
value[i]={1,0};
dfs(i);
if(thex!=1e10){
for(int j:conectedcomp){
value[j]={0,thex*(value[j].f)+value[j].s};
//cout<<value[j].s<<' ';
ans[j]=(value[j].s);
}
}else{
vector<float> valss;
float yyyy=0;
for(int j:conectedcomp){
valss.pb(-(value[j].f)*(value[j].s));
yyyy-=(value[j].f)*(value[j].s);
}
sort(valss.rbegin(),valss.rend());
float tmp=1e10;
int xxxxx=conectedcomp.size();
int idx;
for(int l=0;l<conectedcomp.size();l++){
//cout<<xxxxx<<' '<<yyyy<<'\n';
if (tmp>xxxxx*valss[l]-yyyy){
tmp=xxxxx*valss[l]-yyyy;
idx=valss[l];
}
yyyy-=2*valss[l];
xxxxx-=2;
}
for(int j:conectedcomp){
ans[j]=idx*(value[j].f)+value[j].s;
}
}
}
}
if(possible){
cout<<"YES"<<'\n';
for(int i=1;i<=n;i++){
cout<<ans[i]<<' ';
}
cout<<'\n';
}else{
cout<<"NO"<<'\n';
}
}
return 0;
}
Compilation message
Graph.cpp: In function 'int32_t main()':
Graph.cpp:110:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(int l=0;l<conectedcomp.size();l++){
| ~^~~~~~~~~~~~~~~~~~~~
Graph.cpp:56:12: warning: unused variable 'k' [-Wunused-variable]
56 | int t,n,m,k;
| ^
Graph.cpp:120:17: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
120 | ans[j]=idx*(value[j].f)+value[j].s;
| ~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
answer = YES |
2 |
Correct |
1 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
344 KB |
answer = NO |
5 |
Correct |
0 ms |
344 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
1 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
452 KB |
answer = YES |
17 |
Correct |
0 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
344 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
answer = YES |
2 |
Correct |
1 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
344 KB |
answer = NO |
5 |
Correct |
0 ms |
344 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
1 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
452 KB |
answer = YES |
17 |
Correct |
0 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
344 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
0 ms |
348 KB |
answer = YES |
25 |
Correct |
0 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
452 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
0 ms |
348 KB |
answer = YES |
34 |
Correct |
0 ms |
348 KB |
answer = YES |
35 |
Correct |
0 ms |
348 KB |
answer = YES |
36 |
Correct |
1 ms |
348 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
answer = YES |
2 |
Correct |
1 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
344 KB |
answer = NO |
5 |
Correct |
0 ms |
344 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
1 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
452 KB |
answer = YES |
17 |
Correct |
0 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
344 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
0 ms |
348 KB |
answer = YES |
25 |
Correct |
0 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
452 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
0 ms |
348 KB |
answer = YES |
34 |
Correct |
0 ms |
348 KB |
answer = YES |
35 |
Correct |
0 ms |
348 KB |
answer = YES |
36 |
Correct |
1 ms |
348 KB |
answer = YES |
37 |
Correct |
0 ms |
348 KB |
answer = YES |
38 |
Correct |
0 ms |
348 KB |
answer = YES |
39 |
Correct |
0 ms |
348 KB |
answer = YES |
40 |
Correct |
1 ms |
348 KB |
answer = YES |
41 |
Correct |
1 ms |
348 KB |
answer = NO |
42 |
Correct |
1 ms |
348 KB |
answer = YES |
43 |
Correct |
1 ms |
348 KB |
answer = YES |
44 |
Correct |
1 ms |
344 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
1 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
464 KB |
answer = YES |
48 |
Correct |
1 ms |
348 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
answer = YES |
2 |
Correct |
1 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
344 KB |
answer = NO |
5 |
Correct |
0 ms |
344 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
1 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
452 KB |
answer = YES |
17 |
Correct |
0 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
344 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
0 ms |
348 KB |
answer = YES |
25 |
Correct |
0 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
452 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
0 ms |
348 KB |
answer = YES |
34 |
Correct |
0 ms |
348 KB |
answer = YES |
35 |
Correct |
0 ms |
348 KB |
answer = YES |
36 |
Correct |
1 ms |
348 KB |
answer = YES |
37 |
Correct |
0 ms |
348 KB |
answer = YES |
38 |
Correct |
0 ms |
348 KB |
answer = YES |
39 |
Correct |
0 ms |
348 KB |
answer = YES |
40 |
Correct |
1 ms |
348 KB |
answer = YES |
41 |
Correct |
1 ms |
348 KB |
answer = NO |
42 |
Correct |
1 ms |
348 KB |
answer = YES |
43 |
Correct |
1 ms |
348 KB |
answer = YES |
44 |
Correct |
1 ms |
344 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
1 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
464 KB |
answer = YES |
48 |
Correct |
1 ms |
348 KB |
answer = YES |
49 |
Correct |
6 ms |
1624 KB |
answer = YES |
50 |
Correct |
11 ms |
2064 KB |
answer = YES |
51 |
Correct |
6 ms |
1884 KB |
answer = YES |
52 |
Correct |
3 ms |
1884 KB |
answer = NO |
53 |
Correct |
1 ms |
348 KB |
answer = YES |
54 |
Correct |
2 ms |
656 KB |
answer = YES |
55 |
Correct |
3 ms |
1096 KB |
answer = YES |
56 |
Correct |
6 ms |
1628 KB |
answer = YES |
57 |
Correct |
6 ms |
1372 KB |
answer = YES |
58 |
Correct |
6 ms |
1524 KB |
answer = YES |
59 |
Correct |
5 ms |
1372 KB |
answer = YES |
60 |
Correct |
5 ms |
1456 KB |
answer = YES |
61 |
Correct |
5 ms |
856 KB |
answer = YES |
62 |
Correct |
39 ms |
12368 KB |
answer = NO |
63 |
Correct |
44 ms |
12628 KB |
answer = YES |
64 |
Correct |
52 ms |
12628 KB |
answer = NO |
65 |
Correct |
39 ms |
12628 KB |
answer = YES |
66 |
Correct |
1 ms |
600 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
answer = YES |
2 |
Correct |
1 ms |
348 KB |
answer = YES |
3 |
Correct |
0 ms |
348 KB |
answer = YES |
4 |
Correct |
0 ms |
344 KB |
answer = NO |
5 |
Correct |
0 ms |
344 KB |
answer = YES |
6 |
Correct |
0 ms |
348 KB |
answer = YES |
7 |
Correct |
0 ms |
348 KB |
answer = YES |
8 |
Correct |
1 ms |
348 KB |
answer = YES |
9 |
Correct |
0 ms |
348 KB |
answer = NO |
10 |
Correct |
0 ms |
348 KB |
answer = YES |
11 |
Correct |
0 ms |
348 KB |
answer = YES |
12 |
Correct |
0 ms |
348 KB |
answer = NO |
13 |
Correct |
0 ms |
348 KB |
answer = YES |
14 |
Correct |
1 ms |
348 KB |
answer = YES |
15 |
Correct |
0 ms |
348 KB |
answer = YES |
16 |
Correct |
0 ms |
452 KB |
answer = YES |
17 |
Correct |
0 ms |
344 KB |
answer = YES |
18 |
Correct |
0 ms |
344 KB |
answer = YES |
19 |
Correct |
0 ms |
344 KB |
answer = YES |
20 |
Correct |
0 ms |
348 KB |
answer = YES |
21 |
Correct |
0 ms |
348 KB |
answer = YES |
22 |
Correct |
0 ms |
348 KB |
answer = NO |
23 |
Correct |
0 ms |
344 KB |
answer = NO |
24 |
Correct |
0 ms |
348 KB |
answer = YES |
25 |
Correct |
0 ms |
348 KB |
answer = YES |
26 |
Correct |
0 ms |
348 KB |
answer = YES |
27 |
Correct |
1 ms |
348 KB |
answer = YES |
28 |
Correct |
0 ms |
348 KB |
answer = YES |
29 |
Correct |
0 ms |
348 KB |
answer = YES |
30 |
Correct |
0 ms |
348 KB |
answer = NO |
31 |
Correct |
0 ms |
452 KB |
answer = YES |
32 |
Correct |
0 ms |
348 KB |
answer = YES |
33 |
Correct |
0 ms |
348 KB |
answer = YES |
34 |
Correct |
0 ms |
348 KB |
answer = YES |
35 |
Correct |
0 ms |
348 KB |
answer = YES |
36 |
Correct |
1 ms |
348 KB |
answer = YES |
37 |
Correct |
0 ms |
348 KB |
answer = YES |
38 |
Correct |
0 ms |
348 KB |
answer = YES |
39 |
Correct |
0 ms |
348 KB |
answer = YES |
40 |
Correct |
1 ms |
348 KB |
answer = YES |
41 |
Correct |
1 ms |
348 KB |
answer = NO |
42 |
Correct |
1 ms |
348 KB |
answer = YES |
43 |
Correct |
1 ms |
348 KB |
answer = YES |
44 |
Correct |
1 ms |
344 KB |
answer = YES |
45 |
Correct |
1 ms |
348 KB |
answer = YES |
46 |
Correct |
1 ms |
348 KB |
answer = YES |
47 |
Correct |
1 ms |
464 KB |
answer = YES |
48 |
Correct |
1 ms |
348 KB |
answer = YES |
49 |
Correct |
6 ms |
1624 KB |
answer = YES |
50 |
Correct |
11 ms |
2064 KB |
answer = YES |
51 |
Correct |
6 ms |
1884 KB |
answer = YES |
52 |
Correct |
3 ms |
1884 KB |
answer = NO |
53 |
Correct |
1 ms |
348 KB |
answer = YES |
54 |
Correct |
2 ms |
656 KB |
answer = YES |
55 |
Correct |
3 ms |
1096 KB |
answer = YES |
56 |
Correct |
6 ms |
1628 KB |
answer = YES |
57 |
Correct |
6 ms |
1372 KB |
answer = YES |
58 |
Correct |
6 ms |
1524 KB |
answer = YES |
59 |
Correct |
5 ms |
1372 KB |
answer = YES |
60 |
Correct |
5 ms |
1456 KB |
answer = YES |
61 |
Correct |
5 ms |
856 KB |
answer = YES |
62 |
Correct |
39 ms |
12368 KB |
answer = NO |
63 |
Correct |
44 ms |
12628 KB |
answer = YES |
64 |
Correct |
52 ms |
12628 KB |
answer = NO |
65 |
Correct |
39 ms |
12628 KB |
answer = YES |
66 |
Correct |
1 ms |
600 KB |
answer = YES |
67 |
Correct |
55 ms |
18744 KB |
answer = YES |
68 |
Correct |
54 ms |
18628 KB |
answer = YES |
69 |
Correct |
47 ms |
18116 KB |
answer = YES |
70 |
Correct |
71 ms |
25564 KB |
answer = YES |
71 |
Correct |
51 ms |
18280 KB |
answer = YES |
72 |
Correct |
57 ms |
12748 KB |
answer = YES |
73 |
Correct |
52 ms |
12236 KB |
answer = YES |
74 |
Correct |
36 ms |
11116 KB |
answer = YES |
75 |
Correct |
22 ms |
11260 KB |
answer = NO |
76 |
Correct |
7 ms |
1968 KB |
answer = YES |
77 |
Correct |
14 ms |
3544 KB |
answer = YES |
78 |
Correct |
24 ms |
5844 KB |
answer = YES |
79 |
Correct |
53 ms |
11084 KB |
answer = YES |
80 |
Correct |
40 ms |
11208 KB |
answer = YES |
81 |
Correct |
34 ms |
14788 KB |
answer = NO |
82 |
Correct |
59 ms |
17864 KB |
answer = YES |
83 |
Correct |
65 ms |
18884 KB |
answer = YES |
84 |
Correct |
68 ms |
19140 KB |
answer = YES |
85 |
Correct |
59 ms |
18628 KB |
answer = YES |
86 |
Correct |
56 ms |
18120 KB |
answer = YES |
87 |
Correct |
45 ms |
13916 KB |
answer = NO |
88 |
Correct |
67 ms |
14796 KB |
answer = YES |
89 |
Correct |
52 ms |
11088 KB |
answer = YES |
90 |
Correct |
54 ms |
11092 KB |
answer = YES |
91 |
Correct |
54 ms |
11604 KB |
answer = YES |
92 |
Correct |
28 ms |
6360 KB |
answer = YES |
93 |
Correct |
26 ms |
6356 KB |
answer = YES |
94 |
Correct |
43 ms |
19140 KB |
answer = NO |
95 |
Correct |
31 ms |
11352 KB |
answer = NO |
96 |
Correct |
98 ms |
22736 KB |
answer = YES |
97 |
Correct |
31 ms |
18628 KB |
answer = NO |