結果

問題 No.2337 Equidistant
ユーザー 👑 ygussanyygussany
提出日時 2023-06-02 22:42:45
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,870 bytes
コンパイル時間 811 ms
コンパイル使用メモリ 31,464 KB
実行使用メモリ 29,240 KB
最終ジャッジ日時 2023-08-28 04:55:29
合計ジャッジ時間 7,920 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,148 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,552 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,516 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 359 ms
29,180 KB
testcase_22 AC 155 ms
29,132 KB
testcase_23 WA -
testcase_24 AC 454 ms
29,184 KB
testcase_25 WA -
testcase_26 AC 630 ms
29,144 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

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

void init_LCA(int N, edge* adj[], int par[][20], int depth[])
{
	static int i, u, w, x, q[200001], head, tail;
	static edge *p;
	for (u = 1, depth[0] = -1; u <= N; u++) {
		for (i = 0; i < 20; i++) par[u][i] = 0;
		depth[u] = -1;
	}
	par[1][0] = 0;
	depth[1] = 0;
	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 (depth[w] < 0) {
				par[w][0] = u;
				depth[w] = depth[u] + 1;
				for (i = 0, x = u; x != 0; x = par[x][i++]) par[w][i+1] = par[x][i];
				q[tail++] = w;
			}
		}
	}
}

int get_LCA(int par[][20], int depth[], int u, int w)
{
	int i;
	while (depth[u] > depth[w]) {
		for (i = 1; depth[par[u][i]] >= depth[w]; i++); 
		u = par[u][i-1];
	}
	while (depth[u] < depth[w]) {
		for (i = 1; depth[par[w][i]] >= depth[u]; i++); 
		w = par[w][i-1];
	}
	while (u != w) {
		for (i = 1; par[u][i] != par[w][i]; i++); 
		u = par[u][i-1];
		w = par[w][i-1];
	}
	return u;
}

int main()
{
	int i, N, Q, u, w;
	edge *adj[200001] = {}, e[400001], *p;
	scanf("%d %d", &N, &Q);
	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]);
	}
	
	static int par[200001] = {}, size[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;
			}
		}
	}
	for (head--; head >= 0; head--) {
		u = q[head];
		size[u]++;
		if (head > 0) size[par[u]] += size[u];
	}
	
	int s, t, r;
	static int depth[200001], LCA_par[200001][20];
	init_LCA(N, adj, LCA_par, depth);
	while (Q--) {
		scanf("%d %d", &s, &t);
		if ((depth[s] + depth[t]) % 2 != 0) {
			printf("0\n");
			continue;
		}
		r = get_LCA(LCA_par, depth, s, t);
		if (depth[s] < depth[t]) {
			w = t;
			while (depth[w] > (depth[t] - depth[s]) / 2 + depth[r] + 1) {
				for (i = 1; depth[LCA_par[w][i]] > (depth[t] - depth[s]) / 2 + depth[r]; i++); 
				w = LCA_par[w][i-1];
			}
			printf("%d\n", size[par[w]] - size[w]);
		} else if (depth[s] > depth[t]) {
			u = s;
			while (depth[u] > (depth[s] - depth[t]) / 2 + depth[r] + 1) {
				for (i = 1; depth[LCA_par[u][i]] > (depth[s] - depth[t]) / 2 + depth[r]; i++); 
				u = LCA_par[u][i-1];
			}
			printf("%d\n", size[par[u]] - size[u]);
		} else {
			u = s;
			w = t;
			while (depth[u] > depth[r] + 1) {
				for (i = 1; depth[LCA_par[u][i]] > depth[r]; i++); 
				u = LCA_par[u][i-1];
			}
			while (depth[w] > depth[r] + 1) {
				for (i = 1; depth[LCA_par[w][i]] > depth[r]; i++); 
				w = LCA_par[w][i-1];
			}
			printf("%d\n", size[r] - size[u] - size[w]);
		}
	}
	fflush(stdout);
	return 0;
}
0