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/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
using ld = long double;
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int maxn = 1e5;
const ld UNKNOWN = 1e9, EPS = 1e-6;
vector<int> vals;
vector<pair<int, int>> graph[maxn];
bool found, vis[maxn];
int depth[maxn];
ld ans[maxn];
void dfs2(int u) {
for (auto& [v, e] : graph[u]) {
if (ans[v] == UNKNOWN) {
ans[v] = e - ans[u];
dfs2(v);
} else if (abs(e - ans[u] - ans[v]) > EPS) {
dbg(u, v, ans[u], ans[v]);
cout << "NO" << endl;
exit(0);
}
}
}
void dfs1(int u, int d) {
static vector<pair<int, int>> st;
if (found) {
return;
}
if (vis[u]) {
if ((d - depth[u]) % 2 == 0) {
return;
}
found = true;
ld sum = 0;
vector<pair<int, int>> cur;
for (int i = sz(st) - 1; i >= 0; i--) {
if (i < sz(st) - 1 && st[i].first == u) {
break;
}
cur.push_back(st[i]);
}
for (auto& [_, x] : cur) {
sum += x;
}
sum /= 2;
for (int i = 1; i < sz(cur); i += 2) {
sum -= cur[i].second;
}
dbg(cur[0].first, sum);
ans[cur[0].first] = sum;
dfs2(cur[0].first);
return;
}
vis[u] = true;
depth[u] = d;
for (auto& [v, e] : graph[u]) {
st.emplace_back(v, e);
dfs1(v, d + 1);
st.pop_back();
}
}
void dfs3(int u, int s, bool neg) {
if (vis[u]) {
return;
}
dbg(s);
if (neg) {
vals.push_back(-s);
} else {
vals.push_back(s);
}
vis[u] = true;
for (auto& [v, e] : graph[u]) {
dfs3(v, e - s, !neg);
}
}
void solve() {
int n, m;
cin >> n >> m;
int edges[m][3];
for (auto& [u, v, w] : edges) {
cin >> u >> v >> w;
u--;
v--;
graph[u].emplace_back(v, w);
graph[v].emplace_back(u, w);
}
fill(begin(ans), end(ans), UNKNOWN);
for (int i = 0; i < n; i++) {
if (!vis[i] && ans[i] == UNKNOWN) {
found = false;
dbg(i);
dfs1(i, 0);
}
}
memset(vis, 0, sizeof(vis));
dbg("A");
for (int i = 0; i < n; i++) {
if (ans[i] == UNKNOWN) {
vals.clear();
dfs3(i, 0, true);
sort(begin(vals), end(vals));
ans[i] = vals[sz(vals) / 2];
dfs2(i);
}
}
for (auto& [u, v, w] : edges) {
assert(abs(w - ans[u] - ans[v]) <= EPS);
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
cout << ans[i] << " \n"[i == n - 1];
}
}
int main() {
cout << fixed << setprecision(9);
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin.exceptions(ios::failbit);
solve();
}
# | 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... |