結果
| 問題 | 
                            No.1390 Get together
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-05-25 08:35:24 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,038 ms / 2,000 ms | 
| コード長 | 779 bytes | 
| コンパイル時間 | 212 ms | 
| コンパイル使用メモリ | 12,544 KB | 
| 実行使用メモリ | 46,460 KB | 
| 最終ジャッジ日時 | 2024-10-13 16:42:16 | 
| 合計ジャッジ時間 | 17,306 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
N, M = map(int, input().split())
par = [0] * N
rank = [0] * N
def init(n):
    for i in range(n):
        par[i] = i
        rank[i] = 0
        
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1
def same(x, y):
    return find(x) == find(y)
init(N)
D = dict()
E = dict()
for _ in range(N):
    b, c = map(int, input().split())
    E[c-1] = 0
    if (b-1) not in D:
        D[b-1] = c-1
    else:
        unite(D[b-1], c-1)
        
F = set([find(i) for i in par if i in E])
print(len(D) - len(F))