結果

問題 No.812 Change of Class
ユーザー kurimupykurimupy
提出日時 2020-06-15 22:53:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,594 ms / 4,000 ms
コード長 2,255 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 146,008 KB
最終ジャッジ日時 2024-07-03 11:39:15
合計ジャッジ時間 39,507 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 743 ms
134,860 KB
testcase_01 AC 196 ms
80,976 KB
testcase_02 AC 444 ms
107,384 KB
testcase_03 AC 861 ms
146,008 KB
testcase_04 AC 765 ms
137,220 KB
testcase_05 AC 1,594 ms
129,008 KB
testcase_06 AC 1,207 ms
114,524 KB
testcase_07 AC 129 ms
96,028 KB
testcase_08 AC 78 ms
77,152 KB
testcase_09 AC 1,412 ms
133,756 KB
testcase_10 AC 1,442 ms
132,640 KB
testcase_11 AC 312 ms
112,704 KB
testcase_12 AC 954 ms
127,020 KB
testcase_13 AC 41 ms
55,252 KB
testcase_14 AC 41 ms
54,932 KB
testcase_15 AC 991 ms
117,356 KB
testcase_16 AC 184 ms
78,680 KB
testcase_17 AC 936 ms
114,724 KB
testcase_18 AC 756 ms
108,876 KB
testcase_19 AC 837 ms
110,492 KB
testcase_20 AC 1,494 ms
133,680 KB
testcase_21 AC 752 ms
104,956 KB
testcase_22 AC 410 ms
90,808 KB
testcase_23 AC 166 ms
78,704 KB
testcase_24 AC 204 ms
79,628 KB
testcase_25 AC 343 ms
86,420 KB
testcase_26 AC 1,247 ms
127,608 KB
testcase_27 AC 1,183 ms
119,044 KB
testcase_28 AC 778 ms
111,080 KB
testcase_29 AC 285 ms
81,156 KB
testcase_30 AC 1,022 ms
116,284 KB
testcase_31 AC 845 ms
109,308 KB
testcase_32 AC 502 ms
95,032 KB
testcase_33 AC 954 ms
118,576 KB
testcase_34 AC 202 ms
79,636 KB
testcase_35 AC 325 ms
87,324 KB
testcase_36 AC 181 ms
79,124 KB
testcase_37 AC 562 ms
99,296 KB
testcase_38 AC 180 ms
97,736 KB
testcase_39 AC 518 ms
101,272 KB
testcase_40 AC 255 ms
80,596 KB
testcase_41 AC 161 ms
95,616 KB
testcase_42 AC 1,044 ms
117,560 KB
testcase_43 AC 396 ms
102,940 KB
testcase_44 AC 723 ms
107,484 KB
testcase_45 AC 190 ms
78,868 KB
testcase_46 AC 377 ms
104,260 KB
testcase_47 AC 728 ms
107,248 KB
testcase_48 AC 797 ms
112,940 KB
testcase_49 AC 273 ms
81,212 KB
testcase_50 AC 95 ms
77,356 KB
testcase_51 AC 334 ms
90,128 KB
testcase_52 AC 554 ms
105,824 KB
testcase_53 AC 234 ms
79,776 KB
testcase_54 AC 208 ms
79,724 KB
testcase_55 AC 535 ms
121,188 KB
testcase_56 AC 609 ms
129,676 KB
testcase_57 AC 639 ms
132,324 KB
testcase_58 AC 388 ms
106,624 KB
testcase_59 AC 716 ms
145,080 KB
testcase_60 AC 39 ms
55,560 KB
testcase_61 AC 40 ms
55,712 KB
testcase_62 AC 40 ms
55,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#change of class
from collections import deque
import math


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * (n+1)

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n + 1) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, M = map(int, input().split())
graph = [[] for i in range(N+1)]

Uni = UnionFind(N)
for i in range(M):
    p, q = map(int, input().split())
    graph[p].append(q)
    graph[q].append(p)
    Uni.union(p,q)



Q = int(input())
que = []

for _ in range(Q):
    x = int(input())
    que.append(x)


for i in range(Q):
    s_node = que[i]
    visited = [False for i in range(N+1)]
    distance = [float("inf") for i in range(N+1)]
    d = deque()
    e = deque()
    d.append(s_node)
    distance[s_node] = 0
    visited[s_node]=True
    while d:
        while d:
            x = d.popleft()
            for node in graph[x]:
                if visited[node] == False:
                    visited[node] = True
                    distance[node] = distance[x]+1
                    e.append(node)
        if e:
            d = e
            e = deque()
            
    ans = 0
    for node in Uni.members(s_node):
        if node!=s_node:
            ans = max(ans, math.ceil(math.log2(distance[node])))

    print(Uni.size(s_node)-1,ans)


0