All problems

Problem 685

Union-Find Connectivity Queries

Medium · 20 pts · Union Find, Graph

Problem

N nodes, numbered from 1 to N, start with no connections. Process Q operations in order. 'union u v' connects nodes u and v (they may already be connected). 'query u v' asks whether u and v are now connected through any chain of unions; print YES or NO. Use a disjoint-set union structure with path compression and union by size or rank, which makes each operation almost O(1).

Input

The first line contains N and Q. Each of the next Q lines contains one operation. There is at least one query.

Output

Print YES or NO for each query, one per line.

Constraints

1 <= N, Q <= 200000; 1 <= u, v <= N

Example input

4 4
union 1 2
union 2 3
query 1 3
query 1 4

Example output

YES
NO

Explanation

After union 1 2 and union 2 3, nodes 1 and 3 are connected through 2, so query 1 3 prints YES. Node 4 was never joined to anything, so query 1 4 prints NO.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Distinct Values in Range Queries Next: Seat Reservation Manager