Submission #64115465


Source Code Expand

Copy
#include <stdio.h>
#include <stdlib.h>
int cmp_int(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return a < b ? -1 : a > b;
}
int ec[212345], *es[212345];
void ae(int f, int t) {
es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
if (es[f] == NULL) exit(2);
es[f][ec[f]++] = t;
}
struct syourai_s {
int to_node;
int res;
};
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>

int cmp_int(const void* x, const void* y) {
	int a = *(const int*)x, b = *(const int*)y;
	return a < b ? -1 : a > b;
}

int ec[212345], *es[212345];

void ae(int f, int t) {
	es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
	if (es[f] == NULL) exit(2);
	es[f][ec[f]++] = t;
}

struct syourai_s {
	int to_node;
	int res;
};

/* 最大頂点数の降順 */
int cmp_syourai(const void* x, const void* y) {
	struct syourai_s a = *(const struct syourai_s*)x, b = *(const struct syourai_s*)y;
	return a.res > b.res ? -1 : a.res < b.res;
}

struct syourai_s* syourai[212345];
int *memo[212345];

int get_idx(const int* table, int table_num, int query) {
	int l = 0,r = table_num - 1;
	while (l <= r) {
		int m = l + (r - l) / 2;
		if (table[m] == query) return m;
		else if (table[m] < query) l = m + 1;
		else r = m - 1;
	}
	return table_num;
}

int calc(int node, int from) {
	int *pmemo = &memo[node][get_idx(es[node], ec[node], from)];
	int i;
	int ans = 1, cnt = 0, cnt_needed = 3 + (from <= 0);
	if (*pmemo) return ~*pmemo;

	if (syourai[node] == NULL) {
		/* 来た元以外について、頂点数の最大値を求める */
		syourai[node] = malloc(sizeof(*syourai[node]) * ec[node]);
		for (i = 0; i < ec[node]; i++) {
			syourai[node][i].to_node = es[node][i];
			if (es[node][i] == from) {
				syourai[node][i].res = -1;
			} else {
				syourai[node][i].res = calc(es[node][i], node);
			}
		}
		qsort(syourai[node], ec[node], sizeof(*syourai[node]), cmp_syourai);
	} else if (syourai[node][ec[node] - 1].res < 0 && from != syourai[node][ec[node] - 1].to_node) {
		/* 欠けているデータを埋める */
		syourai[node][ec[node] - 1].res = calc(syourai[node][ec[node] - 1].to_node, node);
		qsort(syourai[node], ec[node], sizeof(*syourai[node]), cmp_syourai);
	}
	/* 来た元以外から、必要な分データを多い順に取る */
	for (i = 0; i < ec[node]; i++) {
		if (syourai[node][i].to_node != from) {
			ans += syourai[node][i].res;
			cnt++;
			if (cnt >= cnt_needed) break;
		}
	}
	/* データが足りない場合、最初の場合失格、そうでない場合その頂点だけを使う */
	if (cnt < cnt_needed) ans = from > 0;

	return ~(*pmemo = ~ans);
}

int main(void) {
	int N;
	int i;
	int ans = 0;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 1; i < N; i++) {
		int A, B;
		if (scanf("%d%d", &A, &B) != 2) return 1;
		ae(A, B);
		ae(B, A);
	}
	for (i = 1; i <= N; i++) {
		if (ec[i] > 0) qsort(es[i], ec[i], sizeof(*es[i]), cmp_int);
		memo[i] = calloc(ec[i] + 1, sizeof(*memo[i]));
	}
	for (i = 1; i <= N; i++) {
		int candidate = calc(i, 0);
		if (candidate > ans) ans = candidate;
	}
	printf("%d\n", ans - (ans == 0));
	return 0;
}

Submission Info

Submission Time
Task F - Alkane
User mikecat
Language C (gcc 12.2.0)
Score 500
Code Size 2817 Byte
Status AC
Exec Time 155 ms
Memory 39952 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 59
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All hand00.txt, hand01.txt, hand02.txt, hand03.txt, sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt
Case Name Status Exec Time Memory
hand00.txt AC 1 ms 1620 KB
hand01.txt AC 1 ms 1644 KB
hand02.txt AC 0 ms 1640 KB
hand03.txt AC 0 ms 1748 KB
sample00.txt AC 0 ms 1616 KB
sample01.txt AC 0 ms 1644 KB
sample02.txt AC 1 ms 1672 KB
testcase00.txt AC 0 ms 1760 KB
testcase01.txt AC 1 ms 1748 KB
testcase02.txt AC 0 ms 1736 KB
testcase03.txt AC 1 ms 1748 KB
testcase04.txt AC 102 ms 24672 KB
testcase05.txt AC 130 ms 26900 KB
testcase06.txt AC 38 ms 11312 KB
testcase07.txt AC 129 ms 26908 KB
testcase08.txt AC 37 ms 11060 KB
testcase09.txt AC 110 ms 27024 KB
testcase10.txt AC 46 ms 15000 KB
testcase11.txt AC 125 ms 32600 KB
testcase12.txt AC 54 ms 15232 KB
testcase13.txt AC 123 ms 26920 KB
testcase14.txt AC 95 ms 21932 KB
testcase15.txt AC 134 ms 26860 KB
testcase16.txt AC 104 ms 25328 KB
testcase17.txt AC 124 ms 26900 KB
testcase18.txt AC 121 ms 26848 KB
testcase19.txt AC 116 ms 26904 KB
testcase20.txt AC 59 ms 16360 KB
testcase21.txt AC 130 ms 26848 KB
testcase22.txt AC 79 ms 27748 KB
testcase23.txt AC 131 ms 39952 KB
testcase24.txt AC 116 ms 24680 KB
testcase25.txt AC 122 ms 25804 KB
testcase26.txt AC 60 ms 15092 KB
testcase27.txt AC 135 ms 25964 KB
testcase28.txt AC 112 ms 24372 KB
testcase29.txt AC 128 ms 25988 KB
testcase30.txt AC 104 ms 23112 KB
testcase31.txt AC 131 ms 25956 KB
testcase32.txt AC 62 ms 15420 KB
testcase33.txt AC 124 ms 26636 KB
testcase34.txt AC 24 ms 7548 KB
testcase35.txt AC 130 ms 26536 KB
testcase36.txt AC 60 ms 14268 KB
testcase37.txt AC 139 ms 26644 KB
testcase38.txt AC 78 ms 17020 KB
testcase39.txt AC 155 ms 26536 KB
testcase40.txt AC 113 ms 22312 KB
testcase41.txt AC 139 ms 26648 KB
testcase42.txt AC 49 ms 12708 KB
testcase43.txt AC 145 ms 26640 KB
testcase44.txt AC 100 ms 22644 KB
testcase45.txt AC 145 ms 26624 KB
testcase46.txt AC 45 ms 12376 KB
testcase47.txt AC 132 ms 26636 KB
testcase48.txt AC 32 ms 9324 KB
testcase49.txt AC 137 ms 26544 KB
testcase50.txt AC 123 ms 25468 KB
testcase51.txt AC 116 ms 26624 KB


2025-03-23 (Sun)
09:35:46 +09:00