Submission #1046302

# Submission time Handle Problem Language Result Execution time Memory
1046302 2024-08-06T12:42:03 Z sleepntsheep File Paths (BOI15_fil) C
0 / 100
17 ms 604 KB
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define N 6005
#define A 1000005

int n_, s, n, m, k, m_, par[N], *eh[N], eo[N], dep[N], chain[N], o, f[N], fo, ans[N], tin[N];

int cc(const void *i, const void *j) {
  return *(int*)i-*(int*)j;
}

void factors(int x) {
  f[0] = 1;
  fo = 1;
  for (int j = 2; j * j <= x; ++j)
    if (x % j == 0) {
      f[fo++] = j;
      if (x != x / j)
        f[fo++] = x / j;
    }
  qsort(f, fo, sizeof *f, cc);
}

void pus(int **eh, int *eo, int i, int j) {
  int o = eo[i]++;
  if (!o) eh[i] = (int*)malloc(sizeof **eh * 2);
  else if (0 == (o & o - 1)) eh[i] = (int*)realloc(eh[i], 2 * o * sizeof **eh);
  eh[i][o] = j;
}

void efs(int u) {
  assert(0);
  dep[u] = 1e9;
  for (int j = 0; j < eo[u]; j += 2) if (eh[u][j] != par[u])
    efs(eh[u][j]);
}

void dfs(int u) {
  for (int j = 0; j < eo[u]; j += 2) if (eh[u][j] != par[u]) {
    int v = eh[u][j], l = eh[u][j+1];
    dep[v] = l + dep[u];
    if (dep[v] > k) efs(v);
    else dfs(v);
  }
}

int main(void) {
  scanf("%d%d%d%d", &n, &m, &k, &s);
  n_ = n;
  m_ = m;
  n = (n_ = n) + (m_ = m);
  ++s;

  for (int p, l, i = 1; i <= n; ++i) {
    scanf("%d%d", &p, &l);
    pus(eh, eo, par[i] = p, i);
    pus(eh, eo, p, l + 1);
  }

  dfs(0);

  for (int i = n_ + 1; i <= n; ++i) {
    int cycle;

    if (dep[i] == k) {
      ans[i] = 1;
      goto Next;
    }

    /* sub 2 - simple check */
    o = 1;
    chain[0] = dep[i];
    for (int cur = i; cur != 0; cur = par[cur])
      if (cur <= n_)
        chain[o++] = dep[i] - dep[cur];

    qsort(chain, o, sizeof *chain, cc);

    for (int j = 0; j <= n_; ++j) {
      int left = k - s - dep[j];
      if (left > 0) {
        int md, lb = 0, ub = o;
        while (ub - lb > 1)
          if (chain[md = lb + (ub - lb) / 2] <= left) lb = md;
          else ub = md;
        if (chain[lb] == left) {
          ans[i] = 0;
          goto Next;
        }
      }
    }

    cycle = k - dep[i];
    if (cycle > 0) {
      factors(cycle);


    }
Next:;
  }

  for (int i = n_ + i; i <= n; ++i)
    puts(ans[i] ? "YES": "NO");
}

Compilation message

fil.c: In function 'pus':
fil.c:29:24: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   29 |   else if (0 == (o & o - 1)) eh[i] = (int*)realloc(eh[i], 2 * o * sizeof **eh);
      |                      ~~^~~
fil.c: In function 'main':
fil.c:50:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   scanf("%d%d%d%d", &n, &m, &k, &s);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fil.c:57:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     scanf("%d%d", &p, &l);
      |     ^~~~~~~~~~~~~~~~~~~~~
fil.c:104:12: warning: 'i' is used uninitialized in this function [-Wuninitialized]
  104 |   for (int i = n_ + i; i <= n; ++i)
      |            ^
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -