fork download
  1. v = [[] for i in range(0, 100005)]
  2. w = [[] for i in range(0, 100005)]
  3.  
  4.  
  5. def dfs(a, b):
  6. if len(v[a]) == 0 and len(w[b]) == 0:
  7. return True
  8. elif len(v[a]) != len(w[b]):
  9. return False
  10. elif len(v[a]) == 1:
  11. return dfs(v[a][0], w[b][0])
  12. else:
  13. return (dfs(v[a][0], w[b][0]) and dfs(v[a][1], w[b][1])) or (dfs(v[a][0], w[b][1]) and dfs(v[a][1], w[b][0]))
  14.  
  15.  
  16. t = int(input())
  17. for z in range(0, t):
  18. n = int(input())
  19. for i in range(0, 100005):
  20. v[i].clear()
  21. w[i].clear()
  22. A = list(map(int, input().split()))
  23. for i in range(0, n):
  24. v[A[i]].append(i + 1)
  25. B = list(map(int, input().split()))
  26. for j in range(0, n):
  27. w[B[j]].append(j + 1)
  28. print(int(dfs(0, 0)))
  29.  
Success #stdin #stdout 0.2s 23712KB
stdin
3
4
3 0 2 3
3 4 0 3
5
5 1 0 3 4
5 1 4 2 0
5
0 1 2 1 4
0 1 4 1 2
stdout
0
1
1