LearnPy Hub
← Arena
AdvancedRating 1500

Are They Connected?

Start with n separate people. Process q commands: 'U a b' makes a and b friends (friendship spreads: friends of friends are connected), 'Q a b' asks whether a and b are connected — answer YES or NO.

Input

n and q; then q commands.

Output

One line per Q.

Sample 1

Input
5 6
Q 1 2
U 1 2
U 3 4
Q 1 2
U 2 3
Q 1 4
Output
NO
YES
YES

Sample 2

Input
87 31
U 34 45
Q 7 27
Q 26 31
Q 51 80
U 84 11
Q 43 20
U 79 33
Q 33 29
U 42 77
U 21 76
U 51 57
Q 49 13
Q 37 61
Q 77 15
U 53 48
U 75 59
Q 15 82
U 26 33
Q 60 58
U 26 21
Q 72 35
U 79 37
Q 37 6
U 80 79
U 51 74
U 26 50
U 38 26
U 25 3
Q 18 9
U 64 68
Q 1 87
Output
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
solution.py
Output appears here
Are They Connected? · Python Arena problem · LearnPy Hub