King & Weber
Time limit1sMemory limit128 MB
Given parallel/intersect observations between streets, check consistency and answer for each query whether two streets must be parallel, must intersect, or could be either.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, BFS, Implementation
- Solved
- No attempts yet
Problem
It is easy to get lost in Kitchener-Waterloo. Many streets that look mostly parallel actually intersect one another, sometimes multiple times. The best-known example is King Street and Weber Street. Other examples include Westmount and Fischer-Hallman, University and Erb, and Queen and Highland.
Navigation is easier in cities that respect the "Manhattan Assumption": every street is a straight line in the Euclidean plane, and any two streets are either parallel or perpendicular to each other. (Note that even Manhattan itself does not fully satisfy this assumption.)
The input describes one particular city as a sequence of observations followed by a sequence of queries. Each observation asserts either that two streets are parallel or that they intersect. Each query asks whether two streets must be parallel or must intersect, assuming the city satisfies the Manhattan Assumption.
Input
The first line contains two integers and ().
Each of the next lines contains one observation: three space-separated words, namely the two street names followed by the word parallel or the word intersect.
Each street name is a string of at most 100 uppercase or lowercase English letters, and names are case-sensitive.
The observations are followed by queries, each on its own line. Each query consists of two street names separated by a space.
Output
If it is impossible for the city to satisfy both the Manhattan Assumption and all of the given observations, output a single line containing the word Waterloo.
Otherwise, output lines with the answers to the queries. Each answer is one of the three words parallel, intersect, or unknown:
- Output
parallelif the two queried streets are parallel in every city that satisfies the observations and the Manhattan Assumption. - Output
intersectif they are perpendicular in every such city. - Output
unknownif they are parallel in some such city and perpendicular in another.