結果

問題 No.1920 Territory
ユーザー とりゐとりゐ
提出日時 2022-03-25 10:19:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,269 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 118,060 KB
最終ジャッジ日時 2023-09-11 01:20:30
合計ジャッジ時間 14,589 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,588 KB
testcase_01 AC 95 ms
71,564 KB
testcase_02 AC 92 ms
71,524 KB
testcase_03 AC 678 ms
80,048 KB
testcase_04 AC 747 ms
80,400 KB
testcase_05 AC 739 ms
79,848 KB
testcase_06 AC 787 ms
80,112 KB
testcase_07 AC 770 ms
79,860 KB
testcase_08 AC 392 ms
79,552 KB
testcase_09 AC 400 ms
79,468 KB
testcase_10 AC 405 ms
79,840 KB
testcase_11 AC 400 ms
79,388 KB
testcase_12 AC 403 ms
79,416 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#UnionFind
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        # {0: [0, 2], 1: [1, 3, 4, 5]}
        # 辞書で返す
        # d=uf.all_group_members() で使う
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

def intersect(i,j):
    x1,y1,x2,y2=segs[i]
    p1=(x1,y1)
    p2=(x2,y2)

    x1,y1,x2,y2=segs[j]
    p3=(x1,y1)
    p4=(x2,y2)
    
    tc1 = (p1[0] - p2[0]) * (p3[1] - p1[1]) + (p1[1] - p2[1]) * (p1[0] - p3[0])
    tc2 = (p1[0] - p2[0]) * (p4[1] - p1[1]) + (p1[1] - p2[1]) * (p1[0] - p4[0])
    td1 = (p3[0] - p4[0]) * (p1[1] - p3[1]) + (p3[1] - p4[1]) * (p3[0] - p1[0])
    td2 = (p3[0] - p4[0]) * (p2[1] - p3[1]) + (p3[1] - p4[1]) * (p3[0] - p2[0])
    return tc1*tc2<=0 and td1*td2<=0

n,m=map(int,input().split())
uf=UnionFind(n+m)
segs=[]
for i in range(n):
  y,x1,x2=map(int,input().split())
  segs.append((x1,y,x2,y))

for i in range(m):
  x,y1,y2=map(int,input().split())
  segs.append((x,y1,x,y2))
  
cnt=0
for i in range(n+m):
  for j in range(i+1,n+m):
    if intersect(i,j):
      cnt+=1
      uf.union(i,j)

print(cnt-n-m+uf.group_count())
0