Leaderboard
Showing:
Handle Score
@Ibrahim Nash 5725
@blackshadows 5685
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4908
@saiujwal13083 4156
@sanjay05 3762
@marius_valentin_dragoi 3511
@sushant_a 3453
@KshamaGupta 3318
@the_coder95 3302
Complete Leaderboard
Disjoint set (Union-Find)
Easy Accuracy: 30.63% Submissions: 2041 Points: 2

Given an array that stores all number from 1 to N (both inclusive and sorted).

The task is to do following operations on array elements :
UNION X Z : Do union of x and z. Parent of z will become parent of x.
FIND X: Find the parent of X and print it.
Note: Initially all are the parent of themselves.

Input:
First line consists of T test cases. First line of every test case consists of 2 integers N and K, denoting the size of array and number of queries. Second line consists of K queries, consisting of a string UNION or FIND , for union input two integers as input and to find take one integer as input.

Output:
For every FIND query print its parent.

Constraints:
1<=T<=100
1<=N,K<=100

Example:
Input:

1
5 4
FIND 4 FIND 1 UNION 3 1 FIND 3
Output:
4 1 1

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Disjoint set (Union-Find)

Output Window