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 200000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int xx[N], yy[N];
long long cross(int i, int j, int k) {
return (long long) (xx[j] - xx[i]) * (yy[k] - yy[i]) - (long long) (xx[k] - xx[i]) * (yy[j] - yy[i]);
}
int i1;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
long long c = cross(i1, ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
int sz[N];
void dfs1(int p, int i) {
int o;
sz[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j);
sz[i] += sz[j];
}
}
}
int ii_[N];
void dfs2(int *ii, int p, int i) {
int o, s;
ii_[i] = ii[0];
s = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(ii + s, i, j), s += sz[j];
}
}
int main() {
static int ii[N];
int n, h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]);
dfs1(-1, 0);
for (i = 0; i < n; i++)
ii[i] = i;
i1 = -1;
for (i = 0; i < n; i++)
if (i1 == -1 || xx[i1] > xx[i] || xx[i1] == xx[i] && yy[i1] > yy[i])
i1 = i;
ii[0] = i1, ii[i1] = 0;
sort(ii, 1, n);
dfs2(ii, -1, 0);
for (i = 0; i < n; i++)
printf("%d ", ii_[i] + 1);
printf("\n");
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:101:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
101 | if (i1 == -1 || xx[i1] > xx[i] || xx[i1] == xx[i] && yy[i1] > yy[i])
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
Main.c:87:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:91:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:95:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d%d", &xx[i], &yy[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |