def deal_with(lk,st,sp,er):#这里的er是单个故障
time=er[-1]
e=er[0]
stopped_process=tuple(tp[0]for tp in sp if tp[-1]<time)
running_process=tuple(tp[0:3]for tp in st if tp[-1]not in stopped_process and tp[-1]<time)
l_errupt=set()
for process in running_process:
x,y=min(process[:2]),max(process[:2])
path_site=set()
while x>1:
path_site.add(x)
x=min(lk[x])
path_site.add(x)
while y not in path_site:
path_site.add(y)
y=min(lk[y])
path_site.add(y)
for i in path_site.copy():
if i<y:path_site.remove(i)
if e in path_site:l_errupt.add(process)
l_return=tuple(i[-1]for i in running_process if i not in l_errupt)
return max(l_return)if len(l_return)!=0 else -1
if __name__=='__main__':
def get_input():
s=input().strip()
if ' 'not in s:return int(s)
return tuple(int(i)for i in s.split())
n,m=get_input()
link=tuple(get_input()for _ in range(n-1))
events=tuple(get_input()+(i+1,)for i in range(m))#每个元组最后数字表示时刻
start,stop,error=tuple(tuple(tp[1:]for tp in events if tp[0]==i)for i in range(3))
linked={i:tuple(device[device.index(i)-1]for device in link if i in device)for i in range(1,n+1)}
for er_ in error:print(deal_with(linked,start,stop,er_))
此代码运行示例仅用时0.0013467s,不知道为何会超出两秒?不能下载输入/输出,难道测试点有几万行?