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 <stdio.h>
#include <stdlib.h>
#define N 100000
#define M 200000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ii[M], jj[M], ww[M];
int *eh[N], eo[N];
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int ff[N], cc[N], aa[N], bb[N], cnt;
void dfs2(int i, int a);
int dfs1(int f, int i, int c, int a) {
int o;
if (cc[i]) {
if (cc[i] != c) {
int j;
j = i ^ ii[f] ^ jj[f], a = 0;
while (j != i)
a = ww[ff[j]] - a, j ^= ii[ff[j]] ^ jj[ff[j]];
a = ww[f] - a, j ^= ii[f] ^ jj[f];
dfs2(j, a / 2);
return 1;
}
return 0;
}
ff[i] = f, cc[i] = c, aa[i] = a;
bb[cnt++] = c == 1 ? -a : a;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
if (dfs1(h, j, c ^ 3, ww[h] - a))
return 1;
}
return 0;
}
void dfs2(int i, int a) {
int o;
if (cc[i] == -1)
return;
cc[i] = -1, aa[i] = a;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
dfs2(j, ww[h] - a);
}
}
int median(int *aa, int n) {
int l = 0, r = n, m = (n - 1) / 2;
while (1) {
int i = l, j = l, k = r, a = aa[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[j] == a)
j++;
else if (aa[j] < a) {
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
i++, j++;
} else {
k--;
tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
}
if (m < i)
r = i;
else if (m >= k)
l = k;
else
return a;
}
}
int main() {
int n, m, h, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++) {
scanf("%d%d%d", &i, &j, &ww[h]), i--, j--, ww[h] *= 2;
ii[h] = i, jj[h] = j;
append(i, h), append(j, h);
}
for (i = 0; i < n; i++)
if (!cc[i]) {
cnt = 0;
if (!dfs1(-1, i, 1, 0))
dfs2(i, median(bb, cnt));
}
for (h = 0; h < m; h++)
if (aa[ii[h]] + aa[jj[h]] != ww[h]) {
printf("NO\n");
return 0;
}
printf("YES\n");
for (i = 0; i < n; i++)
if (aa[i] > 0)
printf("%d.%d ", aa[i] / 2, aa[i] % 2 == 0 ? 0 : 5);
else if (aa[i] < 0)
printf("-%d.%d ", -aa[i] / 2, -aa[i] % 2 == 0 ? 0 : 5);
else
printf("0.0 ");
printf("\n");
return 0;
}
Compilation message (stderr)
Graph.c: In function 'append':
Graph.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Graph.c: In function 'main':
Graph.c:96:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Graph.c:100:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d%d%d", &i, &j, &ww[h]), i--, j--, ww[h] *= 2;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |