結果

問題 No.1390 Get together
ユーザー burita083burita083
提出日時 2021-03-14 19:48:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,206 ms / 2,000 ms
コード長 1,137 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 75,264 KB
最終ジャッジ日時 2024-04-24 02:11:42
合計ジャッジ時間 20,680 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,880 KB
testcase_01 AC 25 ms
10,880 KB
testcase_02 AC 26 ms
10,880 KB
testcase_03 AC 46 ms
12,288 KB
testcase_04 AC 43 ms
12,032 KB
testcase_05 AC 44 ms
12,032 KB
testcase_06 AC 42 ms
11,904 KB
testcase_07 AC 42 ms
11,904 KB
testcase_08 AC 41 ms
12,032 KB
testcase_09 AC 47 ms
12,288 KB
testcase_10 AC 26 ms
10,752 KB
testcase_11 AC 25 ms
10,880 KB
testcase_12 AC 27 ms
10,880 KB
testcase_13 AC 26 ms
10,880 KB
testcase_14 AC 27 ms
10,880 KB
testcase_15 AC 27 ms
10,752 KB
testcase_16 AC 1,047 ms
72,960 KB
testcase_17 AC 1,003 ms
70,528 KB
testcase_18 AC 1,020 ms
75,264 KB
testcase_19 AC 1,190 ms
72,576 KB
testcase_20 AC 1,149 ms
71,040 KB
testcase_21 AC 1,185 ms
71,040 KB
testcase_22 AC 1,123 ms
73,344 KB
testcase_23 AC 1,062 ms
73,472 KB
testcase_24 AC 1,104 ms
73,344 KB
testcase_25 AC 1,113 ms
72,960 KB
testcase_26 AC 1,107 ms
72,960 KB
testcase_27 AC 1,067 ms
72,960 KB
testcase_28 AC 1,069 ms
72,960 KB
testcase_29 AC 1,110 ms
72,960 KB
testcase_30 AC 1,111 ms
72,960 KB
testcase_31 AC 1,206 ms
72,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
AB = [tuple(map(int,input().split())) for i in range(N)]
graph = [[] for _ in range(M)]
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