結果

問題 No.812 Change of Class
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-12 21:41:12
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,217 ms / 4,000 ms
コード長 2,259 bytes
コンパイル時間 773 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 186,448 KB
最終ジャッジ日時 2023-09-03 16:00:02
合計ジャッジ時間 33,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 423 ms
126,796 KB
testcase_01 AC 188 ms
81,724 KB
testcase_02 AC 303 ms
102,616 KB
testcase_03 AC 498 ms
146,088 KB
testcase_04 AC 443 ms
134,192 KB
testcase_05 AC 1,054 ms
151,920 KB
testcase_06 AC 858 ms
119,120 KB
testcase_07 AC 123 ms
95,856 KB
testcase_08 AC 108 ms
76,788 KB
testcase_09 AC 1,182 ms
148,132 KB
testcase_10 AC 1,217 ms
143,580 KB
testcase_11 AC 221 ms
132,644 KB
testcase_12 AC 780 ms
123,860 KB
testcase_13 AC 93 ms
71,588 KB
testcase_14 AC 92 ms
71,672 KB
testcase_15 AC 812 ms
121,748 KB
testcase_16 AC 220 ms
79,812 KB
testcase_17 AC 833 ms
123,360 KB
testcase_18 AC 648 ms
106,588 KB
testcase_19 AC 705 ms
115,076 KB
testcase_20 AC 1,174 ms
149,764 KB
testcase_21 AC 604 ms
105,832 KB
testcase_22 AC 429 ms
89,796 KB
testcase_23 AC 223 ms
80,332 KB
testcase_24 AC 228 ms
80,356 KB
testcase_25 AC 353 ms
86,436 KB
testcase_26 AC 999 ms
130,960 KB
testcase_27 AC 862 ms
121,876 KB
testcase_28 AC 661 ms
114,680 KB
testcase_29 AC 314 ms
82,924 KB
testcase_30 AC 758 ms
120,384 KB
testcase_31 AC 671 ms
113,056 KB
testcase_32 AC 405 ms
93,320 KB
testcase_33 AC 855 ms
127,580 KB
testcase_34 AC 220 ms
80,300 KB
testcase_35 AC 315 ms
85,780 KB
testcase_36 AC 233 ms
80,628 KB
testcase_37 AC 498 ms
97,688 KB
testcase_38 AC 161 ms
100,224 KB
testcase_39 AC 479 ms
97,564 KB
testcase_40 AC 273 ms
81,508 KB
testcase_41 AC 153 ms
97,892 KB
testcase_42 AC 866 ms
123,820 KB
testcase_43 AC 358 ms
105,236 KB
testcase_44 AC 598 ms
110,064 KB
testcase_45 AC 239 ms
80,992 KB
testcase_46 AC 313 ms
107,244 KB
testcase_47 AC 577 ms
108,528 KB
testcase_48 AC 678 ms
109,880 KB
testcase_49 AC 327 ms
83,788 KB
testcase_50 AC 135 ms
77,164 KB
testcase_51 AC 332 ms
88,052 KB
testcase_52 AC 453 ms
102,532 KB
testcase_53 AC 264 ms
81,604 KB
testcase_54 AC 243 ms
80,628 KB
testcase_55 AC 385 ms
138,604 KB
testcase_56 AC 447 ms
161,840 KB
testcase_57 AC 446 ms
161,080 KB
testcase_58 AC 293 ms
111,040 KB
testcase_59 AC 506 ms
186,448 KB
testcase_60 AC 92 ms
71,116 KB
testcase_61 AC 90 ms
71,760 KB
testcase_62 AC 92 ms
71,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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):
    INF = float('inf')
    minDist = [INF] * N
    que = deque([(start, 0)])

    while que:
        now, dist = que.popleft()
        if minDist[now] <= dist:
            continue
        minDist[now] = dist
        for to in edges[now]:
            que.append((to, dist + 1))

    D = [d - 1 for d in minDist if 1 <= d and d != 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