Submission #550937

#TimeUsernameProblemLanguageResultExecution timeMemory
550937Zhora_004Graph (BOI20_graph)C++17
58 / 100
1084 ms13004 KiB
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <string> #include <sstream> #include <iomanip> #include <map> #include <unordered_map> #include <stack> #include <cstdio> #include <climits> #include <tuple> #include <ctime> #include <cstring> #include <numeric> #include <functional> #include <chrono> #include <cassert> #include <bitset> #include <fstream> //#include <bit> //#include <ranges> //#include <numbers> #define itn int #define sacnf scanf #define sz(a) ((int)((a).size())) // printf("%.10f\n", ans); /*ofstream fout("timeline.out"); ifstream fin("timeline.in");*/ using ll = long long; using namespace std; const ll mod = 1e9 + 7; const int limit = 160; const int N = 2e5 + 1, inf = 1e9; struct Edge { int v, w; }; int n, m, timer; vector<bool> vis; vector<int> ans, layer; vector<vector<Edge>> gp; void dfs1(int u) { layer[u] = timer++; vis[u] = 1; for (Edge& e : gp[u]) if (!vis[e.v]) dfs1(e.v); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; vis = vector<bool>(n); ans = layer = vector<int>(n); gp = vector<vector<Edge>>(n); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; gp[--u].push_back({ --v, w * 2 }); gp[v].push_back({ u, w * 2 }); } queue<int> q1; for (int i = 0; i < n; i++) { if (!vis[i]) { timer = 0; dfs1(i); int mn = inf, num = inf; for (int val = -limit; val <= limit; val++) { vector<bool> used(timer); queue<int> q; q.push(i); used[layer[i]] = 1; ans[i] = val; int sum = abs(val); while (!q.empty()) { int u = q.front(); q.pop(); for (Edge& e : gp[u]) { int v = e.v, w = e.w; if (!used[layer[v]]) { used[layer[v]] = 1; ans[v] = w - ans[u]; q.push(v); if (ans[v] < -limit || ans[v] > limit) { sum = inf; break; } sum += abs(ans[v]); } else if (ans[u] + ans[v] != w) { sum = inf; break; } } if (sum == inf) break; } if (sum < mn) mn = sum, num = val; } if (num == inf) return cout << "NO", 0; vector<bool> used(timer); q1.push(i); used[layer[i]] = 1; ans[i] = num; while (!q1.empty()) { int u = q1.front(); q1.pop(); for (Edge& e : gp[u]) { int v = e.v, w = e.w; if (!used[layer[v]]) { used[layer[v]] = 1; ans[v] = w - ans[u]; q1.push(v); } } } } } cout << "YES\n"; for (int& i : ans) { if (i == -1) cout << "-0.5"; else { cout << i / 2; if (abs(i) & 1) cout << ".5"; } cout << " "; } 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...