結果

問題 No.1390 Get together
ユーザー burita083burita083
提出日時 2021-03-14 19:43:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,137 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 75,264 KB
最終ジャッジ日時 2024-04-24 02:09:08
合計ジャッジ時間 23,658 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 33 ms
10,880 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 30 ms
10,880 KB
testcase_15 AC 31 ms
10,880 KB
testcase_16 AC 1,179 ms
72,960 KB
testcase_17 AC 1,143 ms
70,656 KB
testcase_18 AC 1,120 ms
75,264 KB
testcase_19 AC 1,345 ms
72,704 KB
testcase_20 AC 1,302 ms
71,168 KB
testcase_21 AC 1,315 ms
71,040 KB
testcase_22 AC 1,264 ms
73,344 KB
testcase_23 AC 1,275 ms
73,344 KB
testcase_24 AC 1,262 ms
73,344 KB
testcase_25 AC 1,302 ms
72,960 KB
testcase_26 AC 1,324 ms
72,960 KB
testcase_27 AC 1,334 ms
72,960 KB
testcase_28 AC 1,321 ms
72,960 KB
testcase_29 AC 1,320 ms
72,960 KB
testcase_30 AC 1,303 ms
72,960 KB
testcase_31 AC 1,304 ms
73,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
AB = [tuple(map(int,input().split())) for i in range(N)]
graph = [[] for _ in range(N)]
class UnionFind:
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self._size = [1] * N
        self.count = 0

    def root(self,a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]
    def is_same(self,a,b):
        return self.root(a) == self.root(b)

    def unite(self,a,b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb: return
        if self._size[ra] < self._size[rb]: ra,rb = rb,ra
        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        self.count += 1

    def size(self,a):
        return self._size[self.root(a)]

uf = UnionFind(N)
for a,b in AB:
  a,b = a-1,b-1
  graph[a].append(b)

for g in graph:
  if len(g) >= 2:
    for i in range(len(g)-1):
      uf.unite(g[i], g[i+1])

dp = [-1] * N
for g in graph:
  for gg in g:
    dp[uf.root(gg)] += 1
    break
ans = 0
for e in dp:
  if e >= 1:
    ans += e
  
print(ans)
0