human
I found an interesting problem on https://www.codechef.com/problems/FROGV:
Nobody knows, but $N$ frogs live in Chef's garden.
Now they are siting on the X-axis and want to speak to each other. One frog can send a message to another one if the distance between them is less or equal to $K$.
Chef knows all $P$ pairs of frogs, which want to send messages. Help him to define can they or not!
Note : More than $1$ frog can be on the same point on the X-axis.

-----Input-----
- The first line contains three integers $N$, $K$ and $P$.
- The second line contains $N$ space-separated integers $A_1$, $A_2$, …, $A_N$ denoting the x-coordinates of frogs".
- Each of the next $P$ lines contains two integers $A$ and $B$ denoting the numbers of frogs according to the input.

-----Output-----
For each pair print "Yes" without a brackets if frogs can speak and "No" if they cannot.

-----Constraints-----
- $1 \le N, P \le 10^5$
- $0 \le A_i, K \le 10^9$
- $1 \le A, B \le N$

-----Example-----

-----Sample Input:-----
5 3 3
0 3 8 5 12
1 2
1 3
2 5

-----Sample Output:-----
Yes
Yes
No

-----Explanation-----
- 
For pair $(1, 2)$ frog $1$ can directly speak to the frog $2$ as the distance between them is $3 - 0 = 3 \le K$ . 
- 
For pair $(1, 3)$ frog $1$ can send a message to frog $2$, frog $2$ can send it to frog $4$ and it can send it to frog $3$.
- 
For pair $(2, 5)$ frogs can't send a message under current constraints.

I tried it in Python, but could not do it. Can you solve it?

 What is a Python code to print "Yes" without a brackets if frogs can speak and "No" if they cannot?
assistant
#Here is Python code to print "Yes" without a brackets if frogs can speak and "No" if they cannot.

class union_find:
 def __init__(self,N):
  self.parent = {}
  self.rank = []
  for i in range(N):
   self.parent[i] = i
   self.rank.append(0)

 def find(self,x):
  y = x
  while(x!=self.parent[x]):
   x = self.parent[x]
  self.parent[y] = x
  return x

 def union(self,x,y):
  px = self.find(x)
  py = self.find(y)
  if(px==py):
   return
  else:
   if(self.rank[px]>self.rank[py]):
    self.parent[py] = px
   elif(self.rank[px]<self.rank[py]):
    self.parent[px] = py
   else:
    self.parent[py] = px
    self.rank[px] += 1

N,K,P = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
ans = []

uf = union_find(N)

Y = []
for i in range(N):
 Y.append((X[i],i))
Y.sort()

for i in range(N-1):
 if(Y[i+1][0]-Y[i][0]<=K):
  uf.union(Y[i][1],Y[i+1][1])

A = []
B = []

for i in range(P):
 a,b = [int(i) for i in input().split()]
 A.append(a-1)
 B.append(b-1)

for i in range(P):
 pa = uf.find(A[i])
 pb = uf.find(B[i])
 if(pa==pb):
  ans.append('Yes')
 else:
  ans.append('No')

for i in ans:
 print(i)