So, I was doing this code for a question in this olympics that we have here in Brazil, OBI, I will put the official page link, but it has no translation to english, so I'm going to put the translation made by me:
Ms.Ant is a great worker and everyday she colects a lot of leaves for her colony. But at weekends, when all the other ants are resting, she likes to have fun sliding the tunnels of the anthill.
The anthill of Ms.Ant has a lot of tunnels and halls. Each tunnel conects exactly 2 different halls. Each hall have a height in the anthill. If there's a tunnel connecting a hall I to a hall J and the hall I is at a greater height, then Ms. Ant can slide from hall I to hall J using this tunnel.
Given the map of the tunnels of the anthill, the heighs of the halls and the hall where Ms.Ant must start, write a program to determine the maximum number of halls Ms.Ant can visit (without counting the hall where she started), using tunnels exclusively to slide between the halls.
Input:
The first input line contains 3 integers S, T and P, respectively the number of halls, the number of tunnels and the hall where Ms.Ant wants to start. The halls are numbered from 1 to S. The second line contains S integers A<i>, the height where i hall is in the anthill. Each one of the T next lines contains integers I and J, indicating that there's a tunnel between halls I and J.
Output:
Your program must print a single line, containing only one integer, the maximum number of halls that Ms.Ant can visit (whithout counting the hall where she started), using the tunnels exclusively to slide between the halls of the anthill.
End of the question
So, this is my code:
S, T, P = map(int, input().split())
A = {tunel 1:altura for tunel, altura in enumerate(map(int, input().split()))}
conex = [set(map(int, input().split())) for tunel in range(T)]
A = {altura:tunel for tunel, altura in A.items() if altura <= A[P]}
A = {tunel:altura for altura, tunel in A.items()}
A = sorted(A.items(), reverse = True, key = lambda kv: (kv[1], kv[0]))
res = 0
while 1:
try:
sala_1 = A[0]
sala_2 = A[1]
if {sala_1[0], sala_2[0]} in conex: res = 1
else:
del sala_2
continue
except IndexError: break
del sala_1
print(res)
It doesn't seen to have anything wrong, but for some reason, when I run it, after I give the input, it still waits for input, and the debugger that I'm using also don't show nothing.
Also, there are some variable names that I put in portuguese, but to guarantee that the code won't cause problems (because before the problem happened it was working), I prefered to keep it the same.
This problem started when I put the:
else:
del sala_2
break
in the code.
CodePudding user response:
This solves the problem. I decode all of the values into a single list, then pull off S, T, P and the heights. Note that, for debugging, I'm ignoring P and calling the function for every possible value. You would need to change that.
lines = """\
4 5 2
100 150 -50 200
1 2
2 4
1 4
3 4
1 3"""
allx = [int(k) for k in lines.split()]
S,T,P = allx[:3]
hts = [0] allx[3:3 S]
allx = allx[3 S:]
# Build the links.
links = {}
while allx:
a = allx.pop()
b = allx.pop()
if hts[a] < hts[b]:
a,b = b,a
if a not in links:
links[a] = []
links[a].append(b)
print(links)
def howmany(P):
visited = set()
pending = [P]
while pending:
inx = pending.pop(0)
if inx in visited:
continue
visited.add(inx)
if inx in links and links[inx]:
pending.extend(links[inx])
return len(visited)-1
print(howmany(1))
print(howmany(2))
print(howmany(3))
print(howmany(4))