Submission #291900

#TimeUsernameProblemLanguageResultExecution timeMemory
291900BertedGraph (BOI20_graph)C++14
100 / 100
202 ms20924 KiB
#include <iostream> #include <bitset> #include <iomanip> #include <assert.h> #include <vector> #include <cmath> #include <algorithm> #include <queue> #define vi vector<int> #define pub push_back #define pii pair<int, int> #define fst first #define snd second #define vpi vector<pii> const int INF = 1e9; using namespace std; int n, m, res[100001], sgn[100001], c[100001]; vpi adj[100001]; bitset<100001> vis; vector<int> lol, mem; int k = INF; void dfs(int u) { if (sgn[u]) {lol.push_back(c[u]);} else {lol.push_back(-c[u]);} mem.push_back(u); for (auto v : adj[u]) { if (!vis[v.fst]) { vis[v.fst] = 1; sgn[v.fst] = !sgn[u]; c[v.fst] = 2 * v.snd - c[u]; dfs(v.fst); } else { if (sgn[v.fst] != sgn[u] && c[v.fst] == 2 * v.snd - c[u]) continue; else if (sgn[v.fst] == sgn[u]) { int r; if (sgn[v.fst]) { r = c[v.fst] - (2 * v.snd - c[u]); } else { r = (2 * v.snd - c[u]) - c[v.fst]; } r /= 2; if (k == INF || k == r) {k = r;} else {k = -INF;} } else {k = -INF;} } } } int main() { ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) {res[i] = 1000000;} for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; adj[u - 1].push_back({v - 1, w}); adj[v - 1].push_back({u - 1, w}); } bool val = 1; for (int i = 0; i < n && val; i++) { if (!vis[i]) { vis[i] = 1; sgn[i] = 0; c[i] = 0; dfs(i); if (k == INF) { nth_element(lol.begin(), lol.begin() + lol.size() / 2, lol.end()); k = lol[lol.size() / 2]; } if (k == -INF) {val = 0;} else { for (auto x : mem) { res[x] = c[x] + ((sgn[x]) ? (-k) : k); } } lol.clear(); mem.clear(); k = INF; } } if (val) { cout << "YES\n"; for (int i = 0; i < n; i++) { cout << fixed << setprecision(6) << (double)res[i] / 2; if (i + 1 < n) cout << " "; } cout << "\n"; } else {cout << "NO\n";} return 0; }
#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...