# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
855659 | 2023-10-01T16:05:32 Z | Trisanu_Das | Graph (BOI20_graph) | C++17 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define int long long double EPS = 1e-8; vector<pair<int, bool>> adj[100000]; int n, m, c[100000]; double x = 1e18, r[100000]; bool s[100000], vis[100000], ok = true; vector<int> comp; void dfs(int u){ vis[u] = 1; comp.push_back(u); for(auto &[v, w] : g[u]){ if(!vis[v]){ s[v] = !s[u]; c[v] = (2 - (int)w) - c[u]; dfs(v); }else if(s[u] == s[v]) x = ((2 - (int)w) - c[u] - c[v]) / (s[u] ? -2.0 : 2.0); } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; while(m--){ int u, v, w; cin >> u >> v >> w; --u, --v; g[u].emplace_back(v, w & 1); g[v].emplace_back(u, w & 1); } for(int z = 0; z < n && ok; z++){ if(vis[z]) continue; comp.clear(); x = 1e18; dfs(z); if(x > 1e17){ vector<int> v; for(int i : comp) v.push_back(s[i] ? c[i] : -c[i]); sort(v.begin(), v.end()); x = v[(int)v.size() / 2]; } for(int i : comp) r[i] = (c[i] + (s[i] ? -x : x)); } for(int u = 0; u < n; u++) for(auto &[v, w] : g[u]) ok &&= abs((2 - (int)w) - r[u] - r[v]) < EPS; if(!ok) cout << "NO\n"; else{ cout << "YES\n"; cout << setprecision(1) << fixed; for(int i = 0; i < n; i++) cout << r[i] << ' '; } }