結果

問題 No.1494 LCS on Tree
ユーザー 👑 ygussanyygussany
提出日時 2021-04-30 23:02:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,485 bytes
コンパイル時間 1,587 ms
コンパイル使用メモリ 31,900 KB
実行使用メモリ 33,276 KB
最終ジャッジ日時 2023-08-12 22:46:47
合計ジャッジ時間 4,735 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 42 ms
33,160 KB
testcase_04 AC 23 ms
33,228 KB
testcase_05 AC 22 ms
33,264 KB
testcase_06 AC 22 ms
33,164 KB
testcase_07 AC 23 ms
33,276 KB
testcase_08 AC 25 ms
33,128 KB
testcase_09 AC 22 ms
33,216 KB
testcase_10 AC 23 ms
33,236 KB
testcase_11 AC 21 ms
33,212 KB
testcase_12 AC 22 ms
33,116 KB
testcase_13 AC 22 ms
33,120 KB
testcase_14 AC 39 ms
33,228 KB
testcase_15 AC 39 ms
33,260 KB
testcase_16 AC 35 ms
33,216 KB
testcase_17 AC 36 ms
33,228 KB
testcase_18 AC 37 ms
33,092 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,384 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 40 ms
33,212 KB
testcase_31 AC 40 ms
33,180 KB
testcase_32 AC 40 ms
33,092 KB
testcase_33 AC 38 ms
33,220 KB
testcase_34 AC 39 ms
33,092 KB
testcase_35 AC 3 ms
10,436 KB
testcase_36 AC 12 ms
28,920 KB
testcase_37 AC 3 ms
6,888 KB
testcase_38 AC 6 ms
12,200 KB
testcase_39 AC 13 ms
28,920 KB
testcase_40 AC 5 ms
14,092 KB
testcase_41 AC 16 ms
28,660 KB
testcase_42 AC 4 ms
9,188 KB
testcase_43 AC 17 ms
31,700 KB
testcase_44 AC 14 ms
27,680 KB
testcase_45 AC 56 ms
33,180 KB
testcase_46 AC 57 ms
33,180 KB
testcase_47 AC 59 ms
33,152 KB
testcase_48 AC 58 ms
33,092 KB
testcase_49 AC 56 ms
33,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v;
	char c;
} list;

void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}

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

int main()
{
	int i, N, s, u, w, x;
	char S[2001], c;
	list *adj[2001] = {}, e[4001], *p, *pp;
	scanf("%d", &N);
	scanf("%s", S);
	for (s = 0; S[s] != 0; s++);
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d %c ", &u, &w, &c);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].c = c;
		e[i*2+1].c = c;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int par[2001] = {}, q[2001], 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 j, k, l, r, m, LCS[2][2001] = {}, LCS_pos[2][2001][2001], tmp[2001], ans = 0;
	for (u = 1; u <= N; u++) {
		for (i = 1, LCS_pos[0][u][0] = -1, LCS_pos[1][u][0] = s; i <= s; i++) {
			LCS_pos[0][u][i] = s;
			LCS_pos[1][u][i] = -1;
		}
	}
	for (head--; head >= 0; head--) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			c = p->c;
			if (w == par[u]) continue;
			
			for (i = s - 1; i >= 0; i--) {
				if (S[i] != c) continue;
				l = 0;
				r = LCS[0][w] + 1;
				while (l < r) {
					m = (l + r) / 2;
					if (LCS_pos[0][w][m] < i) l = m + 1;
					else r = m;
				}
				LCS_pos[0][w][l] = i;
				if (l == LCS[0][w] + 1) LCS[0][w]++;
			}
			for (i = 1; i <= LCS[0][w]; i++) chmin(&(LCS_pos[0][u][i]), LCS_pos[0][w][i]);
			chmax(&(LCS[0][u]), LCS[0][w]);
			
			for (i = 0; i < s; i++) {
				if (S[i] != c) continue;
				l = 0;
				r = LCS[1][w] + 1;
				while (l < r) {
					m = (l + r) / 2;
					if (LCS_pos[1][w][m] > i) l = m + 1;
					else r = m;
				}
				LCS_pos[1][w][l] = i;
				if (l == LCS[1][w] + 1) LCS[1][w]++;
			}
			for (i = 1; i <= LCS[1][w]; i++) chmax(&(LCS_pos[1][u][i]), LCS_pos[1][w][i]);
			chmax(&(LCS[1][u]), LCS[1][w]);
		}
		chmax(&ans, LCS[0][u]);
		chmax(&ans, LCS[1][u]);
		
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (w == par[u]) continue;
			for (pp = adj[u]; pp != NULL; pp = pp->next) {
				x = pp->v;
				if (x == par[u] || x == w) continue;
				for (i = 0, j = LCS[1][x]; i <= LCS[0][w]; i++) {
					for (; LCS_pos[1][x][j] <= LCS_pos[0][w][i]; j--);
					chmax(&ans, i + j);
				}
			}
		}
	}
	printf("%d\n", ans);
	fflush(stdout);
	return 0;
}
0