結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 ygussanyygussany
提出日時 2022-05-06 21:56:58
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,983 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 31,396 KB
実行使用メモリ 23,796 KB
最終ジャッジ日時 2023-09-20 02:49:50
合計ジャッジ時間 6,388 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,812 KB
testcase_01 AC 2 ms
8,672 KB
testcase_02 AC 2 ms
8,800 KB
testcase_03 AC 3 ms
9,228 KB
testcase_04 AC 3 ms
8,680 KB
testcase_05 AC 3 ms
9,164 KB
testcase_06 WA -
testcase_07 AC 3 ms
9,444 KB
testcase_08 AC 3 ms
9,288 KB
testcase_09 AC 3 ms
9,436 KB
testcase_10 AC 3 ms
9,552 KB
testcase_11 AC 3 ms
8,976 KB
testcase_12 AC 3 ms
9,060 KB
testcase_13 WA -
testcase_14 AC 3 ms
9,308 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 57 ms
15,848 KB
testcase_36 AC 54 ms
19,736 KB
testcase_37 AC 65 ms
22,368 KB
testcase_38 AC 2 ms
4,700 KB
testcase_39 AC 78 ms
18,592 KB
testcase_40 AC 76 ms
18,524 KB
testcase_41 AC 78 ms
18,456 KB
testcase_42 AC 76 ms
18,552 KB
testcase_43 AC 77 ms
18,636 KB
testcase_44 AC 78 ms
18,552 KB
testcase_45 AC 77 ms
18,580 KB
testcase_46 AC 79 ms
18,660 KB
testcase_47 AC 77 ms
18,656 KB
testcase_48 AC 79 ms
18,536 KB
testcase_49 AC 51 ms
15,844 KB
testcase_50 AC 130 ms
23,756 KB
testcase_51 AC 122 ms
23,704 KB
testcase_52 AC 123 ms
23,656 KB
testcase_53 AC 125 ms
23,708 KB
testcase_54 AC 122 ms
23,704 KB
testcase_55 AC 56 ms
23,796 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 82 ms
23,784 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);
		}
		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;
}
0