atcoder#ABC197F. [ABC197F] Construct a Palindrome
[ABC197F] Construct a Palindrome
Score : points
Problem Statement
We have a connected undirected graph with vertices and edges, which is not necessarily simple. Edge connects Vertex and Vertex , and has a character written on it. Let us make a string by choosing a path from Vertex to Vertex (possibly with repetitions of the same edge and vertex) and arrange the characters written on the edges in that path. Determine whether this string can be a palindrome. If it can, find the minimum possible length of such a palindrome.
Constraints
- is a lowercase English letter.
- The given graph is connected.
Input
Input is given from Standard Input in the following format:
Output
If a palindrome can be made, print the minimum possible length of such a palindrome; otherwise, print -1
.
8 8
1 2 a
2 3 b
1 3 c
3 4 b
4 5 a
5 6 c
6 7 b
7 8 a
10
By traversing the edges in the order , we can make a string abcabbacba
, which is a palindrome.
It is impossible to make a shorter palindrome, so the answer is .
4 5
1 1 a
1 2 a
2 3 a
3 4 b
4 4 a
5
By traversing the edges in the order , we can make a string aabaa
, which is a palindrome.
Note that repetitions of the same edge and vertex are allowed.
3 4
1 1 a
1 2 a
2 3 b
3 3 b
-1
We cannot make a palindrome.