This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using i64 = long long;
constexpr int inf = 1E9;
void gg() {
std::cout << "NO\n";
std::exit(0);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<std::vector<std::pair<int, int>>> adj(n);
for (int i = 0; i < m; i++) {
int u, v, c;
std::cin >> u >> v >> c;
u--;
v--;
adj[u].emplace_back(v, c);
adj[v].emplace_back(u, c);
}
std::vector<bool> vis(n);
std::vector<int> col(n), a(n);
std::vector<double> ans(n);
for (int u = 0; u < n; u++) {
if (!vis[u]) {
int res = inf;
std::vector<int> b;
std::function<void(int)> dfs = [&](int u) {
vis[u] = true;
for (auto [v, c] : adj[u]) {
if (!vis[v]) {
col[v] = col[u] ^ 1;
a[v] = c - a[u];
dfs(v);
} else {
if (col[u] != col[v]) {
if (a[u] + a[v] != c) {
gg();
}
} else {
if (res == inf) {
res = c - a[u] - a[v];
} else if (res != c - a[u] - a[v]) {
gg();
}
}
}
}
b.push_back(u);
};
dfs(u);
if (res != inf) {
for (int v : b) {
ans[v] = a[v] + (col[v] == 0 ? res / 2. : -res / 2.);
}
} else {
std::pair<int, int> best(inf, -1);
for (int i = 0; i <= 2; i++) {
int sum = 0;
for (int v : b) {
sum += std::abs(a[v] + (col[v] == 0 ? i : -i));
}
best = std::min(best, std::pair(sum, i));
}
for (int v : b) {
ans[v] = std::abs(a[v] + (col[v] == 0 ? best.second : -best.second));
}
}
}
}
std::cout << "YES\n";
for (int i = 0; i < n; i++) {
std::cout << ans[i] << " \n"[i == n - 1];
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |