結果

問題 No.812 Change of Class
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-11-12 21:40:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,259 bytes
コンパイル時間 871 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 33,636 KB
最終ジャッジ日時 2023-09-03 15:58:36
合計ジャッジ時間 78,890 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,598 ms
30,024 KB
testcase_01 AC 243 ms
11,748 KB
testcase_02 AC 838 ms
20,200 KB
testcase_03 AC 1,850 ms
33,636 KB
testcase_04 AC 1,627 ms
30,556 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 93 ms
13,972 KB
testcase_08 AC 47 ms
10,452 KB
testcase_09 AC 3,576 ms
31,944 KB
testcase_10 AC 3,625 ms
31,916 KB
testcase_11 AC 289 ms
23,336 KB
testcase_12 AC 2,007 ms
28,924 KB
testcase_13 AC 20 ms
8,672 KB
testcase_14 AC 19 ms
8,712 KB
testcase_15 AC 2,467 ms
23,716 KB
testcase_16 AC 101 ms
9,924 KB
testcase_17 AC 2,136 ms
24,920 KB
testcase_18 AC 1,519 ms
20,496 KB
testcase_19 AC 1,858 ms
22,480 KB
testcase_20 AC 3,803 ms
31,460 KB
testcase_21 AC 1,598 ms
20,016 KB
testcase_22 AC 587 ms
14,212 KB
testcase_23 AC 82 ms
9,608 KB
testcase_24 AC 127 ms
10,240 KB
testcase_25 AC 440 ms
13,020 KB
testcase_26 AC 3,120 ms
27,652 KB
testcase_27 AC 2,795 ms
25,352 KB
testcase_28 AC 1,822 ms
22,432 KB
testcase_29 AC 307 ms
11,652 KB
testcase_30 AC 2,429 ms
24,480 KB
testcase_31 AC 1,982 ms
21,892 KB
testcase_32 AC 737 ms
15,516 KB
testcase_33 AC 2,470 ms
26,520 KB
testcase_34 AC 113 ms
10,164 KB
testcase_35 AC 311 ms
12,324 KB
testcase_36 AC 131 ms
10,120 KB
testcase_37 AC 952 ms
15,504 KB
testcase_38 AC 124 ms
15,064 KB
testcase_39 AC 953 ms
15,532 KB
testcase_40 AC 189 ms
10,968 KB
testcase_41 AC 111 ms
14,040 KB
testcase_42 AC 2,185 ms
22,216 KB
testcase_43 AC 366 ms
19,052 KB
testcase_44 AC 1,502 ms
18,268 KB
testcase_45 AC 133 ms
9,968 KB
testcase_46 AC 332 ms
18,896 KB
testcase_47 AC 1,509 ms
18,412 KB
testcase_48 AC 1,535 ms
21,040 KB
testcase_49 AC 366 ms
11,944 KB
testcase_50 AC 35 ms
8,848 KB
testcase_51 AC 370 ms
13,288 KB
testcase_52 AC 789 ms
19,624 KB
testcase_53 AC 229 ms
10,824 KB
testcase_54 AC 199 ms
10,656 KB
testcase_55 AC 1,226 ms
25,288 KB
testcase_56 AC 1,425 ms
27,204 KB
testcase_57 AC 1,520 ms
28,852 KB
testcase_58 AC 800 ms
19,240 KB
testcase_59 AC 1,700 ms
32,132 KB
testcase_60 AC 20 ms
8,824 KB
testcase_61 AC 20 ms
8,688 KB
testcase_62 AC 20 ms
8,832 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