이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <stdlib.h>
#define N 300000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
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 qu[N], cnt;
int dfs1(int p, int i, int t) {
int o;
if (i == t) {
qu[cnt++] = i;
return 1;
}
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && dfs1(i, j, t)) {
qu[cnt++] = i;
return 1;
}
}
return 0;
}
int dp[N], w, x;
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)
if (dp[ii[j]] == dp[i_])
j++;
else if (dp[ii[j]] > dp[i_]) {
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;
}
}
void dfs2(int p, int i) {
static int jj[N];
int o, cnt, h;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
if (i == w && j == x || i == x && j == w)
continue;
dfs2(i, j);
}
}
cnt = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
if (i == w && j == x || i == x && j == w)
continue;
jj[cnt++] = j;
}
}
sort(jj, 0, cnt);
dp[i] = 0;
for (h = 0; h < cnt; h++)
dp[i] = max(dp[i], dp[jj[h]] + h + 1);
}
int main() {
int n, h, i, j, u, v, ans, lower, upper;
scanf("%d%d%d", &n, &u, &v), u--, v--;
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);
}
dfs1(-1, u, v);
ans = INF;
lower = -1, upper = cnt - 1;
while (upper - lower > 1) {
int h = (lower + upper) / 2;
w = qu[h + 1], x = qu[h];
dfs2(-1, u), dfs2(-1, v);
ans = min(ans, max(dp[u], dp[v]));
if (dp[u] > dp[v])
lower = h;
else
upper = h;
}
printf("%d\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
torrent.c: In function 'append':
torrent.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
21 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
torrent.c: In function 'dfs2':
torrent.c:75:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
75 | if (i == w && j == x || i == x && j == w)
| ~~~~~~~^~~~~~~~~
torrent.c:85:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
85 | if (i == w && j == x || i == x && j == w)
| ~~~~~~~^~~~~~~~~
torrent.c: In function 'main':
torrent.c:99:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d%d%d", &n, &u, &v), u--, v--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
torrent.c:103:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |