# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
745424 | MilosMilutinovic | Graph (BOI20_graph) | C++14 | 144 ms | 23124 KiB |
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 namespace std;
const int N = 2e5 + 10;
int n, m, coeff[N], delta[N];
double ans[N], val;
vector<pair<int, int>> g[N];
vector<int> comp;
bool ok = true, found, was[N];
void dfs(int x) {
was[x] = true;
comp.push_back(x);
for (auto& p : g[x]) {
int y = p.first;
int w = p.second;
if (was[y]) {
if (-coeff[y] == coeff[x]) {
ok = (ok & ((w - delta[y]) == delta[x]));
} else {
found = true;
val = (w - delta[x] - delta[y]) * 1.0 / (coeff[y] + coeff[x]);
}
continue;
}
coeff[y] = -coeff[x];
delta[y] = w - delta[x];
dfs(y);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
for (int i = 1; i <= n; i++) if (!was[i]) {
coeff[i] = 1;
delta[i] = 0;
comp.clear();
found = false;
dfs(i);
if (!ok) break;
if (found) {
for (int x : comp) ans[x] = val * coeff[x] + delta[x];
} else {
vector<int> f;
for (int x : comp) f.push_back(delta[x] * coeff[x]);
sort(f.begin(), f.end());
val = -f[(int) f.size() / 2];
for (int x : comp) ans[x] = val * coeff[x] + delta[x];
}
}
for (int i = 1; i <= n; i++) {
for (auto& p : g[i]) {
if (ans[i] + ans[p.first] != p.second * 1.0) ok = false;
}
}
if (!ok) { printf("NO"); return 0; }
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%0.5lf ", ans[i]);
return 0;
}
Compilation message (stderr)
# | 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... |