結果

問題 No.812 Change of Class
ユーザー vwxyzvwxyz
提出日時 2024-04-07 20:06:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,364 ms / 4,000 ms
コード長 3,756 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 40,544 KB
最終ジャッジ日時 2024-04-07 20:08:11
合計ジャッジ時間 79,243 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,122 ms
35,796 KB
testcase_01 AC 334 ms
14,080 KB
testcase_02 AC 1,172 ms
24,272 KB
testcase_03 AC 2,425 ms
40,544 KB
testcase_04 AC 2,131 ms
36,880 KB
testcase_05 AC 3,364 ms
34,220 KB
testcase_06 AC 2,728 ms
31,596 KB
testcase_07 AC 153 ms
13,796 KB
testcase_08 AC 73 ms
11,520 KB
testcase_09 AC 3,144 ms
35,924 KB
testcase_10 AC 3,175 ms
35,900 KB
testcase_11 AC 412 ms
22,348 KB
testcase_12 AC 2,024 ms
32,636 KB
testcase_13 AC 30 ms
10,368 KB
testcase_14 AC 31 ms
10,368 KB
testcase_15 AC 2,147 ms
26,820 KB
testcase_16 AC 140 ms
11,648 KB
testcase_17 AC 1,905 ms
28,124 KB
testcase_18 AC 1,519 ms
23,332 KB
testcase_19 AC 1,588 ms
25,396 KB
testcase_20 AC 3,210 ms
35,320 KB
testcase_21 AC 1,313 ms
22,752 KB
testcase_22 AC 552 ms
16,128 KB
testcase_23 AC 112 ms
11,392 KB
testcase_24 AC 161 ms
11,904 KB
testcase_25 AC 443 ms
14,848 KB
testcase_26 AC 2,647 ms
31,252 KB
testcase_27 AC 2,358 ms
28,584 KB
testcase_28 AC 1,547 ms
25,304 KB
testcase_29 AC 354 ms
13,440 KB
testcase_30 AC 2,194 ms
27,716 KB
testcase_31 AC 1,821 ms
24,752 KB
testcase_32 AC 740 ms
17,688 KB
testcase_33 AC 2,222 ms
29,908 KB
testcase_34 AC 149 ms
11,776 KB
testcase_35 AC 409 ms
14,592 KB
testcase_36 AC 191 ms
11,776 KB
testcase_37 AC 1,105 ms
18,724 KB
testcase_38 AC 181 ms
15,204 KB
testcase_39 AC 1,139 ms
18,716 KB
testcase_40 AC 261 ms
12,928 KB
testcase_41 AC 165 ms
14,420 KB
testcase_42 AC 2,388 ms
26,796 KB
testcase_43 AC 423 ms
21,676 KB
testcase_44 AC 1,639 ms
22,136 KB
testcase_45 AC 186 ms
11,776 KB
testcase_46 AC 389 ms
21,248 KB
testcase_47 AC 1,613 ms
22,208 KB
testcase_48 AC 1,702 ms
24,908 KB
testcase_49 AC 477 ms
14,208 KB
testcase_50 AC 53 ms
10,496 KB
testcase_51 AC 428 ms
15,744 KB
testcase_52 AC 853 ms
22,708 KB
testcase_53 AC 314 ms
12,800 KB
testcase_54 AC 268 ms
12,544 KB
testcase_55 AC 1,604 ms
25,832 KB
testcase_56 AC 1,880 ms
28,744 KB
testcase_57 AC 2,003 ms
29,816 KB
testcase_58 AC 1,064 ms
20,452 KB
testcase_59 AC 2,305 ms
32,592 KB
testcase_60 AC 30 ms
10,368 KB
testcase_61 AC 31 ms
10,368 KB
testcase_62 AC 31 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Graph:
    def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if graph!=None:
            self.graph=graph
            """
            self.edges=[]
            for i in range(self.V):
                if self.weighted:
                    for j,d in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j,d))
                else:
                    for j in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j))
            """
        else:
            self.edges=edges
            self.graph=[[] for i in range(self.V)]
            if weighted:
                for i,j,d in self.edges:
                    self.graph[i].append((j,d))
                    if not self.directed:
                        self.graph[j].append((i,d))
            else:
                for i,j in self.edges:
                    self.graph[i].append(j)
                    if not self.directed:
                        self.graph[j].append(i)

    def SIV_BFS(self,s,bfs_tour=False,bipartite_graph=False,linked_components=False,parents=False,unweighted_dist=False,weighted_dist=False):
        seen=[False]*self.V
        seen[s]=True
        if bfs_tour:
            bt=[s]
        if linked_components:
            lc=[s]
        if parents:
            ps=[None]*self.V
        if unweighted_dist or bipartite_graph:
            uwd=[self.inf]*self.V
            uwd[s]=0
        if weighted_dist:
            wd=[self.inf]*self.V
            wd[s]=0
        queue=deque([s])
        while queue:
            x=queue.popleft()
            for y in self.graph[x]:
                if self.weighted:
                    y,d=y
                if not seen[y]:
                    seen[y]=True
                    queue.append(y)
                    if bfs_tour:
                        bt.append(y)
                    if linked_components:
                        lc.append(y)
                    if parents:
                        ps[y]=x
                    if unweighted_dist or bipartite_graph:
                        uwd[y]=uwd[x]+1
                    if weighted_dist:
                        wd[y]=wd[x]+d
        if bipartite_graph:
            bg=[[],[]]
            for tpl in self.edges:
                i,j=tpl[:2] if self.weighted else tpl
                if uwd[i]==self.inf or uwd[j]==self.inf:
                    continue
                if not uwd[i]%2^uwd[j]%2:
                    bg=False
                    break
            else:
                for x in range(self.V):
                    if uwd[x]==self.inf:
                        continue
                    bg[uwd[x]%2].append(x)
        retu=()
        if bfs_tour:
            retu+=(bt,)
        if bipartite_graph:
            retu+=(bg,)
        if linked_components:
            retu+=(lc,)
        if parents:
            retu+=(ps,)
        if unweighted_dist:
            retu+=(uwd,)
        if weighted_dist:
            retu+=(wd,)
        if len(retu)==1:
            retu=retu[0]
        return retu

N,M=map(int,input().split())
edges=[]
for m in range(M):
    p,q=map(int,input().split())
    p-=1;q-=1
    edges.append((p,q))
inf=1<<30
G=Graph(N,edges=edges,inf=inf)
Q=int(input())
for q in range(Q):
    A=int(input())-1
    dist=G.SIV_BFS(A,unweighted_dist=True)
    cnt,ma=-1,1
    for d in dist:
        if d==inf:
            continue
        cnt+=1
        ma=max(ma,d)
    ans=(ma-1).bit_length()
    print(cnt,ans)
0