Submission #439809

#TimeUsernameProblemLanguageResultExecution timeMemory
439809rainboyMag (COCI16_mag)C11
24 / 120
496 ms141500 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...