結果

問題 No.812 Change of Class
ユーザー akakimidoriakakimidori
提出日時 2019-04-12 21:44:33
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 122 ms / 4,000 ms
コード長 2,267 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 29,768 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-03 01:04:22
合計ジャッジ時間 5,336 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
4,380 KB
testcase_01 AC 6 ms
4,376 KB
testcase_02 AC 23 ms
4,376 KB
testcase_03 AC 50 ms
4,376 KB
testcase_04 AC 45 ms
4,380 KB
testcase_05 AC 122 ms
4,380 KB
testcase_06 AC 114 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 103 ms
4,380 KB
testcase_10 AC 109 ms
4,408 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 62 ms
4,376 KB
testcase_13 AC 0 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 75 ms
4,380 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 67 ms
4,376 KB
testcase_18 AC 50 ms
4,376 KB
testcase_19 AC 58 ms
4,380 KB
testcase_20 AC 112 ms
4,376 KB
testcase_21 AC 48 ms
4,380 KB
testcase_22 AC 21 ms
4,504 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 16 ms
4,380 KB
testcase_26 AC 92 ms
4,380 KB
testcase_27 AC 82 ms
4,380 KB
testcase_28 AC 56 ms
4,380 KB
testcase_29 AC 12 ms
4,376 KB
testcase_30 AC 72 ms
4,380 KB
testcase_31 AC 62 ms
4,380 KB
testcase_32 AC 27 ms
4,380 KB
testcase_33 AC 76 ms
4,380 KB
testcase_34 AC 5 ms
4,380 KB
testcase_35 AC 13 ms
4,380 KB
testcase_36 AC 6 ms
4,376 KB
testcase_37 AC 36 ms
4,376 KB
testcase_38 AC 4 ms
4,376 KB
testcase_39 AC 37 ms
4,376 KB
testcase_40 AC 8 ms
4,380 KB
testcase_41 AC 4 ms
4,376 KB
testcase_42 AC 78 ms
4,376 KB
testcase_43 AC 14 ms
4,376 KB
testcase_44 AC 56 ms
4,380 KB
testcase_45 AC 6 ms
4,376 KB
testcase_46 AC 13 ms
4,380 KB
testcase_47 AC 54 ms
4,376 KB
testcase_48 AC 52 ms
4,380 KB
testcase_49 AC 15 ms
4,380 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 14 ms
4,376 KB
testcase_52 AC 27 ms
4,504 KB
testcase_53 AC 10 ms
4,380 KB
testcase_54 AC 8 ms
4,376 KB
testcase_55 AC 29 ms
4,380 KB
testcase_56 AC 34 ms
4,376 KB
testcase_57 AC 36 ms
4,376 KB
testcase_58 AC 19 ms
4,376 KB
testcase_59 AC 41 ms
4,376 KB
testcase_60 AC 0 ms
4,376 KB
testcase_61 AC 0 ms
4,380 KB
testcase_62 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef struct directed_edge {
  int32_t vertex;
  int32_t next;
} graph_edge;

typedef struct directedGraph {
  graph_edge *edge;
  int32_t *start;
  int32_t pointer;
  int32_t vertex_num;
  int32_t edge_max_size;
} graph;

graph* new_graph (const int vertex_num) {
  graph *g = (graph *) calloc (1, sizeof (graph));
  g->edge = (graph_edge *) calloc (1, sizeof (graph_edge));
  g->start = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  g->pointer = 0;
  g->vertex_num = vertex_num;
  g->edge_max_size = 1;
  for (int32_t i = 0; i < vertex_num; ++i) {
    g->start[i] = -1;
  }
  return g;
}

void add_edge (graph *g, int32_t from, int32_t to) {
  if (g->pointer == g->edge_max_size) {
    g->edge_max_size *= 2;
    g->edge = (graph_edge *) realloc (g->edge, sizeof (graph_edge) * g->edge_max_size);
  }
  g->edge[g->pointer] = (graph_edge) {to, g->start[from]};
  g->start[from] = g->pointer++;
}

int32_t BFS_graph (graph *g, int32_t src, int32_t *res) {
  int32_t *q = (int32_t *) calloc (g->vertex_num, sizeof (int32_t));
  uint8_t *used = (uint8_t *) calloc (g->vertex_num, sizeof (uint8_t));
  int32_t *d = (int32_t *) calloc (g->vertex_num, sizeof (int32_t));
  int32_t front = 0;
  int32_t last = 0;
  used[src] = 1;
  q[last++] = src;
  while (front < last) {
    const int32_t v = q[front++];
    for (int32_t p = g->start[v]; p != -1; p = g->edge[p].next) {
      const int32_t u = g->edge[p].vertex;
      if (!used[u]) {
	used[u] = 1;
	q[last++] = u;
	d[u] = d[v] + 1;
      }
    }
  }
  *res = d[q[last - 1]];
  free(q);
  free(used);
  free (d);
  return last;
}

typedef int32_t i32;

void run (void) {
  i32 n, m;
  scanf ("%" SCNi32 "%" SCNi32, &n, &m);
  graph *g = new_graph (n);
  while (m--) {
    i32 a, b;
    scanf ("%" SCNi32 "%" SCNi32, &a, &b);
    a--;
    b--;
    add_edge (g, a, b);
    add_edge (g, b, a);
  }
  i32 q;
  scanf ("%" SCNi32, &q);
  while (q--) {
    i32 a;
    scanf ("%" SCNi32, &a);
    a--;
    i32 dd = 0;
    i32 ans = BFS_graph (g, a, &dd) - 1;
    i32 d = 1;
    i32 k = 0;
    while (d < dd) {
      d *= 2;
      k++;
    }
    printf ("%" PRIi32 " %" PRIi32 "\n", ans, k);
  }
}

int main (void) {
  run ();
  return 0;
}
0