結果

問題 No.2337 Equidistant
ユーザー 👑 chro_96chro_96
提出日時 2023-06-02 23:48:45
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 2,236 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 29,432 KB
実行使用メモリ 40,880 KB
最終ジャッジ日時 2023-08-28 06:34:48
合計ジャッジ時間 12,066 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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]++;
    }
  }
  *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]] = idx[l->v][0];
      ch[v]++;
    }
  }
  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];
    }
  }
  res = scanf("%d", &q);
  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[ps][bit]) {
          ps = p[ps][bit];
          pt = p[pt][bit];
        }
        bit--;
      }
      ps = p[ps][0];
      pt = p[pt][0];
    }
    q--;
  }
  
  printf("%d\n", a-n);
  return 0;
}
0