Crop Network
Farmer Tom maintains an array of crop fields throughout the farming season and underneath these fields lie a vast subterranean network of irrigation pipes, tanks, and valves. Tom has employed a new AI system that will toggle the valves connecting the irrigation pipes between any two fields, and this system will constantly toggle these valves throughout the season in order to save water and increase irrigation efficiency, delivering water flow to the fields that need it most. The pipes are bidirectional, meaning water can flow both ways.
Thus, using various sensors and signals, the valves between any two fields may change throughout the season, toggling to either allow or block water flow depending on the previous state. However, there has been some flooding in the area recently and the environment authority has been regularly auditing you.
The inspector points to two fields and questions whether they are connected to each other. Can water flow between these two fields (including if it flows via intermediate fields)? Also, how many fields can water flow to from any particular field?
You need to be ready to answer his queries swiftly, or be hit with heavy fines.
Input
The first line contains two integers and
, the number of fields and the number of discrete events there are, respectively. Each event is either some pipes toggling, the inspector asking you if two fields are connected to each other, or the inspector asking about the size (the number of fields) connected via open pipes to some field. The fields are labelled
through
.
The next lines each contain an event. These lines will take the form
TOGGLE u v
(),
ASK u v
(), or
SIZE u
().
TOGGLE u v
will flip the state of the pipe directly connecting fieldsand
. If it was open before, close it. If it was closed before, open it. All pipes are closed initially, and they are all bidirectional, so water can flow both ways.
ASK u v
asks whether fieldsand
are connected via open irrigation pipes. Answer
YES
orNO
.SIZE u
asks what is the number of fields connected to field(and including field
) via a network of open pipes. Answer as a single integer.
Output
For each ASK
query, output YES
or NO
if the two fields are connected or not. For each SIZE
query, output a single integer. These outputs should be given in the same order the queries are given in the input.
Example
Input 1
4 6
TOGGLE 1 2
TOGGLE 2 3
ASK 1 3
TOGGLE 2 3
ASK 1 3
SIZE 1
Output 1
YES
NO
2
We can proceed event by event. Firstly, fields and fields
are linked. Then, we are asked if fields
and
are linked, which they are, since water can flow between then via
. Next,
becomes disconnected. Now, there is no way for water to flow from fields
to
, so we output
NO
. Finally, we are asked for the number of fields connected to field , which is currently two (fields
and
), since field
was disconnected.
Input 2
5 9
ASK 1 2
TOGGLE 1 2
ASK 1 2
TOGGLE 2 3
SIZE 2
TOGGLE 1 2
ASK 1 3
TOGGLE 1 3
ASK 1 3
Output 2
NO
YES
3
NO
YES
- We will proceed event-by-event through the input. Firstly, all pipes are closed, so the first
ASK
returnsNO
. - However, the two fields are now connected since the next event is a toggle, so the following
ASK
returnsYES
. - Next, the pipe between fields
and
is opened.
- Fields
are all connected to field
via open pipes, so we return
.
- Next, fields
and
are disconnected, as they were previously connected.
- Previously, we could get from
to
by traversing
, but now
is disconnected so we cannot.
- Now, fields
and
are directly connected, so water can travel between them.
Comments