結果

問題 No.1718 Random Squirrel
ユーザー 👑 ygussanyygussany
提出日時 2021-10-10 12:01:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,554 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 31,596 KB
実行使用メモリ 22,264 KB
最終ジャッジ日時 2023-10-24 11:32:52
合計ジャッジ時間 2,502 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,168 KB
testcase_01 AC 2 ms
6,168 KB
testcase_02 AC 2 ms
6,168 KB
testcase_03 AC 2 ms
6,168 KB
testcase_04 AC 2 ms
6,168 KB
testcase_05 AC 2 ms
6,168 KB
testcase_06 AC 2 ms
6,168 KB
testcase_07 AC 1 ms
6,168 KB
testcase_08 AC 2 ms
6,168 KB
testcase_09 AC 2 ms
6,168 KB
testcase_10 AC 2 ms
6,168 KB
testcase_11 AC 30 ms
7,276 KB
testcase_12 AC 7 ms
6,168 KB
testcase_13 AC 21 ms
6,408 KB
testcase_14 AC 29 ms
7,360 KB
testcase_15 AC 32 ms
6,984 KB
testcase_16 AC 15 ms
6,168 KB
testcase_17 AC 28 ms
7,168 KB
testcase_18 AC 44 ms
7,668 KB
testcase_19 AC 30 ms
6,904 KB
testcase_20 AC 12 ms
6,168 KB
testcase_21 AC 44 ms
8,204 KB
testcase_22 AC 54 ms
8,204 KB
testcase_23 AC 42 ms
8,208 KB
testcase_24 AC 41 ms
8,208 KB
testcase_25 AC 54 ms
8,208 KB
testcase_26 AC 30 ms
8,204 KB
testcase_27 AC 30 ms
8,204 KB
testcase_28 AC 29 ms
8,204 KB
testcase_29 AC 44 ms
22,264 KB
testcase_30 AC 45 ms
22,264 KB
testcase_31 AC 44 ms
22,264 KB
testcase_32 AC 44 ms
22,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct Edge {
	struct Edge *next;
	int v;
} edge;

void chmax(int* a, int b)
{
	if (*a < b) *a = b;
}

void DFS(edge* adj[], int D[], int u, int par[], int size[], int max_depth[], int ans[])
{
	int w, max[2] = {0, 0};
	edge *p;
	for (p = adj[u]; p != NULL; p = p->next) {
		w = p->v;
		if (max[0] < max_depth[w]) {
			max[1] = max[0];
			max[0] = max_depth[w];
		} else if (max[1] < max_depth[w]) max[1] = max_depth[w];
	}
	ans[u] = (size[u] - 1) * 2 - max[0];
	
	int tmp;
	for (p = adj[u]; p != NULL; p = p->next) {
		w = p->v;
		if (w == par[u]) continue;
		if (max_depth[w] == 0) {
			size[w] = size[u] + 1;
			max_depth[w] = max_depth[u] + 1;
			DFS(adj, D, w, par, size, max_depth, ans);
		} else if (max_depth[w] == max[0]) {
			tmp = max_depth[u];
			if (max[1] != 0 || D[u] != 0) {
				size[u] -= size[w];
				max_depth[u] = max[1] + 1;
			} else {
				size[u] = 0;
				max_depth[u] = 0;
			}
			size[w] += size[u];
			if (max_depth[u] > 0) chmax(&(max_depth[w]), max_depth[u] + 1);
			DFS(adj, D, w, par, size, max_depth, ans);
			size[w] -= size[u];
			if (max[1] != 0 || D[u] != 0) size[u] += size[w];
			else size[u] = size[w] + 1;
			max_depth[u] = tmp;
		} else {
			size[u] -= size[w];
			size[w] += size[u];
			tmp = max_depth[u];
			max_depth[u] = max[0] + 1;
			max_depth[w] = max_depth[u] + 1;
			DFS(adj, D, w, par, size, max_depth, ans);
			size[w] -= size[u];
			size[u] += size[w];
			max_depth[u] = tmp;
		}
	}
}

int main()
{
	int i, N, K, u, w, D[100001] = {};
	edge *adj[100001] = {}, e[200001], *p;
	scanf("%d %d", &N, &K);
	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]);
	}
	for (i = 1; i <= K; i++) {
		scanf("%d", &u);
		D[u] = 1;
	}
	
	int par[100001] = {}, q[100001], 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 size[100001] = {}, max_depth[100001] = {};
	for (head--; head >= 0; head--) {
		u = q[head];
		if (size[u] == 0) size[u] += D[u];
		else size[u]++;
		if (u != 1) size[par[u]] += size[u];
		if (size[u] != 0) {
			chmax(&(max_depth[u]), 1);
			if (u != 1) chmax(&(max_depth[par[u]]), max_depth[u] + 1);
		}
	}
	
	int ans[100001];
	DFS(adj, D, 1, par, size, max_depth, ans);
	for (u = 1; u <= N; u++) printf("%d\n", ans[u]);
	fflush(stdout);
	return 0;
}
0