結果

問題 No.812 Change of Class
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-12 21:08:52
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 3,949 ms / 4,000 ms
コード長 2,282 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 86,580 KB
実行使用メモリ 248,828 KB
最終ジャッジ日時 2023-09-03 15:51:55
合計ジャッジ時間 82,288 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 522 ms
122,892 KB
testcase_01 AC 229 ms
81,528 KB
testcase_02 AC 389 ms
102,040 KB
testcase_03 AC 610 ms
136,628 KB
testcase_04 AC 559 ms
128,368 KB
testcase_05 AC 3,886 ms
206,348 KB
testcase_06 AC 3,949 ms
209,348 KB
testcase_07 AC 144 ms
90,420 KB
testcase_08 AC 106 ms
77,720 KB
testcase_09 AC 3,506 ms
182,704 KB
testcase_10 AC 3,350 ms
183,216 KB
testcase_11 AC 298 ms
101,776 KB
testcase_12 AC 1,917 ms
138,524 KB
testcase_13 AC 78 ms
71,012 KB
testcase_14 AC 83 ms
70,908 KB
testcase_15 AC 2,565 ms
152,520 KB
testcase_16 AC 400 ms
83,328 KB
testcase_17 AC 2,411 ms
147,528 KB
testcase_18 AC 1,979 ms
129,932 KB
testcase_19 AC 2,204 ms
138,280 KB
testcase_20 AC 3,352 ms
187,540 KB
testcase_21 AC 1,713 ms
124,764 KB
testcase_22 AC 1,040 ms
100,792 KB
testcase_23 AC 488 ms
83,964 KB
testcase_24 AC 465 ms
83,420 KB
testcase_25 AC 910 ms
95,936 KB
testcase_26 AC 3,379 ms
172,916 KB
testcase_27 AC 2,574 ms
155,160 KB
testcase_28 AC 2,062 ms
136,868 KB
testcase_29 AC 790 ms
90,080 KB
testcase_30 AC 2,512 ms
150,376 KB
testcase_31 AC 2,165 ms
135,984 KB
testcase_32 AC 1,032 ms
102,000 KB
testcase_33 AC 2,578 ms
151,724 KB
testcase_34 AC 436 ms
82,820 KB
testcase_35 AC 600 ms
89,936 KB
testcase_36 AC 420 ms
83,336 KB
testcase_37 AC 1,165 ms
104,668 KB
testcase_38 AC 194 ms
93,084 KB
testcase_39 AC 1,253 ms
105,784 KB
testcase_40 AC 446 ms
84,580 KB
testcase_41 AC 177 ms
90,960 KB
testcase_42 AC 2,118 ms
130,504 KB
testcase_43 AC 511 ms
96,512 KB
testcase_44 AC 1,552 ms
115,092 KB
testcase_45 AC 438 ms
84,188 KB
testcase_46 AC 482 ms
101,820 KB
testcase_47 AC 1,547 ms
115,892 KB
testcase_48 AC 1,433 ms
118,376 KB
testcase_49 AC 639 ms
90,400 KB
testcase_50 AC 211 ms
78,308 KB
testcase_51 AC 620 ms
92,352 KB
testcase_52 AC 846 ms
105,116 KB
testcase_53 AC 568 ms
87,180 KB
testcase_54 AC 505 ms
85,584 KB
testcase_55 AC 1,376 ms
192,040 KB
testcase_56 AC 1,594 ms
227,024 KB
testcase_57 AC 1,706 ms
226,672 KB
testcase_58 AC 897 ms
149,092 KB
testcase_59 AC 1,920 ms
248,828 KB
testcase_60 AC 79 ms
70,824 KB
testcase_61 AC 78 ms
71,100 KB
testcase_62 AC 80 ms
70,872 KB
権限があれば一括ダウンロードができます

ソースコード

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 = dist.bit_length()
    ans.append((tree.sizeOfSameRoot(A) - 1, day))

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