Submission #70526137


Source Code Expand

Copy
#include <stdio.h>
#include <stdlib.h>
int ec[512345], *es[512345];
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;
}
int cmp(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return (a > b) - (a < b);
}
int find(const int* arr, int size, int target) {
int l = 0, r = size - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == target) return m;
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>

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

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;
}

int cmp(const void* x, const void* y) {
	int a = *(const int*)x, b = *(const int*)y;
	return (a > b) - (a < b);
}

int find(const int* arr, int size, int target) {
	int l = 0, r = size - 1;
	while (l <= r) {
		int m = l + (r - l) / 2;
		if (arr[m] == target) return m;
		else if (arr[m] < target) l = m + 1;
		else r = m - 1;
	}
	return size;
}

struct res_s {
	int vert, depth;
};

struct res_s* memo[512345];
char* memo_valid[512345];

struct res_info_s {
	int vert;
	struct res_s res;
};

int cmp_info(const void* x, const void* y) {
	struct res_info_s a = *(const struct res_info_s*)x, b = *(const struct res_info_s*)y;
	if (a.res.depth != b.res.depth) {
		return (a.res.depth < b.res.depth) - (a.res.depth > b.res.depth);
	}
	return (a.res.vert < b.res.vert) - (a.res.vert > b.res.vert);
}

struct res_info_s* info[512345];
int info_status[512345]; /* 0:未調査 1~N:そこのみ未調査 -1:調査済 */

struct res_s solve(int vert, int from) {
	struct res_s ans;
	int from_id = find(es[vert], ec[vert], from);
	if (memo_valid[vert][from_id]) return memo[vert][from_id];

	if (info_status[vert] == 0) {
		/* フル調査 */
		int i;
		info[vert] = malloc(sizeof(*info[vert]) * ec[vert]);
		info_status[vert] = -1;
		for (i = 0; i < ec[vert]; i++) {
			info[vert][i].vert = es[vert][i];
			if (es[vert][i] != from) {
				info[vert][i].res = solve(es[vert][i], vert);
			} else {
				info[vert][i].res = (struct res_s){ vert, 0 };
				info_status[vert] = es[vert][i];
			}
		}
		qsort(info[vert], ec[vert], sizeof(*info[vert]), cmp_info);
	} else if (info_status[vert] > 0) {
		/* 足りないところだけ調査 */
		int i;
		for (i = 0; i < ec[vert]; i++) {
			if (info[vert][i].vert == info_status[vert]) {
				info[vert][i].res = solve(info[vert][i].vert, vert);
			}
		}
		info_status[vert] = -1;
		qsort(info[vert], ec[vert], sizeof(*info[vert]), cmp_info);
	}

	if (info[vert][0].vert != from) {
		ans.vert = info[vert][0].res.vert;
		ans.depth = info[vert][0].res.depth + 1;
	} else if (ec[vert] > 1 && info[vert][1].vert != from) {
		ans.vert = info[vert][1].res.vert;
		ans.depth = info[vert][1].res.depth + 1;
	} else {
		ans.vert = vert;
		ans.depth = 0;
	}
	memo[vert][from_id] = ans;
	memo_valid[vert][from_id] = 1;
	return ans;
}

int main(void) {
	int N, i;
	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);
		memo[i] = malloc(sizeof(*memo[i]) * (ec[i] + 1));
		memo_valid[i] = calloc(ec[i] + 1, sizeof(*memo_valid[i]));
	}
	for (i = 1; i <= N; i++) {
		struct res_s ans = solve(i, 0);
		printf("%d\n", ans.vert);
	}
	return 0;
}

Submission Info

Submission Time
Task E - Farthest Vertex
User mikecat
Language C (gcc 12.2.0)
Score 475
Code Size 3065 Byte
Status AC
Exec Time 462 ms
Memory 131752 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 2
AC × 31
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 02_path_00.txt, 02_path_01.txt, 02_path_02.txt, 02_path_03.txt, 03_star_1_00.txt, 03_star_1_01.txt, 03_star_1_02.txt, 04_star_2_00.txt, 04_star_2_01.txt, 04_star_2_02.txt, 04_star_2_03.txt, 04_star_2_04.txt, 05_star_3_00.txt, 05_star_3_01.txt, 05_star_3_02.txt, 06_corner_00.txt, 06_corner_01.txt, 06_corner_02.txt, 06_corner_03.txt, 06_corner_04.txt, 06_corner_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 0 ms 1588 KiB
00_sample_01.txt AC 0 ms 1612 KiB
01_random_00.txt AC 207 ms 55196 KiB
01_random_01.txt AC 380 ms 92412 KiB
01_random_02.txt AC 257 ms 68152 KiB
01_random_03.txt AC 388 ms 92420 KiB
01_random_04.txt AC 264 ms 69844 KiB
01_random_05.txt AC 376 ms 92352 KiB
01_random_06.txt AC 407 ms 89812 KiB
01_random_07.txt AC 392 ms 92372 KiB
02_path_00.txt AC 190 ms 131752 KiB
02_path_01.txt AC 434 ms 122088 KiB
02_path_02.txt AC 427 ms 116496 KiB
02_path_03.txt AC 437 ms 131360 KiB
03_star_1_00.txt AC 216 ms 98236 KiB
03_star_1_01.txt AC 255 ms 104108 KiB
03_star_1_02.txt AC 363 ms 104076 KiB
04_star_2_00.txt AC 175 ms 86176 KiB
04_star_2_01.txt AC 462 ms 93556 KiB
04_star_2_02.txt AC 363 ms 86172 KiB
04_star_2_03.txt AC 435 ms 96940 KiB
04_star_2_04.txt AC 428 ms 110664 KiB
05_star_3_00.txt AC 418 ms 85540 KiB
05_star_3_01.txt AC 406 ms 86104 KiB
05_star_3_02.txt AC 410 ms 86092 KiB
06_corner_00.txt AC 191 ms 117288 KiB
06_corner_01.txt AC 193 ms 117232 KiB
06_corner_02.txt AC 442 ms 117344 KiB
06_corner_03.txt AC 445 ms 117288 KiB
06_corner_04.txt AC 453 ms 114560 KiB
06_corner_05.txt AC 442 ms 114464 KiB


2025-10-29 (Wed)
02:03:15 +09:00