結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 ygussanyygussany
提出日時 2022-05-06 22:07:57
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 145 ms / 3,000 ms
コード長 2,714 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 31,360 KB
実行使用メモリ 23,780 KB
最終ジャッジ日時 2023-09-20 03:03:18
合計ジャッジ時間 7,088 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,928 KB
testcase_01 AC 3 ms
9,020 KB
testcase_02 AC 3 ms
9,504 KB
testcase_03 AC 2 ms
9,612 KB
testcase_04 AC 3 ms
9,512 KB
testcase_05 AC 3 ms
10,384 KB
testcase_06 AC 3 ms
9,316 KB
testcase_07 AC 3 ms
9,328 KB
testcase_08 AC 3 ms
9,440 KB
testcase_09 AC 3 ms
9,168 KB
testcase_10 AC 3 ms
9,400 KB
testcase_11 AC 3 ms
9,364 KB
testcase_12 AC 3 ms
8,948 KB
testcase_13 AC 3 ms
9,408 KB
testcase_14 AC 3 ms
8,696 KB
testcase_15 AC 7 ms
9,816 KB
testcase_16 AC 5 ms
9,380 KB
testcase_17 AC 4 ms
9,528 KB
testcase_18 AC 5 ms
9,020 KB
testcase_19 AC 4 ms
9,580 KB
testcase_20 AC 5 ms
9,024 KB
testcase_21 AC 3 ms
9,736 KB
testcase_22 AC 5 ms
9,112 KB
testcase_23 AC 4 ms
9,204 KB
testcase_24 AC 6 ms
9,644 KB
testcase_25 AC 6 ms
9,472 KB
testcase_26 AC 113 ms
21,124 KB
testcase_27 AC 64 ms
17,988 KB
testcase_28 AC 67 ms
17,416 KB
testcase_29 AC 140 ms
22,736 KB
testcase_30 AC 56 ms
16,472 KB
testcase_31 AC 80 ms
19,108 KB
testcase_32 AC 133 ms
22,344 KB
testcase_33 AC 63 ms
17,596 KB
testcase_34 AC 84 ms
20,176 KB
testcase_35 AC 66 ms
22,100 KB
testcase_36 AC 58 ms
19,360 KB
testcase_37 AC 67 ms
22,412 KB
testcase_38 AC 2 ms
6,736 KB
testcase_39 AC 87 ms
21,240 KB
testcase_40 AC 85 ms
21,240 KB
testcase_41 AC 87 ms
21,272 KB
testcase_42 AC 87 ms
21,168 KB
testcase_43 AC 84 ms
21,100 KB
testcase_44 AC 83 ms
21,080 KB
testcase_45 AC 87 ms
21,152 KB
testcase_46 AC 86 ms
21,264 KB
testcase_47 AC 83 ms
21,208 KB
testcase_48 AC 84 ms
21,184 KB
testcase_49 AC 56 ms
22,188 KB
testcase_50 AC 140 ms
23,772 KB
testcase_51 AC 119 ms
23,684 KB
testcase_52 AC 127 ms
23,780 KB
testcase_53 AC 130 ms
23,596 KB
testcase_54 AC 129 ms
23,668 KB
testcase_55 AC 57 ms
23,684 KB
testcase_56 AC 137 ms
22,592 KB
testcase_57 AC 135 ms
22,512 KB
testcase_58 AC 145 ms
22,876 KB
testcase_59 AC 82 ms
23,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
		}
		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];
			}
		}
		if (h[u].size > 0) {
			d = pop(&(h[u]));
			dp[u] += d.key;
		}
		if (h[u].size > 0) {
			d = pop(&(h[u]));
			dp[u] += d.key;
		}
	}
	printf("%d\n", dp[1]);
	fflush(stdout);
	return 0;
}
0