fork download
  1. E=enumerate
  2. def f(n,m,c=[]):
  3. yield c,n
  4. for i,(I,a)in E(m[n]):M=eval(str(m));del M[n][i];yield from f(a,M,c+[(a,I)])
  5. M=lambda m,B,p:M(m,dict(m[B])[int(p[0])],p[1:])if p else B
  6. def F(m):
  7. r={}
  8. for i,_ in E(m):
  9. for c,n in f(i,m,[(i,-1)]):
  10. if~c[-1][-1]-c[-1][1]:r[n]=r.get(n,[])+[(i,c)]
  11. for n in r:
  12. if not(V:={i for i,_ in E(m)})-{a for a,_ in r[n]}:
  13. if any(all(M(m,B,''.join(str(K)for _,K in c[1:]))==n for B in V)for _,c in r[n]):return 1
  14.  
  15. import ast
  16. def to_g(V):return[[*E(i)]for i in V]
  17. s="""[[0,1],[0,1]]->true
  18. [[1,1],[0,0]]->false
  19. [[0,0],[1,1]]->false
  20. [[4,1],[0,3],[0,0],[0,1],[4,3]]->true
  21. [[2,1],[3,4],[0,4],[2,1],[0,3]]->true
  22. [[4,4],[0,4],[2,1],[0,3],[0,0]]->false
  23. [[8,5],[0,8],[0,0],[8,2],[2,6],[5,2],[3,8],[7,3],[8,4],[3,0]]->true
  24. [[9,2],[8,4],[2,5],[6,9],[8,9],[9,5],[4,0],[4,2],[0,7],[2,1]]->true
  25. [[5,0],[3,7],[9,2],[9,0],[1,8],[8,4],[6,5],[7,1],[2,4],[3,6]]->true
  26. [[5,1],[4,9],[8,1],[8,6],[2,3],[7,0],[2,3],[5,6],[4,9],[7,0]]->false
  27. [[6,3],[1,1],[7,5],[7,1],[4,5],[6,6],[4,6],[5,1],[3,4],[2,4]]->false
  28. """
  29. for i in filter(None,s.split('\n')):
  30. a,b=i.split('->')
  31. print(F(to_g(ast.literal_eval(a))))
Time limit exceeded #stdin #stdout 5s 22792KB
stdin
Standard input is empty
stdout
Standard output is empty