Submission #642770

#TimeUsernameProblemLanguageResultExecution timeMemory
642770berrGraph (BOI20_graph)C++17
100 / 100
165 ms28980 KiB
#include <bits/stdc++.h> using namespace std; #define int long long int c[(int)1e5+5], val[(int)1e5+5], vis[(int)(1e5+5)], val2[(int)(1e5)+5]; vector<array<int, 2>> adj[(int)(1e5+5)]; vector<int> pos[(int)(1e5+5)], aaa; int flag=1; void dfs(int x, int y, int z) { vis[x]=z; pos[z].push_back(x); for(auto i: adj[x]) { if(i[0]==y) continue; if(vis[i[0]]!=0) { if(c[x]+c[i[0]]==0&&val[i[0]]+val[x]!=i[1]) { flag=0; } else if(c[x]+c[i[0]]!=0) { if(val2[z]==-1) { val2[z]=(i[1]-val[i[0]]-val[x])/(c[x]+c[i[0]]); } else { if(val2[z]!=(i[1]-val[i[0]]-val[x])/(c[x]+c[i[0]])) flag=0; } } } else { val[i[0]]=i[1]-val[x]; c[i[0]]=-1*c[x]; dfs(i[0], x, z); } } } void dfs2(int z, int p) { vis[z]=1; for(auto i: adj[z]) { if(val[i[0]]+val[z]!=i[1]) flag=0; else if(!vis[i[0]]) dfs2(i[0], z); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin>>n>>m; for(int i=0; i<m; i++) { int x, y, z; cin>>x>>y>>z; z*=1e7; adj[x].push_back({y, z}); adj[y].push_back({x, z}); } for(int i=1; i<=n; i++) { val2[i]=-1; } for(int i=1; i<=n; i++) { if(!vis[i]) c[i]=1,dfs(i, 0, i), aaa.push_back(i); } for(auto z: aaa) { if(val2[z]!=-1) { for(auto i: pos[z]) { val[i]+=c[i]*val2[z]; } } else { int s=-1e10; for(int i=40; i>=0; i--) { int tmp=s+(1LL<<i); int ans1=0, ans2=0; int tmp2=tmp-1; for(auto j: pos[z]) { ans1+=abs(val[j]+(c[j]*tmp)); ans2+=abs(val[j]+(c[j]*tmp2)); } if(ans1<ans2) s=tmp; } val2[z]=s; for(auto i: pos[z]) { val[i]+=c[i]*val2[z]; } } } if(flag) { cout<<"YES\n"; for(int i=1; i<=n; i++) { float x=(float)val[i]/(float)10000000; cout<<fixed<<x<<" "; } } else { cout<<"NO\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...