Crop Network


Submit solution

Points: 1
Time limit: 1.0s
Python 2.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
C++, Java, Python

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 n and q (1 \leq n,q \leq 10^5), 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 1 through n.

The next q lines each contain an event. These lines will take the form TOGGLE u v (1 \leq u,v \leq n, u\neq v), ASK u v (1 \leq u,v \leq n, u\neq v), or SIZE u (1 \leq u \leq n).

  • TOGGLE u v will flip the state of the pipe directly connecting fields u and v. 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 fields u and v are connected via open irrigation pipes. Answer YES or NO.

  • SIZE u asks what is the number of fields connected to field u (and including field u) 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 1 \leftrightarrow 2 and fields 2 \leftrightarrow 3 are linked. Then, we are asked if fields 1 and 3 are linked, which they are, since water can flow between then via 2. Next, 2 \leftrightarrow 3 becomes disconnected. Now, there is no way for water to flow from fields 1 to 3, so we output NO. Finally, we are asked for the number of fields connected to field 1, which is currently two (fields 1 and 2), since field 3 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 returns NO.
  • However, the two fields are now connected since the next event is a toggle, so the following ASK returns YES.
  • Next, the pipe between fields 2 and 3 is opened.
  • Fields 1,2,3 are all connected to field 2 via open pipes, so we return 3.
  • Next, fields 1 and 2 are disconnected, as they were previously connected.
  • Previously, we could get from 1 to 3 by traversing 2, but now 1 \leftrightarrow 2 is disconnected so we cannot.
  • Now, fields 1 and 3 are directly connected, so water can travel between them.

Comments

There are no comments at the moment.