結果
| 問題 | 
                            No.330 Eigenvalue Decomposition
                             | 
                    
| コンテスト | |
| ユーザー | 
                             lam6er
                         | 
                    
| 提出日時 | 2025-03-20 20:55:17 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 208 ms / 5,000 ms | 
| コード長 | 981 bytes | 
| コンパイル時間 | 214 ms | 
| コンパイル使用メモリ | 82,236 KB | 
| 実行使用メモリ | 141,412 KB | 
| 最終ジャッジ日時 | 2025-03-20 20:56:10 | 
| 合計ジャッジ時間 | 4,238 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 31 | 
ソースコード
import sys
sys.setrecursionlimit(1 << 25)
def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr +=1
    M = int(input[ptr])
    ptr +=1
    
    parent = list(range(N+1))  # Nodes are 1-based
    
    def find(u):
        while parent[u] != u:
            parent[u] = parent[parent[u]]
            u = parent[u]
        return u
    
    def union(u, v):
        u_root = find(u)
        v_root = find(v)
        if u_root == v_root:
            return
        if u_root < v_root:
            parent[v_root] = u_root
        else:
            parent[u_root] = v_root
    
    for _ in range(M):
        a = int(input[ptr])
        ptr +=1
        b = int(input[ptr])
        ptr +=1
        c = int(input[ptr])
        ptr +=1
        union(a, b)
    
    # Count the number of distinct roots
    components = set()
    for i in range(1, N+1):
        components.add(find(i))
    print(len(components))
if __name__ == "__main__":
    main()
            
            
            
        
            
lam6er