fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int t, n, x;
  4. vector<int>v[100005], w[100005];
  5. bool dfs ( int a, int b )
  6. {
  7. if ( v[a].size() == 0 && w[b].size() == 0 )
  8. return true;
  9. else
  10. if ( v[a].size() != w[b].size() )
  11. return false;
  12. else
  13. if ( v[a].size() == 1 )
  14. return dfs ( v[a][0], w[b][0] );
  15. else
  16. return ( ( dfs ( v[a][0], w[b][0] ) && dfs ( v[a][1], w[b][1] ) ) || ( dfs ( v[a][0], w[b][1] ) && dfs ( v[a][1], w[b][0] ) ) );
  17. }
  18. int main()
  19. {
  20. scanf ( "%d", &t );
  21. while ( t-- )
  22. {
  23. scanf ( "%d", &n );
  24. for ( int i = 0; i < 100005; i++ )
  25. {
  26. v[i].clear();
  27. w[i].clear();
  28. }
  29. for ( int i = 1; i <= n; i++ )
  30. {
  31. scanf ( "%d", &x );
  32. v[x].push_back ( i );
  33. }
  34. for ( int i = 1; i <= n; i++ )
  35. {
  36. scanf ( "%d", &x );
  37. w[x].push_back ( i );
  38. }
  39. cout << dfs ( 0, 0 ) << endl;
  40. }
  41. }
  42.  
Success #stdin #stdout 0.01s 7800KB
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