結果

問題 No.2072 Anatomy
ユーザー ygd.ygd.
提出日時 2022-09-16 22:49:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 669 ms / 2,000 ms
コード長 2,290 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 133,892 KB
最終ジャッジ日時 2023-08-23 16:21:55
合計ジャッジ時間 11,981 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,812 KB
testcase_01 AC 88 ms
71,872 KB
testcase_02 AC 97 ms
76,420 KB
testcase_03 AC 95 ms
76,332 KB
testcase_04 AC 91 ms
72,560 KB
testcase_05 AC 96 ms
76,288 KB
testcase_06 AC 94 ms
75,948 KB
testcase_07 AC 96 ms
76,092 KB
testcase_08 AC 644 ms
116,520 KB
testcase_09 AC 317 ms
116,720 KB
testcase_10 AC 545 ms
111,932 KB
testcase_11 AC 403 ms
119,764 KB
testcase_12 AC 339 ms
104,592 KB
testcase_13 AC 585 ms
122,376 KB
testcase_14 AC 492 ms
102,608 KB
testcase_15 AC 266 ms
107,516 KB
testcase_16 AC 620 ms
124,936 KB
testcase_17 AC 363 ms
124,668 KB
testcase_18 AC 248 ms
104,324 KB
testcase_19 AC 593 ms
122,764 KB
testcase_20 AC 669 ms
125,640 KB
testcase_21 AC 321 ms
133,520 KB
testcase_22 AC 615 ms
125,884 KB
testcase_23 AC 325 ms
133,600 KB
testcase_24 AC 316 ms
133,892 KB
testcase_25 AC 574 ms
122,964 KB
testcase_26 AC 87 ms
71,632 KB
testcase_27 AC 327 ms
133,616 KB
testcase_28 AC 473 ms
124,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
import math
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
from collections import defaultdict 
from collections import deque
#import copy #DeepCopy: hoge = [_[:] for _ in hogehoge]
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

def shift(x):
    return x//2 + pow(2,15) * (x%2)

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]


def main():
    N,M = map(int,input().split())
    G = [[] for _ in range(N)]
    edges = []
    for i in range(M):
        u,v = map(int,input().split())
        u -= 1; v -= 1
        edges.append((u,v))
    
    uf = UnionFind(N)

    dic = defaultdict(int)
    for i in reversed(range(M)):
        u,v = edges[i]
        paru = uf.find(u)
        parv = uf.find(v)
        uf.union(u,v)
        par = uf.find(u)
        dic[par] = max(dic[paru], dic[parv]) + 1
        # print("X",dic[par])


    ans = max(dic.values())
    print(ans)
    

if __name__ == '__main__':
    main()
0