Submission #6413474


Source Code Expand

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

typedef int32_t i32;
typedef int64_t i64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))
#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))
#define SORT(a,num,cmp) qsort((a),(num),sizeof(*(a)),cmp)

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 free_graph (graph *g) {
  free (g->edge);
  free (g->start);
  free (g);
  return;
}

void clear_graph (graph *g) {
  g->pointer = 0;
  for (int32_t i = 0; i < g->vertex_num; ++i) {
    g->start[i] = -1;
  }
}

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++;
}

void BFS_graph (graph *g, int32_t src, i32 *q, i32 *parent) {
  uint8_t *used = (uint8_t *) calloc (g->vertex_num, sizeof (uint8_t));
  int32_t front = 0;
  int32_t last = 0;
  used[src] = 1;
  q[last++] = src;
  parent[src] = -1;
  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;
      //hoge
      if (!used[u]) {
        used[u] = 1;
        q[last++] = u;
        parent[u] = v;
      }
    }
  }
  free(used);
}

#define POS(i, j) ((i) * (2 * k + 1) + (j))

void run (void) {
  i32 n, k;
  scanf ("%" SCNi32 "%" SCNi32, &n, &k);
  graph *g = new_graph (n);
  for (i32 i = 1; i < n; ++i) {
    i32 a, b;
    scanf ("%" SCNi32 "%" SCNi32, &a, &b);
    a--; b--;
    add_edge (g, a, b);
    add_edge (g, b, a);
  }
  i32 *q = ALLOC (n, i32);
  i32 *parent = ALLOC (n, i32);
  BFS_graph (g, 0, q, parent);
  const i32 mod = 1000000007;
  i32 *dp = ALLOC (n * 3 * (2 * k + 1), i32);
  i64 *buf = ALLOC (3 * (2 * k + 1), i64);
  for (i32 i = n - 1; i >= 0; --i) {
    i32 v = q[i];
    i32 *now = dp + v * 3 * (2 * k + 1);
    now[POS(0, 0)] = 1;
    now[POS(2, 1)] = 1;
    for (i32 p = g->start[v]; p != -1; p = g->edge[p].next) {
      i32 u = g->edge[p].vertex;
      if (u == parent[v]) continue;
      i32 *way = dp + u * 3 * (2 * k + 1);
      memset (buf, 0, sizeof (i64) * 3 * (2 * k + 1));
      for (i32 j = 0; j <= k; ++j) {
        for (i32 l = 0; l <= k; ++l) {
          buf[POS(0, j + l)] += (i64) now[POS(0, j)] * way[POS(0, l)] % mod;
          buf[POS(0, j + l)] += (i64) now[POS(0, j)] * way[POS(1, l)] % mod;
          buf[POS(1, j + l)] += (i64) now[POS(1, j)] * way[POS(0, l)] % mod;
          buf[POS(1, j + l)] += (i64) now[POS(1, j)] * way[POS(1, l)] % mod;
          buf[POS(2, j + l)] += (i64) now[POS(2, j)] * way[POS(0, l)] % mod;
          buf[POS(2, j + l)] += (i64) now[POS(2, j)] * way[POS(1, l)] % mod;
          if (j > 0 && l > 0) {
            buf[POS(0, j + l - 1)] += (i64) now[POS(1, j)] * way[POS(1, l)] % mod;
            buf[POS(0, j + l - 1)] += (i64) now[POS(1, j)] * way[POS(2, l)] % mod;
            buf[POS(1, j + l - 1)] += (i64) now[POS(2, j)] * way[POS(1, l)] % mod;
            buf[POS(1, j + l - 1)] += (i64) now[POS(2, j)] * way[POS(2, l)] % mod;
          }
        }
      }
      for (i32 j = 0; j < 3; ++j) {
        for (i32 l = 0; l <= k; ++l) {
          now[POS(j, l)] = buf[POS(j, l)] % mod;
        }
      }
    }
  }
  i32 ans = (dp[POS(0, k)] + dp[POS(1, k)]) % mod;
  printf ("%" PRIi32 "\n", ans);
}

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

Submission Info

Submission Time
Task P - うなぎ
User sansen
Language C (GCC 5.4.1)
Score 6
Code Size 4281 Byte
Status AC
Exec Time 40 ms
Memory 1408 KB

Compile Error

./Main.c: In function ‘run’:
./Main.c:92:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%" SCNi32 "%" SCNi32, &n, &k);
   ^
./Main.c:96:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%" SCNi32 "%" SCNi32, &a, &b);
     ^

Judge Result

Set Name All
Score / Max Score 6 / 6
Status
AC × 9
Set Name Test Cases
All 00, 01, 02, 03, 04, 05, 06, 90, 91
Case Name Status Exec Time Memory
00 AC 39 ms 1408 KB
01 AC 1 ms 256 KB
02 AC 40 ms 1408 KB
03 AC 39 ms 1408 KB
04 AC 40 ms 1408 KB
05 AC 39 ms 1408 KB
06 AC 40 ms 1408 KB
90 AC 1 ms 128 KB
91 AC 1 ms 128 KB