結果

問題 No.1612 I hate Construct a Palindrome
ユーザー ygussanyygussany
提出日時 2021-06-19 22:24:17
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,904 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 32,560 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-07-17 15:14:27
合計ジャッジ時間 3,168 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 57 ms
9,600 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 62 ms
9,600 KB
testcase_06 AC 41 ms
8,704 KB
testcase_07 AC 42 ms
8,960 KB
testcase_08 AC 41 ms
8,704 KB
testcase_09 AC 39 ms
8,576 KB
testcase_10 AC 41 ms
8,576 KB
testcase_11 AC 39 ms
8,704 KB
testcase_12 AC 42 ms
8,960 KB
testcase_13 AC 44 ms
8,960 KB
testcase_14 AC 43 ms
8,960 KB
testcase_15 AC 77 ms
11,776 KB
testcase_16 AC 76 ms
11,776 KB
testcase_17 AC 78 ms
11,648 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v, id;
} list;

int is_palindrome(char S[])
{
	int i, j;
	for (j = 0; S[j] != 0; j++);
	for (i = 0, j--; i < j; i++, j--) if (S[i] != S[j]) break;
	if (i < j) return 0;
	else return 1;
}

int solve(int N, int M, list* adj[], list e[], char c[], int ans[])
{
	int i;
	list *p;
	for (i = 2; i <= M; i++) if (c[i] != c[1]) break;
	if (i > M) return -1;
	
	int u, w, x = adj[1]->v, par[2][100001][2], dist[2][100001] = {}, q[100001], head, tail;
	dist[0][x] = 1;
	q[0] = x;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (dist[0][w] == 0) {
				dist[0][w] = dist[0][u] + 1;
				par[0][w][0] = u;
				par[0][w][1] = p->id;
				q[tail++] = w;
			}
		}
	}
	dist[1][N] = 1;
	q[0] = N;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (dist[1][w] == 0) {
				dist[1][w] = dist[1][u] + 1;
				par[1][w][0] = u;
				par[1][w][1] = p->id;
				q[tail++] = w;
			}
		}
	}
	
	int j, k = 0, y, z;
	char S[200001];
	for (j = 1, i = adj[1]->id; j <= M; j++) if (c[j] != c[i]) break;
	y = e[(j-1)*2].v;
	z = e[j*2-1].v;
	ans[k] = i;
	S[k++] = c[i];
	if (dist[0][y] <= dist[0][z]) {
		k += dist[0][y] - 1;
		for (i = k - 1, u = y; u != x; i--, u = par[0][u][0]) {
			ans[i] = par[0][u][1];
			S[i] = c[ans[i]];
		}

		if (dist[1][y] < dist[1][z]) {
			ans[k] = j;
			S[k++] = c[j];
			ans[k] = j;
			S[k++] = c[j];
			for (u = y; u != N; k++, u = par[1][u][0]) {
				ans[k] = par[1][u][1];
				S[k] = c[ans[k]];
			}
		} else {
			ans[k] = j;
			S[k++] = c[j];
			for (u = z; u != N; k++, u = par[1][u][0]) {
				ans[k] = par[1][u][1];
				S[k] = c[ans[k]];
			}
		}
	} else {
		k += dist[0][z] - 1;
		for (i = k - 1, u = z; u != x; i--, u = par[0][u][0]) {
			ans[i] = par[0][u][1];
			S[i] = c[ans[i]];
		}
		
		if (dist[1][y] > dist[1][z]) {
			ans[k] = j;
			S[k++] = c[j];
			ans[k] = j;
			S[k++] = c[j];
			for (u = z; u != N; k++, u = par[1][u][0]) {
				ans[k] = par[1][u][1];
				S[k] = c[ans[k]];
			}
		} else {
			ans[k] = j;
			S[k++] = c[j];
			for (u = y; u != N; k++, u = par[1][u][0]) {
				ans[k] = par[1][u][1];
				S[k] = c[ans[k]];
			}
		}
	}
	
	if (is_palindrome(S) == 0) return k;
	else {
		ans[k] = ans[k-1];
		ans[k+1] = ans[k];
		return k + 2;
	}
}

int main()
{
	int i, N, M, u, w;
	char c[200001];
	list *adj[100001] = {}, e[400001];
	scanf("%d %d", &N, &M);
	for (i = 0; i < M; i++) {
		scanf("%d %d %c ", &u, &w, &(c[i+1]));
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].id = i + 1;
		e[i*2+1].id = i + 1;
		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 ans[200001], k = solve(N, M, adj, e, c, ans);
	printf("%d\n", k);
	if (k >= 0) for (i = 0; i < k; i++) printf("%d\n", ans[i]);
	fflush(stdout);
	return 0;
}
0