Walk
Time limit5sMemory limit256 MB
Given up to one million missing strings among n-bit names, decide whether two present names are connected through single-bit flips avoiding blocked names.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Bit manipulation, Hash map
- Solved
- No attempts yet
Problem
The names of towns in Byteotia are distinct binary strings of exactly bits. There are towns in Byteotia, so exactly of the length- bit sequences name no town.
Some pairs of towns are directly connected by roads. Precisely, two towns are directly linked by a road if and only if their names differ in exactly one bit. Roads never cross outside of towns.
Byteasar wants to take a stroll from town to town , walking only along existing roads. Write a program that decides whether such a walk from to is possible.
Input
The first line contains two integers and separated by a single space (, , , ). Here is the length of a town name in bits and is the number of length- bit sequences that name no town.
The second line contains two strings separated by a single space, each a name of length over the characters 0 and 1. These are the names of towns and .
Each of the next lines contains one length- bit sequence that names no town, one per line, each a string of 0 and 1. Neither nor appears among these sequences.
Output
Print TAK (Polish for yes) on a single line if a walk from town to town is possible, and NIE (Polish for no) otherwise.
Hint
For instance, here are two possible walks from to :