結果

問題 No.812 Change of Class
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-12 21:05:41
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 3,461 ms / 4,000 ms
コード長 2,313 bytes
コンパイル時間 1,206 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 248,448 KB
最終ジャッジ日時 2023-09-03 16:06:13
合計ジャッジ時間 70,985 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 460 ms
123,020 KB
testcase_01 AC 206 ms
81,608 KB
testcase_02 AC 318 ms
100,492 KB
testcase_03 AC 541 ms
134,952 KB
testcase_04 AC 494 ms
126,940 KB
testcase_05 AC 3,461 ms
206,080 KB
testcase_06 AC 3,445 ms
210,240 KB
testcase_07 AC 128 ms
90,284 KB
testcase_08 AC 94 ms
77,532 KB
testcase_09 AC 2,940 ms
180,720 KB
testcase_10 AC 2,963 ms
183,356 KB
testcase_11 AC 264 ms
101,876 KB
testcase_12 AC 1,696 ms
138,312 KB
testcase_13 AC 68 ms
70,744 KB
testcase_14 AC 70 ms
70,708 KB
testcase_15 AC 2,241 ms
151,764 KB
testcase_16 AC 359 ms
83,296 KB
testcase_17 AC 1,944 ms
145,892 KB
testcase_18 AC 1,649 ms
128,316 KB
testcase_19 AC 1,774 ms
135,824 KB
testcase_20 AC 3,048 ms
187,548 KB
testcase_21 AC 1,485 ms
124,908 KB
testcase_22 AC 770 ms
98,628 KB
testcase_23 AC 380 ms
83,020 KB
testcase_24 AC 387 ms
83,496 KB
testcase_25 AC 783 ms
96,028 KB
testcase_26 AC 2,719 ms
170,244 KB
testcase_27 AC 2,245 ms
155,616 KB
testcase_28 AC 1,720 ms
135,056 KB
testcase_29 AC 614 ms
88,852 KB
testcase_30 AC 2,246 ms
152,040 KB
testcase_31 AC 1,772 ms
137,228 KB
testcase_32 AC 879 ms
102,248 KB
testcase_33 AC 2,065 ms
150,364 KB
testcase_34 AC 384 ms
82,384 KB
testcase_35 AC 527 ms
89,960 KB
testcase_36 AC 337 ms
82,272 KB
testcase_37 AC 929 ms
103,220 KB
testcase_38 AC 166 ms
93,088 KB
testcase_39 AC 946 ms
105,664 KB
testcase_40 AC 399 ms
84,436 KB
testcase_41 AC 160 ms
90,576 KB
testcase_42 AC 1,874 ms
129,404 KB
testcase_43 AC 441 ms
96,372 KB
testcase_44 AC 1,250 ms
114,860 KB
testcase_45 AC 346 ms
83,292 KB
testcase_46 AC 438 ms
101,592 KB
testcase_47 AC 1,309 ms
115,488 KB
testcase_48 AC 1,281 ms
118,420 KB
testcase_49 AC 545 ms
90,196 KB
testcase_50 AC 180 ms
78,272 KB
testcase_51 AC 502 ms
91,900 KB
testcase_52 AC 715 ms
104,940 KB
testcase_53 AC 451 ms
86,620 KB
testcase_54 AC 405 ms
84,436 KB
testcase_55 AC 1,234 ms
191,740 KB
testcase_56 AC 1,451 ms
226,936 KB
testcase_57 AC 1,490 ms
226,480 KB
testcase_58 AC 787 ms
148,964 KB
testcase_59 AC 1,689 ms
248,448 KB
testcase_60 AC 68 ms
71,152 KB
testcase_61 AC 70 ms
71,152 KB
testcase_62 AC 67 ms
70,968 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 = 0
    while (1 << day) <= dist:
        day += 1
    ans.append((tree.sizeOfSameRoot(A) - 1, day))

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