結果

問題 No.2072 Anatomy
ユーザー kuro_Bkuro_B
提出日時 2023-03-05 23:35:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 552 ms / 2,000 ms
コード長 2,872 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 117,800 KB
最終ジャッジ日時 2023-10-18 05:01:16
合計ジャッジ時間 13,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
82,684 KB
testcase_01 AC 126 ms
82,684 KB
testcase_02 AC 141 ms
87,952 KB
testcase_03 AC 144 ms
87,968 KB
testcase_04 AC 128 ms
82,688 KB
testcase_05 AC 142 ms
87,964 KB
testcase_06 AC 144 ms
87,948 KB
testcase_07 AC 144 ms
88,056 KB
testcase_08 AC 552 ms
115,036 KB
testcase_09 AC 283 ms
113,624 KB
testcase_10 AC 466 ms
115,152 KB
testcase_11 AC 361 ms
115,256 KB
testcase_12 AC 320 ms
109,020 KB
testcase_13 AC 422 ms
116,008 KB
testcase_14 AC 424 ms
105,508 KB
testcase_15 AC 267 ms
105,304 KB
testcase_16 AC 453 ms
116,272 KB
testcase_17 AC 334 ms
115,352 KB
testcase_18 AC 255 ms
102,776 KB
testcase_19 AC 415 ms
116,868 KB
testcase_20 AC 550 ms
117,800 KB
testcase_21 AC 276 ms
114,876 KB
testcase_22 AC 449 ms
116,920 KB
testcase_23 AC 278 ms
114,852 KB
testcase_24 AC 272 ms
115,504 KB
testcase_25 AC 400 ms
116,364 KB
testcase_26 AC 127 ms
82,688 KB
testcase_27 AC 291 ms
116,188 KB
testcase_28 AC 508 ms
117,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

###スニペット始まり###
import sys, re
from math import ceil, floor, sqrt,factorial, gcd, pi, degrees, radians, sin, asin, cos, acos, tan, atan2
from collections import Counter, deque, defaultdict
from heapq import heapify, heappop, heappush
from itertools import permutations, accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce, lru_cache
from string import ascii_uppercase, ascii_lowercase
from decimal import Decimal, ROUND_HALF_UP #四捨五入用

def input(): return sys.stdin.readline().rstrip('\n')

#easy-testでは再帰数を下げる。
if __file__=='prog.py':
    sys.setrecursionlimit(10**5)
else:
    sys.setrecursionlimit(10**6)

def lcm(a, b): return a * b // gcd(a, b)

#3つ以上の最大公約数/最小公倍数
def gcd_v2(l: list): return reduce(gcd, l)
def lcm_v2(l: list): return reduce(lcm, l)

#nPk
def nPk(n, k): return factorial(n) // factorial(n - k)

#逆元
def modinv(a, mod=10**9+7): return pow(a, mod-2, mod)
INF = float('inf')
MOD = 10 ** 9 + 7
###スニペット終わり###

#0-index
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[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, input().split())

e=[]
for _ in range(M):
    a, b = map(int, input().split())
    e.append([a-1, b-1])

cnt=[0]*N #必要な操作回数
uf=UnionFind(N)

#逆から見ていく。
for a, b in e[::-1]:
    #逆から見る
    if uf.same(a,b):
        cnt[uf.find(a)]+=1 #親に加算
    else:
        pa=uf.find(a)
        pb=uf.find(b)

        uf.union(a,b) #結合
        np=uf.find(a) #新しい親
        cnt[np]=max(cnt[pa], cnt[pb])+1 #更新する
print(max(cnt))
0