結果

問題 No.812 Change of Class
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-12 21:05:10
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
TLE  
実行時間 -
コード長 2,313 bytes
コンパイル時間 107 ms
コンパイル使用メモリ 10,808 KB
実行使用メモリ 58,088 KB
最終ジャッジ日時 2023-09-03 15:45:18
合計ジャッジ時間 15,104 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,653 ms
58,088 KB
testcase_01 AC 255 ms
11,640 KB
testcase_02 AC 875 ms
20,304 KB
testcase_03 AC 1,953 ms
33,404 KB
testcase_04 AC 1,730 ms
30,284 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop

class UnionFind :
    def __init__(self, size) :
        self.parent = list(range(size))
        self.height = [0] * size
        self.size = [1] * size
        self.component = size

    def root(self, index) :
        if self.parent[index] == index :  # 根の場合
            return index
        rootIndex = self.root(self.parent[index])  # 葉の場合親の根を取得
        self.parent[index] = rootIndex  # 親の付け直し
        return rootIndex

    def union(self, index1, index2) :  # 結合
        root1 = self.root(index1)
        root2 = self.root(index2)

        if root1 == root2 :  # 連結されている場合
            return

        self.component -= 1  # 連結成分を減らす

        if self.height[root1] < self.height[root2] :
            self.parent[root1] = root2  # root2に結合
            self.size[root2] += self.size[root1]
        else :
            self.parent[root2] = root1  # root1に結合
            self.size[root1] += self.size[root2]
            if self.height[root1] == self.height[root2] :
                self.height[root1] += 1
        return

    def isSameRoot(self, index1, index2) :
        return self.root(index1) == self.root(index2)

    def sizeOfSameRoot(self, index) :
        return self.size[self.root(index)]

    def getComponent(self) :
        return self.component

N, M = map(int, input().split())
tree = UnionFind(N)
edges = [[] for _ in range(N)]

for _ in range(M):
    p, q = map(int, input().split())
    p -= 1
    q -= 1
    tree.union(p, q)
    edges[p].append(q)
    edges[q].append(p)

def search(start):
    que = [(0, start)]
    minDist = [float('inf')] * N
    while que:
        d, now = heappop(que)
        if minDist[now] <= d:
            continue
        minDist[now] = d
        for to in edges[now]:
            if minDist[to] > d + 1:
                heappush(que, (d + 1, to))

    D = [d - 1 for d in minDist if 1 <= d and d != float('inf')]
    if D:
        return max(D)
    else:
        return 0

# Qが十分小さい
Q = int(input())
ans = []
for _ in range(Q):
    A = int(input())
    A -= 1
    dist = search(A)
    day = 0
    while (1 << day) <= dist:
        day += 1
    ans.append((tree.sizeOfSameRoot(A) - 1, day))

for a, b in ans:
    print(a, b)
0