eolymp
bolt
Try our new interface for solving problems
Məsələlər

Morphing is fun

Morphing is fun

Morphic is a tree that grows very rapidly, bringing happiness to its owner. It has a single trunk consisting of a number of cells stacked one on top of another. Each cell has one of \textbf{n} possible colors which determine the way it mutates during the night, while nobody can see it. Florists denote these colors by the first \textbf{n} small letters of the English alphabet and know exactly into how many cells, and of what colors, a cell of each color divides. In fact, they have wrote their knowledge down simply with \textbf{n} nonempty words, each word representing the resulting sequence of colors. A seed of a Morphic has a single cell of color \textbf{a} and is rooted firmly in the ground. As long as the Morphic is still alive, each night all its cells simultaniously morph according to the aforementioned rules, possibly causing an exponential growth because each new cell is of the same size as the original one. For example, if rules say that \textbf{a} becomes \textbf{ab}, and \textbf{b} becomes \textbf{ca}, then after two nights a seed will evolve to a trunk consisting of \textbf{4} cells: \textbf{abca}. Therefore the top of a Morphic is usually hidden in clouds. The only way to tell if it is still alive is to check if visible part of the trunk is changing colors. In order to do so, one can build enormously high (yet still of constant height) tower, and watch from its top a fixed fragment of the trunk. As you can easily see, it is either sufficient to observe first \textbf{k} cells from the bottom for some fixed \textbf{k}, or no matter how high the tower is, you will not be able to tell for sure if a Morphic died. The latter happens when for every \textbf{k}, rules cause the \textbf{k}-th cell to eventually stop changing colors, even though the tree is still alive and mutating. To prevent waste of money on building such enormous towers, you are to write a program that determines if it is possible to monitor health of a Morphic. \InputFile The input contains several Morphics descriptions. The first line contains the number of descriptions \textbf{t} (\textbf{t} ≤ \textbf{10^4}) that follow. Each of them begins with the number of colors \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{26}). Next \textbf{n} lines contain the rules by which the Morphic grows. The \textbf{i}-th one describes the sequence of colors in bottom-up order obtained from a single cell of \textbf{i}-th color. Each line contains at most \textbf{100} lowercase English letters. \OutputFile For each test case output one line containing "\textbf{YES}" if building of a tower is pointless (as in: \textbf{YES}, we can save money!). Otherwise output "\textbf{NO}".
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
4
2
ab
a
3
ba
c
c
3
ba
c
b
3
bbbbbbbbbbbbbbb
ccccccccccccccc
c
Çıxış verilənləri #1
YES
YES
NO
YES
Mənbə Central European Programming Contest 2008, Wroclaw, Poland, November 28-30, 2008