結果

問題 No.2337 Equidistant
ユーザー 👑 chro_96chro_96
提出日時 2023-06-03 00:10:26
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,566 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 30,792 KB
実行使用メモリ 63,496 KB
最終ジャッジ日時 2023-08-28 06:53:10
合計ジャッジ時間 6,981 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
32,272 KB
testcase_01 WA -
testcase_02 AC 9 ms
32,244 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 290 ms
63,496 KB
testcase_22 AC 347 ms
32,316 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct list {
  int v;
  struct list *n;
} list;

int func (int v, int tmpd, int tmpp, int tmpi, list **e, int *d, int p[][20], int idx[][2], int *size, int *visited, int **chidx, int *chidx_arr, int *ch, int *chcnt) {
  list *l = e[v];
  
  visited[v] = 1;
  d[v] = tmpd;
  p[v][0] = tmpp;
  idx[v][0] = tmpi;
  size[v] = 1;
  chidx[v] = chidx_arr+(*chcnt);
  ch[v] = 0;
  while (l != NULL) {
    if (visited[l->v] <= 0) {
      ch[v]++;
    }
    l = l->n;
  }
  *chcnt += ch[v];
  l = e[v];
  ch[v] = 0;
  while (l != NULL) {
    if (visited[l->v] <= 0) {
      tmpi = func(l->v, tmpd+1, v, tmpi+1, e, d, p, idx, size, visited, chidx, chidx_arr, ch, chcnt);
      size[v] += size[l->v];
      chidx[v][ch[v]] = l->v;
      ch[v]++;
    }
    l = l->n;
  }
  tmpi++;
  idx[v][1] = tmpi;
  return tmpi;
}

int main () {
  int n = 0;
  int q = 0;
  int a = 0;
  int b = 0;
  int s = 0;
  int t = 0;
  
  int res = 0;
  
  int d[200000] = {};
  int size[200000] = {};
  int p[200000][20] = {};
  
  list pool[399998] = {};
  int used = 0;
  
  list *e[200000] = {};
  int visited[200000] = {};
  
  int idx[200000][2] = {};
  int *chidx[200000] = {};
  int chidx_arr[200000] = {};
  int ch[200000] = {};
  int chcnt = 0;
  
  res = scanf("%d", &n);
  res = scanf("%d", &q);
  for (int i = 0; i < n-1; i++) {
    res = scanf("%d", &a);
    res = scanf("%d", &b);
    a--;
    b--;
    pool[used].v = b;
    pool[used].n = e[a];
    e[a] = pool+used;
    used++;
    pool[used].v = a;
    pool[used].n = e[b];
    e[b] = pool+used;
    used++;
  }
  res = func(0, 0, 0, 0, e, d, p, idx, size, visited, chidx, chidx_arr, ch, &chcnt);
  for (int i = 1; i < 20; i++) {
    for (int j = 0; j < n; j++) {
      p[j][i] = p[p[j][i-1]][i-1];
    }
  }
  while (q > 0) {
    res = scanf("%d", &s);
    res = scanf("%d", &t);
    s--;
    t--;
    if (d[s]%2 != d[t]%2) {
      printf("0\n");
    } else {
      int ps = s;
      int pt = t;
      int bit = 19;
      while (bit >= 0) {
        if (p[ps][bit] != p[pt][bit]) {
          ps = p[ps][bit];
          pt = p[pt][bit];
        }
        bit--;
      }
      ps = p[ps][0];
      pt = p[pt][0];
      if (d[s] == d[t]) {
        int ans = n;
        int r[2] = { 0, ch[ps] };
        while (r[1]-r[0] > 1) {
          int nxt = (r[0]+r[1])/2;
          if (idx[chidx[ps][nxt]][0] <= idx[s][0]) {
            r[0] = nxt;
          } else {
            r[1] = nxt;
          }
        }
        ans -= size[chidx[ps][r[0]]];
        r[0] = 0;
        r[1] = ch[ps];
        while (r[1]-r[0] > 1) {
          int nxt = (r[0]+r[1])/2;
          if (idx[chidx[ps][nxt]][0] <= idx[t][0]) {
            r[0] = nxt;
          } else {
            r[1] = nxt;
          }
        }
        ans -= size[chidx[ps][r[0]]];
        printf("%d\n", ans);
      } else {
        int pd = 0;
        int cnt = s;
        int r[2] = {};
        int ans = 0;
        if (d[s] < d[t]) {
          int swap = s;
          s = t;
          t = swap;
        }
        pd = d[s]-d[ps]-(d[s]-d[t])/2;
        for (int i = 0; i < 20; i++) {
          if ((pd&(1<<i)) > 0) {
            cnt = p[cnt][i];
          }
        }
        r[0] = 0;
        r[1] = ch[cnt];
        while (r[1]-r[0] > 1) {
          int nxt = (r[0]+r[1])/2;
          if (idx[chidx[cnt][nxt]][0] <= idx[s][0]) {
            r[0] = nxt;
          } else {
            r[1] = nxt;
          }
        }
        ans = size[cnt];
        ans -= size[chidx[cnt][r[0]]];
        printf("%d\n", ans);
      }
    }
    q--;
  }
  
  return 0;
}
0