# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
439809 |
2021-06-30T20:11:04 Z |
rainboy |
Mag (COCI16_mag) |
C |
|
496 ms |
141500 KB |
#include <stdio.h>
#include <stdlib.h>
#define N 1000000
#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; }
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 aa[N], kk[N];
void dfs1(int p, int i) {
int o, k;
k = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j);
k = max(k, kk[j]);
}
}
kk[i] = aa[i] == 1 ? k + 1 : 0;
}
int p_, q_;
void dfs2(int p, int i, int k) {
int o, k1, k2, j1;
if (aa[i] == 1) {
q_ = max(q_, k + 1);
k1 = k2 = k, j1 = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
if (k1 < kk[j])
k2 = k1, k1 = kk[j], j1 = j;
else if (k2 < kk[j])
k2 = kk[j];
}
}
k1++, k2++;
} else
k1 = k2 = 0, j1 = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(i, j, j == j1 ? k2 : k1);
}
}
int main() {
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", &aa[i]);
p_ = 1;
dfs1(-1, 0), dfs2(-1, 0, 0);
if (q_ == 0) {
p_ = INF, q_ = 1;
for (i = 0; i < n; i++)
p_ = min(p_, aa[i]);
}
printf("%d/%d\n", p_, q_);
return 0;
}
Compilation message
mag.c: In function 'append':
mag.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
mag.c: In function 'main':
mag.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
mag.c:73:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
mag.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
424 ms |
78476 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
496 ms |
141500 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
480 ms |
50792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
397 ms |
51944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
5576 KB |
Output is correct |
2 |
Incorrect |
430 ms |
51536 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
380 ms |
48012 KB |
Output is correct |
2 |
Incorrect |
416 ms |
50144 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
51584 KB |
Output is correct |
2 |
Correct |
328 ms |
27160 KB |
Output is correct |