-
Notifications
You must be signed in to change notification settings - Fork 0
/
circles.py
64 lines (49 loc) · 1.7 KB
/
circles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import math
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, u):
if self.parent[u] != u:
self.parent[u] = self.find(self.parent[u])
return self.parent[u]
def union(self, u, v):
root_u = self.find(u)
root_v = self.find(v)
if root_u != root_v:
if self.rank[root_u] > self.rank[root_v]:
self.parent[root_v] = root_u
elif self.rank[root_u] < self.rank[root_v]:
self.parent[root_u] = root_v
else:
self.parent[root_v] = root_u
self.rank[root_u] += 1
def distance(x1, y1, x2, y2):
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
def solve():
t = int(input())
results = []
for _ in range(t):
n = int(input())
circles = []
for _ in range(n):
x, y = map(int, input().split())
circles.append((x, y))
xs, ys, xt, yt = map(int, input().split())
uf = UnionFind(n + 2)
start_index = n
target_index = n + 1
for i in range(n):
dist_start = distance(xs, ys, circles[i][0], circles[i][1])
dist_target = distance(xt, yt, circles[i][0], circles[i][1])
if dist_start <= dist_target:
if dist_start <= dist_target:
uf.union(start_index, i)
if uf.find(start_index) == uf.find(target_index):
results.append("NO")
else:
results.append("YES")
# Output all results at once
print("\n".join(results))
# Entry point
solve()