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 50000
int max(int a, int b) { return a > b ? a : b; }
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;
}
char cc[N + 1]; int d_;
void dfs(int p1, int i1, int p2, int i2, int d) {
int o1, o2;
d_ = max(d_, d);
for (o1 = eo[i1]; o1--; ) {
int j1 = ej[i1][o1];
for (o2 = eo[i2]; o2--; ) {
int j2 = ej[i2][o2];
if (j1 != p1 && j2 != p2 && (i1 != i2 || j1 < j2) && cc[j1] == cc[j2])
dfs(i1, j1, i2, j2, d + 2);
}
}
}
int main() {
static int ii[N - 1], jj[N - 1];
int n, h, i, j;
scanf("%d%s", &n, cc);
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--;
ii[h] = i, jj[h] = j;
append(i, j), append(j, i);
}
for (i = 0; i < n; i++)
dfs(-1, i, -1, i, 1);
for (h = 0; h < n - 1; h++)
if (cc[ii[h]] == cc[jj[h]])
dfs(ii[h], jj[h], jj[h], ii[h], 2);
printf("%d\n", d_);
return 0;
}
Compilation message (stderr)
lampice.c: In function 'append':
lampice.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
lampice.c: In function 'main':
lampice.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
lampice.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |