#25218: Python TLE


ericisfunny0429@gmail.com (波波幽靈)

學校 : 不指定學校
編號 : 99594
來源 : [163.20.243.76]
最後登入時間 :
2021-12-10 16:05:39
b967. 4. 血緣關係 -- 2016年3月apcs | From: [163.20.243.76] | 發表日期 : 2021-04-30 18:41

救救我 3,4 一直過不了

 

def DFS(maze,V):
global Visit
global Depth
Visit[V]=1
for i in range(len(maze)):
if maze[V][i]==1 and Visit[i]==0:
Depth[i] = Depth[V]+1
DFS(maze, i)

def Leaf(Maze):
count=0
leaf = []
for i in range(len(Maze)):
for j in Maze[i]:
if j == 1:
count+=1
if count==1:
leaf.append(i)
count=0
return leaf


def main(n, maze):
global Visit
global Depth
leaf = []
ADep = []
Visit = [0]*n
Depth = [1]*n

leaf = Leaf(maze)

for i in leaf:
for a in range(len(maze)):
Visit[a] = 0
for j in range(len(maze)):
Depth[j] = 1
DFS(maze,i)
ADep.append(max(Depth))
 
return max(ADep)-1

result = []
while True:
try:
n = int(input())
maze = [[0]*n for i in range(n)]
for i in range(n-1):
temp = list(map(int, input().split()))
maze[temp[0]][temp[1]] = 1
maze[temp[1]][temp[0]] = 1

result.append(main(n, maze))
except ValueError or EOFError:
break

for i in result:
print(i)
 
#25219: Re:Python TLE


hsugoya@gmail.com (Мигает cf4?)

學校 : 國立臺北科技大學
編號 : 139476
來源 : [218.172.15.43]
最後登入時間 :
2023-09-07 11:23:36
b967. 4. 血緣關係 -- 2016年3月apcs | From: [36.224.203.103] | 發表日期 : 2021-04-30 19:00

Python的效能比較低 不一定能AC 
ZeroJudge Forum