|
| 1 | +import sys |
| 2 | +from collections import Counter |
| 3 | + |
| 4 | +read = lambda: sys.stdin.readline().rstrip() |
| 5 | + |
| 6 | + |
| 7 | +class Problem: |
| 8 | + def __init__(self): |
| 9 | + self.n = int(read()) |
| 10 | + self.lines = [] |
| 11 | + self.root = list(range(self.n)) |
| 12 | + |
| 13 | + for idx in range(self.n): |
| 14 | + x1, y1, x2, y2 = tuple(map(int, read().split())) |
| 15 | + self.lines.append(((x1, y1), (x2, y2))) |
| 16 | + |
| 17 | + def solve(self) -> None: |
| 18 | + for line_x in range(self.n - 1): |
| 19 | + for line_y in range(line_x + 1, self.n): |
| 20 | + (a, b), (c, d) = self.lines[line_x], self.lines[line_y] |
| 21 | + if self.is_cross(a, b, c, d): |
| 22 | + self.union(line_x, line_y) |
| 23 | + |
| 24 | + group = [self.find(num) for num in range(self.n)] |
| 25 | + print(len(set(group))) |
| 26 | + print(max(Counter(group).values())) |
| 27 | + |
| 28 | + def union(self, x: int, y: int): |
| 29 | + root_x, root_y = self.find(x), self.find(y) |
| 30 | + if root_x != root_y: |
| 31 | + self.root[root_y] = root_x |
| 32 | + |
| 33 | + def find(self, num: int) -> int: |
| 34 | + if num != self.root[num]: |
| 35 | + self.root[num] = self.find(self.root[num]) |
| 36 | + return self.root[num] |
| 37 | + |
| 38 | + def is_cross(self, a: tuple[int, int], b: tuple[int, int], c: tuple[int, int], d: tuple[int, int]) -> bool: |
| 39 | + ccw_ab, ccw_cd = self.ccw((a, b, c)) * self.ccw((a, b, d)), self.ccw((c, d, a)) * self.ccw((c, d, b)) |
| 40 | + |
| 41 | + if ccw_ab == 0 and ccw_cd == 0: |
| 42 | + return self.is_overlap(a, b, c, d) |
| 43 | + |
| 44 | + return ccw_ab <= 0 and ccw_cd <= 0 |
| 45 | + |
| 46 | + def ccw(self, points: tuple[tuple[int, int], tuple[int, int], tuple[int, int]]) -> int: |
| 47 | + (x1, y1), (x2, y2), (x3, y3) = points |
| 48 | + result = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) |
| 49 | + return 0 if result == 0 else (result // abs(result)) |
| 50 | + |
| 51 | + def is_overlap(self, a: tuple[int, int], b: tuple[int, int], c: tuple[int, int], d: tuple[int, int]) -> bool: |
| 52 | + return ( |
| 53 | + min(a[0], b[0]) <= max(c[0], d[0]) |
| 54 | + and min(c[0], d[0]) <= max(a[0], b[0]) |
| 55 | + and min(a[1], b[1]) <= max(c[1], d[1]) |
| 56 | + and min(c[1], d[1]) <= max(a[1], b[1]) |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + Problem().solve() |
0 commit comments