Problem p02991 (python)

Fixed submission:
import sys
input = sys.stdin.readline
 
def main():
     n,m=map(int,input().split())
     field=[[] for i in range(3*n)]
     for i in range(m):
          x,y=map(int,input().split())
          x,y=x-1,y-1
          field[3*x].append(3*y+1)
          field[3*x+1].append(3*y+2)
          field[3*x+2].append(3*y)
     
     s,t=map(int,input().split())
     s,t=s-1,t-1
     s,t=3*s,3*t
     
     d=[float('inf') for i in range(3*n)]
     d[s]=0
     
     que=[s]
     cnt=0
     while que!=[]:
          que2=[]
          cnt+=1
          for i in que:
               for j in field[i]:
                    if d[j]>cnt:
                         d[j]=cnt
                         que2.append(j)
          que=que2
     if d[t]==float('inf'):
          print(-1)
     else:
          print(d[t]//3)
     return 
 
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import numpy as np
class AdjWeightedDiGraph:
    def __init__(self,g):
        lines = g.split("\n")
        self.size = int(lines[0])*3+3
        self.adjMatrix = np.full((self.size,self.size),float('inf'))
        for i in range(self.size):
            self.adjMatrix[i][i] = 0 # 対角成分は$0$とする。
        for s in lines[1:]:
            d1,d2 = s.split(" ")
            self.connect(3*int(d1),3*int(d2)+1)
            self.connect(3*int(d1)+1,3*int(d2)+2)
            self.connect(3*int(d1)+2,3*int(d2))
    def connect(self,x, y):
        self.adjMatrix[x][y] = 1
    def floyd(self):
        d = self.adjMatrix        # 距離行列dの初期値は隣接行列
        for k in range(self.size):   # 頂点kを経由したら短くいけるか調べる
            newd = np.copy(d)
            for i in range(self.size):
                for j in range(self.size):
                    if d[i][j] > d[i][k]+d[k][j]: # 頂点iから頂点jに行くとき頂点kを経由したら短くいけるなら、
                        newd[i][j] = d[i][k]+d[k][j] # それを新しい値とする。
            d = newd # 新しい距離行列を次の距離行列とする。
        return d

n,m=map(int,input().split())
g=[str(n),"\n"]
for i in range(m):
     g.append(input())
     g.append("\n")
graph = AdjWeightedDiGraph("".join(g[:-1]))

s,t=map(int,input().split())


dm = graph.floyd()[3*s][3*t]
if dm==float('inf'):
     print(-1)
else:
     print(int(dm)//3)

Buggy submission (verdict: Runtime Error):
import numpy as np
class AdjDiGraph:
     def __init__(self,g):
          lines = g.split("\n")
          self.size = int(lines[0])+1 
          self.matrix = np.zeros((self.size, self.size)) # 指定された大きさで要素が0の行列を生成する
          for s in lines[1:]:
               d1,d2 = s.split(" ")
               self.connect(int(d1),int(d2))
          self.adjMatrix=np.dot(np.dot(self.matrix,self.matrix),self.matrix)
     def connect(self, x, y):
          self.connect1(x,y)            
     def connect1(self, x, y):
          self.matrix[x][y] = 1
     def connected(self,x,y):
          return self.matrix[x][y] == 1

     def traceByPower(self,start,end,rememberA=None,kaisu=None):
          if rememberA==None:
               c=self.adjMatrix
               rememberA=[self.adjMatrix]
               for i in range(2,self.size):
                    c=np.dot(c,self.adjMatrix)
                    rememberA.append(c)
        
          if kaisu==None:
               kaisu=0
               for A_i in rememberA:
                    kaisu+=1
                    if kaisu==self.size:
                         return None
                    if A_i[start][end]!=0:
                         return self.traceByPower(start,end,rememberA,kaisu)
          else:
               if kaisu==1:
                    return [start,end]
               else:
                    for i in range(1,kaisu):#startからi回でinterchange(中継地点)、interchangeからendをkaisu-i回で行く
                         for interchange in range(self.size):
                              if rememberA[i-1][start][interchange]!=0 and rememberA[kaisu-i-1][interchange][end]!=0:
                                   return self.traceByPower(start,interchange,rememberA,i)[:-1]+self.traceByPower(interchange,end,rememberA,kaisu-i)


n,m=map(int,input().split())
g=[str(n),"\n"]
for i in range(m):
     g.append(input())
     g.append("\n")
graph = AdjDiGraph("".join(g[:-1]))
s,t=map(int,input().split())
ans=graph.traceByPower(s,t)
if ans==None:
     print(-1)
else:
     print(len(ans)-1)

Buggy submission (verdict: Runtime Error):
import numpy as np
class AdjDiGraph:
     def __init__(self,g):
          lines = g.split("\n")
          self.size = int(lines[0])+1 
          self.matrix = np.zeros((self.size, self.size)) # 指定された大きさで要素が0の行列を生成する
          for s in lines[1:]:
               d1,d2 = s.split(" ")
               self.connect(int(d1),int(d2))
          self.adjMatrix=np.dot(np.dot(self.matrix,self.matrix),self.matrix)
     def connect(self, x, y):
          self.connect1(x,y)            
     def connect1(self, x, y):
          self.matrix[x][y] = 1
     def connected(self,x,y):
          return self.matrix[x][y] == 1

     def traceByPower(self,start,end,rememberA=None,kaisu=None):
          if rememberA==None:
               c=self.adjMatrix
               rememberA=[self.adjMatrix]
               for i in range(2,self.size):
                    c=np.dot(c,self.adjMatrix)
                    rememberA.append(c)
        
          if kaisu==None:
               kaisu=0
               for A_i in rememberA:
                    kaisu+=1
                    if A_i[start][end]!=0:
                         return self.traceByPower(start,end,rememberA,kaisu)
          else:
               if kaisu==1:
                    return [start,end]
               else:
                    for i in range(1,kaisu):#startからi回でinterchange(中継地点)、interchangeからendをkaisu-i回で行く
                         for interchange in range(self.size):
                              if rememberA[i-1][start][interchange]!=0 and rememberA[kaisu-i-1][interchange][end]!=0:
                                   return self.traceByPower(start,interchange,rememberA,i)[:-1]+self.traceByPower(interchange,end,rememberA,kaisu-i)


n,m=map(int,input().split())
g=[str(n),"\n"]
for i in range(m):
     g.append(input())
     g.append("\n")
graph = AdjDiGraph("".join(g[:-1]))
s,t=map(int,input().split())
ans=graph.traceByPower(s,t)
if ans==None:
     print(-1)
else:
     print(len(ans)-1)

Buggy submission (verdict: Runtime Error):
import numpy as np
class AdjDiGraph:
     def __init__(self,g):
          lines = g.split("\n")
          self.size = int(lines[0])+1 
          self.adjMatrix = np.zeros((self.size, self.size)) # 指定された大きさで要素が0の行列を生成する
          for s in lines[1:]:
               d1,d2 = s.split(" ")
               self.connect(int(d1),int(d2))
          self.adjMatrix=np.dot(np.dot(self.adjMatrix,self.adjMatrix),self.adjMatrix)
     def connect(self, x, y):
          self.connect1(x,y)            
     def connect1(self, x, y):
          self.adjMatrix[x][y] = 1
     def connected(self,x,y):
          return self.adjMatrix[x][y] == 1

     def traceByPower(self,start,end,rememberA=None,kaisu=None):
          if rememberA==None:
               c=self.adjMatrix
               rememberA=[self.adjMatrix]
               for i in range(2,self.size):
                    c=np.dot(c,self.adjMatrix)
                    rememberA.append(c)
        
          if kaisu==None:
               kaisu=0
               for A_i in rememberA:
                    kaisu+=1
                    if A_i[start][end]!=0:
                         return self.traceByPower(start,end,rememberA,kaisu)
          else:
               if kaisu==1:
                    return [start,end]
               else:
                    for i in range(1,kaisu):#startからi回でinterchange(中継地点)、interchangeからendをkaisu-i回で行く
                         for interchange in range(self.size):
                              if rememberA[i-1][start][interchange]!=0 and rememberA[kaisu-i-1][interchange][end]!=0:
                                   return self.traceByPower(start,interchange,rememberA,i)[:-1]+self.traceByPower(interchange,end,rememberA,kaisu-i)



n,m=map(int,input().split())
g=[str(n),"\n"]
for i in range(m):
     g.append(input())
     g.append("\n")
graph = AdjDiGraph("".join(g[:-1]))
s,t=map(int,input().split())
ans=graph.traceByPower(s,t)
if ans==None:
     print(-1)
else:
     print(len(ans)-1)