No.1612 I hate Construct a Palindrome
Problem Statement
P.P. has a phobia of palindromes and feels unpleasant whenever he sees one. This P.P. has become trapped in a connected undirected graph with $N$ vertices and $M$ edges, where each edge has a lowercase English letter as its label. P.P. is currently at vertex $1$, and it is known that the exit is at vertex $N$.
For P.P.'s sake, please find $1$ escape route such that the string formed by arranging the labels of the traversed edges in order is not a palindrome. You may pass through the same vertex or the same edge multiple times, but since it would be too cruel to P.P. if the route were excessively long, output one in which the total number of edge traversals is at most $2N$.
Input
$N$ $M$
$A_1\ \ B_1\ \ C_1$
$A_2\ \ B_2\ \ C_2$
$\vdots$
$A_M\ \ B_M\ \ C_M$
- $2 \le N \le 10^5$
- $1 \le M \le 2 \times 10^5$
- $1 \le A_i, B_i \le N,\ A_i \neq B_i \ \ (1 \le i \le M)$
- $C_i \ (1 \le i \le M)$ is a lowercase English letter.
- All values in the input except $C_i$ $(1 \le i \le M)$ are integers.
- The given graph is a connected undirected graph.
- The vertices are numbered $1, 2, \dots, N$.
- The edges are numbered $1, 2, \dots, M$.
- Edge $i$ connects vertex $A_i$ and vertex $B_i$ bidirectionally.
Output
Output a route from vertex $1$ to vertex $N$ such that the string formed by arranging the labels of the traversed edges in order is not a palindrome and the total number of edge traversals is at most $2N$.
On line $1$, output the total number of edge traversals $k$, and on lines $2$ through $k + 1$, output the numbers of the traversed edges in order. However, if no route satisfying the conditions exists, output only $-1$ on line $1$.
Sample
Sample 1
Input
3 3 1 2 a 2 3 a 1 3 b
Output
3 1 1 3
If he moves $1 \to 2 \to 1 \to 3$ through edges $1, 1, 3$, the string formed by arranging the labels in order is aab, which is not a palindrome.
There are other routes as well, such as the one through edges $1, 2, 3, 3$ that gives aabb, and outputting any of them is accepted.
If he moves $1 \to 3 \to 2 \to 1 \to 3$ through edges $3, 2, 1, 3$, the resulting string is baab, which is a palindrome, so this route is invalid.
Also, if he moves $1 \to 3 \to 2 \to 1 \to 3 \to 1 \to 2 \to 3$ through edges $3, 2, 1, 3, 3, 1, 2$, he obtains baabbaa, which is not a palindrome, but the total number of edge traversals is $7 > 6 = 2N$, so this route is invalid.
Sample 2
Input
4 4 1 2 a 1 2 b 2 3 a 3 4 b
Output
3 1 3 4
The graph in which P.P. is trapped is not necessarily simple.
Sample 3
Input
2 1 1 2 a
Output
-1
No matter how many times he traverses the only edge $1$, the result is only the palindrome aaa...a, so unfortunately P.P. cannot escape.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
tatyam
nok0