King & Weber

Time limit1sMemory limit128 MB

Summary
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 mm and nn (1≤m,n≤1000001 \le m, n \le 100000).

Each of the next mm 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 nn 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 nn lines with the answers to the nn queries. Each answer is one of the three words parallel, intersect, or unknown:

  • Output parallel if the two queried streets are parallel in every city that satisfies the observations and the Manhattan Assumption.
  • Output intersect if they are perpendicular in every such city.
  • Output unknown if they are parallel in some such city and perpendicular in another.

Examples5

  1. Example 1

    Input
    3 3
    fourthstreet fifthstreet parallel
    fifthstreet sixthstreet parallel
    fourthavenue fifthstreet intersect
    sixthstreet fourthstreet
    sixthstreet fourthavenue
    sixthstreet King
    
    Expected output
    parallel
    intersect
    unknown
    
  2. Example 2

    Input
    2 1
    King Weber parallel
    King Weber intersect
    King Weber
    
    Expected output
    Waterloo
    
  3. Example 3

    Input
    1 1
    a b intersect
    a b
    
    Expected output
    intersect
    
  4. Example 4

    Input
    2 1
    a b parallel
    b c parallel
    a c
    
    Expected output
    parallel
    
  5. Example 5

    Input
    1 1
    a b parallel
    x y
    
    Expected output
    unknown