I’m new to SPOJ, and is practically using this site’s problems to practice my Python.
I tried LASTSHOT and got TLE every time. I don’t quite understand the reason, my code has a time complexity of n^2,but most of the accepted solutions in c++ have a complexity of n^2. Any optimization?
Any idea, about this?
from collections import defaultdict
def dfs(node, v, c, a):
v[node] = True
c = 1
for i in a[node]:
if not v[i]:
c += dfs(i, v, c, a)
return c
n, m = [int(i) for i in input().split()]
a_l = defaultdict(list)
l = []
for i in range(m):
a1, a2 = [int(i) for i in input().split()]
a_l[a1-1].append(a2-1)
for j in range(n):
visited = [False]*n
c = 0
x = dfs(j, visited, c, a_l)
l.append(x)
print(max(l))
created
last reply
- 1
reply
- 631
views
- 2
users
- 1
link