First Last
Time limit1sMemory limit2048 MB
Treat each word as an edge from its first letter to its last letter, then count the words that let Alice force a win when she picks them first in the chain game.
- Level
Medium6 of 10
- Topics
- Game theory, Graph
- Solved
- No attempts yet
Problem
Alice and Bob are playing a word game. They start with a list of words and take turns. Alice goes first and chooses a starting word from the list. On each later turn, the current player must choose a word from the list that starts with the letter that the previous player's word ends with. No word can be used more than once. A player who cannot choose a word loses.
Assuming both play optimally, how many words in the list, when Alice chooses one as her first word, lead to a win for her?
Input
The first line contains a single integer (), the number of words. Each of the next lines contains one word made only of the lowercase letters 'a' through 'z'. Each word is two to fifteen letters long. All words are distinct. At most three distinct letters appear at the beginnings and ends of all words. Alice and Bob may only choose words from this list.
Output
Output a single integer: the number of words that force a win for Alice if she chooses them first, assuming both players play optimally.