Submission #258912

#TimeUsernameProblemLanguageResultExecution timeMemory
258912easruiGraph (BOI20_graph)C++14
100 / 100
244 ms26348 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() const int MN = 2e5+5; //start at 1:43 int E[MN],dep[MN],cnt[MN],par[MN],odd[MN],cur; double C[MN],sum[MN],val[MN]; vector<int> G[MN],group; vector<double> tmp; bool vis[MN],fail; void dfs(int s) { vis[s] = 1; for(int d:G[s]){ int e = E[d]-s; if(vis[e]){ if(dep[e]<dep[s]) continue; if((dep[e]-dep[s])%2){ if( (cnt[e]-cnt[par[s]]) - (cnt[par[e]]-cnt[s]) != C[d]-1) fail = 1; } else{ if(odd[cur]) continue; odd[cur] = s; val[s] = (-sum[e]+sum[s]+sum[par[e]]-sum[par[s]]+C[d])/2.0; } } else{ par[e] = s; dep[e] = dep[s]+1; cnt[e] = cnt[par[s]]; if(C[d]==2) cnt[e]++; sum[e] = sum[par[s]]+C[d]; dfs(e); } } } void solve(int s) { group.push_back(s); vis[s] = 1; for(int d:G[s]){ int e = E[d]-s; if(vis[e]){ if(odd[cur]&&val[s]+val[e]!=C[d]) fail = 1; } else{ val[e] = C[d]-val[s]; solve(e); } } tmp.push_back(dep[s]%2 ? -val[s] : val[s]); } int N,M; int main() { ios_base::sync_with_stdio(0),cin.tie(0); cin >> N >> M; for(int i=0; i<M; i++){ int a,b; cin >> a >> b >> C[i]; E[i] = a+b; G[a].push_back(i); G[b].push_back(i); } for(int i=1; i<=N; i++){ if(vis[i]) continue; cur = i; dfs(i); } fill(vis,vis+N+1,0); for(int i=1; i<=N; i++){ if(vis[i]) continue; cur = i; group.clear(); tmp.clear(); if(odd[i]) solve(odd[i]); else{ solve(i); sort(all(tmp)); double x = -tmp[group.size()/2]; for(int s:group){ if(dep[s]%2) val[s] -= x; else val[s] += x; } } } fixed(cout); cout.precision(1); if(fail) cout << "NO"; else{ cout << "YES\n"; for(int i=1; i<=N; i++) cout << val[i] << ' '; } }
#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...