#K61412. Message Relay Communication
Message Relay Communication
Message Relay Communication
You are given a set of messages exchanged between employees and several queries. Each message is described by a sender, a recipient, and a timestamp. For each query, you must determine if there exists a chain of messages (a communication relay) which starts from the specified start employee and reaches the specified end employee, while respecting the time constraints. More formally, given a query with start time \(t_s\) and end time \(t_e\), a valid communication chain is a sequence of messages \((m_1, m_2, \ldots, m_k)\) such that:
- The sender of \(m_1\) is the start employee and the recipient of \(m_k\) is the end employee.
- Each message \(m_i\) has a timestamp \(t_i\) satisfying \(t_s \le t_i \le t_e\).
- The timestamps are non-decreasing, i.e. \(t_1 \le t_2 \le \cdots \le t_k\).
Output "YES" if such a chain exists for the query, and "NO" otherwise.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- An integer \(N\) representing the number of messages.
- \(N\) lines follow, each containing a message in the format:
sender recipient timestamp
. sender and recipient are strings (without spaces) and timestamp is an integer. - An integer \(Q\) representing the number of queries.
- \(Q\) lines follow, each containing a query in the format:
start end start_time end_time
, where start and end are employee names (strings) and start_time and end_time are integers.
outputFormat
For each query, print either "YES" or "NO" (each on a separate line) to standard output (stdout) indicating whether a valid communication chain exists.
## sample6
Alice Bob 1
Bob Charlie 2
Charlie Dave 4
Alice Eve 3
Eve Bob 5
Bob Alice 6
3
Alice Dave 1 4
Alice Charlie 1 5
Eve Charlie 2 6
YES
YES
NO
</p>