题目
代码
70分
n,m=map(int,input().split())
#表示距离大赛开幕的天数和训练科目的数量
p=[0]#依赖
p.extend(list(map(int,input().split())))
t=[0]#每个项目需要的天数
t.extend(list(map(int,input().split())))
def judge(start):
for i in start:
if i!=0:
return 1
return 0
def dfs(p,start,status,t,symbol):
if status[p[symbol]]==1:
start[symbol]+=start[p[symbol]]+t[p[symbol]]
#print(start)
return
else:
dfs(p,start,status,t,p[symbol])
status[p[symbol]]=1
def dfs1(p,end,status,t,symbol):
temp=[]
for i in range(m,symbol-1,-1):
if p[i]==symbol and status[i]==0 :
temp.append(end[i])
return min(temp)
#递归
start=[0]*(m+1)
status=[0]*(m+1)
for i in range(1,m+1):
if p[i]==0:#无依赖
start[i]=1
status[i]=1
for i in range(1,m+1):
if p[i]!=0 and status[i]==0:
dfs(p,start,status,t,i)
status[i]=1
for i in start[1:]:
print(i,end=" ")
print()
100分
n,m=map(int,input().split())
#表示距离大赛开幕的天数和训练科目的数量
p=[0]#依赖
p.extend(list(map(int,input().split())))
t=[0]#每个项目需要的天数
t.extend(list(map(int,input().split())))
def judge(start):
for i in start:
if i!=0:
return 1
return 0
def dfs(p,start,status,t,symbol):
if status[p[symbol]]==1:
start[symbol]+=start[p[symbol]]+t[p[symbol]]
#print(start)
return
else:
dfs(p,start,status,t,p[symbol])
status[p[symbol]]=1
def dfs1(p,end,status,t,symbol):
temp=[]
for i in range(m,symbol-1,-1):
if p[i]==symbol and status[i]==0 :
temp.append(end[i])
return min(temp)
#递归
start=[0]*(m+1)
status=[0]*(m+1)
for i in range(1,m+1):
if p[i]==0:#无依赖
start[i]=1
status[i]=1
for i in range(1,m+1):
if p[i]!=0 and status[i]==0:
dfs(p,start,status,t,i)
status[i]=1
for i in start[1:]:
print(i,end=" ")
print()
flag=0
for i in range(1,m+1):
if start[i]+t[i]>n:
#print("不用输出最晚开始天数")
flag=1
break
if flag==0 :#输出最晚开始天数
#print("需要输出最晚开始天数")
end=[0]*(m+1)
for i in range(1,m+1):
if i not in p:
end[i]=n-t[i]+1
status[i]=0
for i in range(m,0,-1):#递减
#print(i)
if status[i] == 1:
end[i]=dfs1(p,end,status,t,i)-t[i]
status[i]=0
for i in end[1:]:
print(i,end=" ")
print()
注意
1.递归注意返回return
2.计算最晚开始时间时要注意end[i]=n-t[i]+1
如果没有以来完成时间为n即可,如果t=1,n=10,那么它最晚开始的时间就是10;
(仔细读题)