#P3950. Tribal Wars Connectivity
Tribal Wars Connectivity
Tribal Wars Connectivity
You are given N tribes arranged in a line. Initially, every two adjacent tribes are at peace (i.e. there is a connection between tribe i and tribe i+1 for all 1 ≤ i < N). You will then be given M events in chronological order. There are three types of events:
Q p q
: A query. A building worker from tribe $p$ wants to know whether it can reach tribe $q$ via a chain of connected (peaceful) tribes. You must printYes
orNo
(case‐sensitive).C p q
: A war begins between tribe $p$ and tribe $q$. It is guaranteed that $p$ and $q$ are adjacent and that the connection between them is currently active. This event removes the connection between these two tribes.U x
: The war that occurred in the $x$-th event (which is guaranteed to be a war event) has ended. The connection between the two tribes involved in that war is restored permanently.
The events are given in order. Process each event and for each query, output the answer on a new line.
inputFormat
The first line contains two integers N and M, where N is the number of tribes (numbered from 1 to N) and M is the number of events.
Each of the next M lines contains an event in one of the following forms:
Q p q
C p q
U x
It is guaranteed that for a C p q
event, tribes $p$ and $q$ are adjacent and currently connected, and for a U x
event, the war event at position $x$ has not yet been undone.
outputFormat
For each query event (Q p q
), output a single line containing either Yes
if tribe p can reach tribe q, or No
otherwise.
sample
5 7
C 2 3
Q 1 5
C 4 5
Q 3 5
U 1
Q 1 3
U 3
No
No
Yes
</p>