結果

問題 No.330 Eigenvalue Decomposition
ユーザー ayaoniayaoni
提出日時 2021-04-19 03:56:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 261 ms / 5,000 ms
コード長 1,321 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 78,564 KB
最終ジャッジ日時 2024-07-04 04:56:50
合計ジャッジ時間 5,300 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  # 親のノード番号
        self.rank = [0]*(n+1)

    def find(self,x):  # xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self,x,y):  # x,yが同じグループか否か
        return self.find(x) == self.find(y)

    def unite(self,x,y):  # x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            x,y = y,x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        self.par[y] = x


N,M = MI()

UF = UnionFind(N)
for _ in range(M):
    a,b,c = MI()
    UF.unite(a,b)

ans = sum(UF.find(i) == i for i in range(1,N+1))
print(ans)
0