Buggy python submission (verdict: Time Limit Exceeded):

N,M = map(int,input().split())

lis = [[] for i in range(N)]
rlis = [[] for i in range(N)]

for i in range(M):

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

    lis[s].append(t)
    rlis[t].append(s)


dp = [0] * N

for i in range(N):

    i = N - 1 - i

    for j in lis[i]:

        dp[i] += (dp[j] + 1)/len(lis[i])

ans = dp[0]


for k in range(N):

    dp = [0] * N
    flag = True

    for i in range(N):

        i = N - 1 - i

        if i != k:

            for j in lis[i]:

                dp[i] += (dp[j] + 1)/len(lis[i])

        else:

            maxi = 0

            for j in lis[i]:

                maxi = max(maxi,dp[j])
                dp[i] += (dp[j] + 1)

            if len(lis[i]) > 1:
                dp[i] = (dp[i]-(maxi+1)) / (len(lis[i])-1)

            else:
                flag = False
                
    #print (k,dp)

    if flag:
        ans = min(ans,dp[0])

print (ans)


    
    


Fixed submission:

N,M = map(int,input().split())

lis = [[] for i in range(N)]
rlis = [[] for i in range(N)]
mlis = [0] * N

for i in range(M):

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

    lis[s].append(t)
    rlis[t].append(s)


odp = [0] * N

for i in range(N):

    i = N - 1 - i

    for j in lis[i]:

        odp[i] += (odp[j] + 1)/len(lis[i])
        

ans = odp[0]


for k in range(N):

    dp = [0] * N
    flag = True

    for i in range(N):

        i = N - 1 - i

        if i != k:

            if k < i:
                dp[i] = odp[i]

            else:

                for j in lis[i]:

                    dp[i] += (dp[j] + 1)/len(lis[i])

        else:

            maxi = 0

            for j in lis[i]:

                maxi = max(maxi,dp[j])
                dp[i] += (dp[j] + 1)

            if len(lis[i]) > 1:
                dp[i] = (dp[i]-(maxi+1)) / (len(lis[i])-1)

            else:
                flag = False
                
    #print (k,dp)

    if flag:
        ans = min(ans,dp[0])

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

N,M = map(int,input().split())

lis = [[] for i in range(N)]
rlis = [[] for i in range(N)]

for i in range(M):

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

    lis[s].append(t)
    rlis[t].append(s)


dp = [0] * N

for i in range(N):

    i = N - 1 - i

    for j in lis[i]:

        dp[i] += (dp[j] + 1)/len(lis[i])

ans = dp[0]


for k in range(N):

    dp = [0] * N
    flag = True

    for i in range(N):

        i = N - 1 - i

        if i != k:

            for j in lis[i]:

                dp[i] += (dp[j] + 1)/len(lis[i])

        else:

            maxi = 0

            for j in lis[i]:

                maxi = max(maxi,dp[j])
                dp[i] += (dp[j] + 1)

            if len(lis[i]) > 1:
                dp[i] = (dp[i]-(maxi+1)) / (len(lis[i])-1)

            else:
                flag = False
                
    #print (k,dp)

    if flag:
        ans = min(ans,dp[0])

print (ans)


    
    


Fixed submission:

N,M = map(int,input().split())

lis = [[] for i in range(N)]
rlis = [[] for i in range(N)]
mlis = [0] * N

for i in range(M):

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

    lis[s].append(t)
    rlis[t].append(s)


odp = [0] * N

for i in range(N):

    i = N - 1 - i

    for j in lis[i]:

        odp[i] += (odp[j] + 1)/len(lis[i])
        

ans = odp[0]


for k in range(N):

    dp = [0] * N
    flag = True

    for i in range(N):

        i = N - 1 - i

        if i != k:

            if k < i:
                dp[i] = odp[i]

            else:

                for j in lis[i]:

                    dp[i] += (dp[j] + 1)/len(lis[i])

        else:

            maxi = 0

            for j in lis[i]:

                maxi = max(maxi,dp[j])
                dp[i] += (dp[j] + 1)

            if len(lis[i]) > 1:
                dp[i] = (dp[i]-(maxi+1)) / (len(lis[i])-1)

            else:
                flag = False
                
    #print (k,dp)

    if flag:
        ans = min(ans,dp[0])

print (ans)