結果

問題 No.2337 Equidistant
ユーザー 👑 ygussanyygussany
提出日時 2023-06-02 22:45:10
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 533 ms / 4,000 ms
コード長 2,864 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 31,592 KB
実行使用メモリ 29,224 KB
最終ジャッジ日時 2023-08-28 04:58:31
合計ジャッジ時間 7,551 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,308 KB
testcase_01 AC 3 ms
6,864 KB
testcase_02 AC 2 ms
6,600 KB
testcase_03 AC 2 ms
6,012 KB
testcase_04 AC 2 ms
6,428 KB
testcase_05 AC 3 ms
5,516 KB
testcase_06 AC 3 ms
6,492 KB
testcase_07 AC 3 ms
7,232 KB
testcase_08 AC 3 ms
6,184 KB
testcase_09 AC 3 ms
5,644 KB
testcase_10 AC 3 ms
6,700 KB
testcase_11 AC 173 ms
29,144 KB
testcase_12 AC 174 ms
29,180 KB
testcase_13 AC 179 ms
29,140 KB
testcase_14 AC 174 ms
29,180 KB
testcase_15 AC 175 ms
29,124 KB
testcase_16 AC 190 ms
29,180 KB
testcase_17 AC 191 ms
29,220 KB
testcase_18 AC 181 ms
29,216 KB
testcase_19 AC 177 ms
29,220 KB
testcase_20 AC 176 ms
29,172 KB
testcase_21 AC 260 ms
29,124 KB
testcase_22 AC 136 ms
29,224 KB
testcase_23 AC 147 ms
29,188 KB
testcase_24 AC 346 ms
29,180 KB
testcase_25 AC 163 ms
29,124 KB
testcase_26 AC 533 ms
29,172 KB
testcase_27 AC 181 ms
29,184 KB
testcase_28 AC 181 ms
29,144 KB
権限があれば一括ダウンロードができます

ソースコード

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", N - size[u] - size[w]);
		}
	}
	fflush(stdout);
	return 0;
}
0