結果

問題 No.330 Eigenvalue Decomposition
ユーザー vwxyzvwxyz
提出日時 2021-11-26 03:39:10
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 491 ms / 5,000 ms
コード長 2,071 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 10,868 KB
実行使用メモリ 15,064 KB
最終ジャッジ日時 2023-09-11 05:52:33
合計ジャッジ時間 7,265 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 229 ms
11,292 KB
testcase_01 AC 35 ms
10,536 KB
testcase_02 AC 39 ms
10,456 KB
testcase_03 AC 238 ms
11,200 KB
testcase_04 AC 36 ms
10,456 KB
testcase_05 AC 38 ms
10,444 KB
testcase_06 AC 247 ms
11,084 KB
testcase_07 AC 35 ms
10,492 KB
testcase_08 AC 174 ms
10,940 KB
testcase_09 AC 433 ms
10,428 KB
testcase_10 AC 36 ms
10,468 KB
testcase_11 AC 158 ms
10,504 KB
testcase_12 AC 441 ms
14,520 KB
testcase_13 AC 36 ms
10,568 KB
testcase_14 AC 143 ms
10,940 KB
testcase_15 AC 57 ms
14,948 KB
testcase_16 AC 36 ms
10,348 KB
testcase_17 AC 45 ms
10,756 KB
testcase_18 AC 112 ms
14,704 KB
testcase_19 AC 37 ms
10,392 KB
testcase_20 AC 52 ms
10,796 KB
testcase_21 AC 491 ms
12,016 KB
testcase_22 AC 44 ms
10,440 KB
testcase_23 AC 63 ms
10,716 KB
testcase_24 AC 428 ms
10,516 KB
testcase_25 AC 37 ms
10,448 KB
testcase_26 AC 49 ms
10,484 KB
testcase_27 AC 46 ms
15,064 KB
testcase_28 AC 35 ms
10,348 KB
testcase_29 AC 40 ms
12,740 KB
testcase_30 AC 35 ms
10,420 KB
testcase_31 AC 36 ms
10,552 KB
testcase_32 AC 36 ms
10,404 KB
testcase_33 AC 36 ms
10,464 KB
testcase_34 AC 36 ms
10,568 KB
testcase_35 AC 36 ms
10,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter, deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as Gcd
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
class UnionFind:
    def __init__(self,n):
        self.n=n
        self.parents=[-1]*n

    def Find(self,x):
        stack=[]
        while self.parents[x]>=0:
            stack.append(x)
            x=self.parents[x]
        for y in stack:
            self.parents[y]=x
        return 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):
        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())

N,M=map(int,readline().split())
UF=UnionFind(N)
for _ in range(M):
    A,B,C=map(int,readline().split())
    A-=1;B-=1
    UF.Union(A,B)
ans=UF.Group_Count()
print(ans)
0