結果

問題 No.1147 土偶Ⅱ
ユーザー vwxyzvwxyz
提出日時 2023-12-03 12:32:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 149 ms / 500 ms
コード長 5,121 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-09-26 22:07:09
合計ジャッジ時間 2,393 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,136 KB
testcase_01 AC 31 ms
11,136 KB
testcase_02 AC 29 ms
11,008 KB
testcase_03 AC 33 ms
11,136 KB
testcase_04 AC 41 ms
11,136 KB
testcase_05 AC 34 ms
11,136 KB
testcase_06 AC 56 ms
11,136 KB
testcase_07 AC 98 ms
11,136 KB
testcase_08 AC 34 ms
11,136 KB
testcase_09 AC 55 ms
11,264 KB
testcase_10 AC 41 ms
11,136 KB
testcase_11 AC 149 ms
11,008 KB
testcase_12 AC 66 ms
11,136 KB
testcase_13 AC 40 ms
11,136 KB
testcase_14 AC 143 ms
11,136 KB
testcase_15 AC 63 ms
11,264 KB
testcase_16 AC 40 ms
11,008 KB
testcase_17 AC 30 ms
11,136 KB
testcase_18 AC 34 ms
11,008 KB
testcase_19 AC 65 ms
11,264 KB
testcase_20 AC 30 ms
11,008 KB
testcase_21 AC 29 ms
11,136 KB
testcase_22 AC 48 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
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

    def Warshall_Floyd(self,route_restoration=False):
        dist=[[self.inf]*self.V for i in range(self.V)]
        for i in range(self.V):
            dist[i][i]=0
        if route_restoration:
            parents=[[j for j in range(self.V)] for i in range(self.V)]
        for i,j,d in self.edges:
            if i==j:
                continue
            if dist[i][j]>d:
                dist[i][j]=d
                if route_restoration:
                    parents[i][j]=i
            if not self.directed and dist[j][i]>d:
                dist[j][i]=d
                if route_restoration:
                    parents[j][i]=j
        for k in range(self.V):
            for i in range(self.V):
                for j in range(self.V):
                    if dist[i][j]>dist[i][k]+dist[k][j]:
                        dist[i][j]=dist[i][k]+dist[k][j]
                        if route_restoration:
                            parents[i][j]=parents[k][j]
        for i in range(self.V):
            if dist[i][i]<0:
                for j in range(self.V):
                    if dist[i][j]!=self.inf:
                        dist[i][j]=-self.inf
        if route_restoration:
            for i in range(self.V):
                if dist[i][i]==0:
                    parents[i][i]=None
            return dist,parents
        else:
            return dist

N,M=map(int,readline().split())
edges=[]
for m in range(M):
    a,b=map(int,readline().split())
    a-=1;b-=1
    edges.append((a,b,1))
inf=1<<60
G=Graph(N,edges=edges,inf=inf,weighted=True)
dist=G.Warshall_Floyd()
ans=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if not 2 in (dist[i][j],dist[i][k],dist[j][k]):
                ans+=1
print(ans)
0