結果
問題 | No.1928 Make a Binary Tree |
ユーザー | ygussany |
提出日時 | 2022-05-06 21:56:58 |
言語 | C (gcc 12.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,983 bytes |
コンパイル時間 | 806 ms |
コンパイル使用メモリ | 32,512 KB |
実行使用メモリ | 23,680 KB |
最終ジャッジ日時 | 2024-07-05 23:10:18 |
合計ジャッジ時間 | 5,669 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 4 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | WA | - |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 4 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | WA | - |
testcase_14 | AC | 3 ms
5,376 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | AC | 59 ms
15,872 KB |
testcase_36 | AC | 53 ms
19,712 KB |
testcase_37 | AC | 64 ms
22,416 KB |
testcase_38 | AC | 3 ms
5,376 KB |
testcase_39 | AC | 76 ms
18,688 KB |
testcase_40 | AC | 75 ms
18,496 KB |
testcase_41 | AC | 74 ms
18,488 KB |
testcase_42 | AC | 72 ms
18,620 KB |
testcase_43 | AC | 74 ms
18,560 KB |
testcase_44 | AC | 75 ms
18,548 KB |
testcase_45 | AC | 74 ms
18,524 KB |
testcase_46 | AC | 74 ms
18,504 KB |
testcase_47 | AC | 73 ms
18,656 KB |
testcase_48 | AC | 74 ms
18,528 KB |
testcase_49 | AC | 50 ms
15,744 KB |
testcase_50 | AC | 109 ms
23,680 KB |
testcase_51 | AC | 108 ms
23,552 KB |
testcase_52 | AC | 115 ms
23,552 KB |
testcase_53 | AC | 117 ms
23,680 KB |
testcase_54 | AC | 129 ms
23,680 KB |
testcase_55 | AC | 55 ms
23,552 KB |
testcase_56 | WA | - |
testcase_57 | WA | - |
testcase_58 | WA | - |
testcase_59 | AC | 82 ms
23,680 KB |
ソースコード
#include <stdio.h> #include <stdlib.h> typedef struct Edge { struct Edge *next; int v; } edge; typedef struct { int key, id; } data; typedef struct { data *obj; int size, max_size; } max_heap; void push(max_heap* h, data x) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key > h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(max_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key > h->obj[j].key) j ^= 1; if (h->obj[j].key > h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j <<= 1; } else break; } return output; } int main() { int i, N, u, w, deg[200001] = {}; edge *adj[200001] = {}, e[400001], *p; scanf("%d", &N); if (N == 1) { printf("1\n"); fflush(stdout); return 0; } for (i = 0; i < N - 1; i++) { scanf("%d %d", &u, &w); e[i*2].v = w; e[i*2+1].v = u; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); deg[u]++; deg[w]++; } int par[200001] = {}, q[200001], head, tail; par[1] = 1; q[0] = 1; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (par[w] == 0) { par[w] = u; q[tail++] = w; } } } int dp[200001]; max_heap h[200001]; data d; for (head--; head >= 0; head--) { u = q[head]; dp[u] = 1; h[u].max_size = deg[u]; h[u].size = 0; h[u].obj = (data*)malloc(sizeof(data) * (h[u].max_size + 1)); for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (w == par[u]) continue; d.key = dp[w]; d.id = w; push(&(h[u]), d); } if (h[u].size == 1) { d = pop(&(h[u])); dp[u] += d.key; free(h[u].obj); for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (w != par[u]) break; } h[u] = h[w]; if (h[u].size > 0) { d = pop(&(h[u])); dp[u] += d.key; } } else if (h[u].size > 1) { d = pop(&(h[u])); dp[u] += d.key; d = pop(&(h[u])); dp[u] += d.key; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (w == par[u] || h[w].size == 0) continue; if (h[u].size >= h[w].size) { if (h[u].size + h[w].size > h[u].max_size) { h[u].max_size *= 2; h[u].obj = (data*)realloc(h[u].obj, sizeof(data) * (h[u].max_size + 1)); } while (h[w].size > 0) { d = pop(&(h[w])); push(&(h[u]), d); } free(h[w].obj); } else { if (h[u].size + h[w].size > h[w].max_size) { h[w].max_size *= 2; h[w].obj = (data*)realloc(h[w].obj, sizeof(data) * (h[w].max_size + 1)); } while (h[u].size > 0) { d = pop(&(h[u])); push(&(h[w]), d); } free(h[u].obj); h[u] = h[w]; } } } } printf("%d\n", dp[1]); fflush(stdout); return 0; }