結果
問題 | No.828 全方位神童数 |
ユーザー | akakimidori |
提出日時 | 2019-06-06 09:56:14 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 88 ms / 2,000 ms |
コード長 | 3,457 bytes |
コンパイル時間 | 438 ms |
コンパイル使用メモリ | 32,128 KB |
実行使用メモリ | 7,808 KB |
最終ジャッジ日時 | 2024-09-24 23:57:38 |
合計ジャッジ時間 | 6,221 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 5 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 5 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 3 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 3 ms
6,940 KB |
testcase_13 | AC | 31 ms
6,944 KB |
testcase_14 | AC | 65 ms
6,944 KB |
testcase_15 | AC | 23 ms
6,940 KB |
testcase_16 | AC | 32 ms
6,944 KB |
testcase_17 | AC | 25 ms
6,940 KB |
testcase_18 | AC | 45 ms
6,944 KB |
testcase_19 | AC | 81 ms
7,680 KB |
testcase_20 | AC | 54 ms
6,940 KB |
testcase_21 | AC | 72 ms
6,940 KB |
testcase_22 | AC | 17 ms
6,940 KB |
testcase_23 | AC | 6 ms
6,940 KB |
testcase_24 | AC | 43 ms
6,940 KB |
testcase_25 | AC | 18 ms
6,940 KB |
testcase_26 | AC | 80 ms
7,296 KB |
testcase_27 | AC | 68 ms
6,940 KB |
testcase_28 | AC | 81 ms
7,424 KB |
testcase_29 | AC | 76 ms
7,040 KB |
testcase_30 | AC | 43 ms
6,944 KB |
testcase_31 | AC | 41 ms
6,940 KB |
testcase_32 | AC | 79 ms
7,168 KB |
testcase_33 | AC | 87 ms
7,680 KB |
testcase_34 | AC | 68 ms
6,940 KB |
testcase_35 | AC | 44 ms
6,944 KB |
testcase_36 | AC | 56 ms
6,940 KB |
testcase_37 | AC | 37 ms
6,944 KB |
testcase_38 | AC | 51 ms
6,944 KB |
testcase_39 | AC | 80 ms
7,296 KB |
testcase_40 | AC | 37 ms
6,940 KB |
testcase_41 | AC | 31 ms
6,944 KB |
testcase_42 | AC | 88 ms
7,808 KB |
testcase_43 | AC | 58 ms
6,940 KB |
testcase_44 | AC | 23 ms
6,944 KB |
testcase_45 | AC | 34 ms
6,944 KB |
ソースコード
#include<stdio.h> #include<stdlib.h> #include<stdint.h> #include<inttypes.h> #include<string.h> typedef int32_t i32; typedef int64_t i64; 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++; } typedef struct union_find { int32_t *parent; i32 *sum; int32_t size; } union_find; void init_union_find (union_find * const u) { for (int32_t i = 0; i < u->size; ++i){ u->parent[i] = -1; u->sum[i] = 0; } } union_find* new_union_find (const int32_t size) { union_find * const u = (union_find *) calloc (1, sizeof (union_find)); u->parent = (int32_t *) calloc (size, sizeof (int32_t)); u->sum = (i32 *) calloc (size, sizeof (i32)); u->size = size; init_union_find (u); return u; } int32_t root (union_find * const u, int32_t x) { i32 stack[32]; i32 top = 0; stack[top++] = x; while (u->parent[x] >= 0) { x = u->parent[x]; stack[top++] = x; } i32 r = stack[top - 1]; i32 sum = 0; for (i32 i = top - 2; i >= 0; --i) { i32 y = stack[i]; i32 next = sum + u->sum[y]; u->sum[y] += sum; sum = next; u->parent[y] = r; } return r; } int same (union_find * const u, const int32_t x, const int32_t y) { return root (u, x) == root (u, y); } int32_t get_size (union_find * const u, const int32_t x) { return - (u->parent[root (u, x)]); } void unite (union_find * const u, int32_t x, int32_t y) { x = root (u, x); y = root (u, y); if (x == y) return; if (u->parent[x] > u->parent[y]) { const int32_t swap = x; x = y; y = swap; } u->parent[x] += u->parent[y]; u->parent[y] = x; u->sum[y] -= u->sum[x]; } void add (union_find *u, i32 x, i32 v) { u->sum[root (u, x)] += v; } i32 get_sum (union_find *u, i32 x) { i32 r = root (u, x); return r == x ? u->sum[x] : u->sum[x] + u->sum[r]; } void run (void) { i32 n; scanf ("%" SCNi32, &n); i32 *r = (i32 *) calloc (n, sizeof (i32)); for (i32 i = 0; i < n; ++i) { scanf ("%" SCNi32, r + i); } 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); } union_find *u = new_union_find (n); for (i32 i = 0; i < n; ++i) { for (i32 p = g->start[i]; p != -1; p = g->edge[p].next) { i32 v = g->edge[p].vertex; if (v > i) continue; unite (u, i, v); } add (u, i, 1); } const i32 mod = 1000000007; i32 ans = 1; for (i32 i = 0; i < n; ++i) { i32 c = get_sum (u, i); ans = (i64) ans * (r[i] + c) % mod; } printf ("%" PRIi32 "\n", ans); } int main (void) { run(); return 0; }