結果

問題 No.2072 Anatomy
ユーザー taiga0629kyopro
提出日時 2022-09-16 21:28:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,357 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 96,664 KB
最終ジャッジ日時 2024-12-21 18:26:15
合計ジャッジ時間 10,107 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 7 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #







class unionfind:
    def __init__(self,uni_num):
        self.uni_num=uni_num
        self.union_root = [-1 for i in range(self.uni_num + 1)]
        self.union_depth = [0] * (self.uni_num + 1)
        self.e_num=[0]*(self.uni_num+1)

    def find(self,x):  # 親は誰?
        if self.union_root[x] < 0:
            return x
        else:
            self.union_root[x] = self.find(self.union_root[x])
            return self.union_root[x]

    def unite(self,x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            self.e_num[x]+=1
            return
        if self.union_depth[x] < self.union_depth[y]:
            x, y = y, x
        if self.union_depth[x] == self.union_depth[y]:
            self.union_depth[x] += 1
        self.union_root[x] += self.union_root[y]
        self.union_root[y] = x
        self.e_num[x]+=self.e_num[y]+1


    def size(self,x):
        return -self.union_root[self.find(x)]

    def same(self,x,y):
        return self.find(x)==self.find(y)
    def edge(self,x):
        return self.e_num[self.find(x)]



n,m=map(int,input().split())
e=[]
for i in range(m):
    u,v=map(int,input().split())
    e.append((u,v))
e.append((0,0))
e.reverse()
ans=1
uf=unionfind(n+3)
for i in range(1,m+1):
    u,v=e[i]
    bu,bv=e[i-1]
    uf.unite(u,v)
    if uf.same(u,bu):ans+=1
print(ans)



0