Advertisement
GerONSo

F open

Dec 23rd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /*
  2. ┓┏┓┏┓┃
  3. ┛┗┛┗┛┃
  4. ┓┏┓┏┓┃
  5. ┛┗┛┗┛┃
  6. ┓┏┓┏┓┃\○/
  7. ┛┗┛┗┛┃ / /
  8. ┓┏┓┏┓┃ノ
  9. ┛┗┛┗┛┃
  10. ┓┏┓┏┓┃
  11. ┛┗┛┗┛┃
  12. ┓┏┓┏┓┃
  13. ┛┗┛┗┛┃
  14. ┓┏┓┏┓┃
  15. ┛┗┛┗┛┃
  16. ┓┏┓┏┓┃┓
  17. ┛┗┛┗┛┃┃
  18. MIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSIC
  19. */
  20.  
  21. // #define pragma
  22.  
  23. #ifdef pragma
  24. #pragma GCC optimize("Ofast")
  25. #pragma GCC optimize("no-stack-protector")
  26. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27. #pragma GCC optimize("unroll-loops")
  28. #pragma GCC diagnostic ignored "-Wunused-result"
  29. #endif // pragma
  30.  
  31. #include<bits/stdc++.h>
  32. #include <ext/pb_ds/assoc_container.hpp>
  33. #include <ext/pb_ds/tree_policy.hpp>
  34.  
  35. #define ll long long
  36. #define all(x) begin(x), end(x)
  37. #define pb push_back
  38. #define x first
  39. #define y second
  40. #define int long long
  41. #define zero(two) memset(two, 0, sizeof(two))
  42.  
  43. using namespace std;
  44. using namespace __gnu_pbds;
  45.  
  46.  
  47. typedef vector<int> vi;
  48. typedef vector<bool> vb;
  49. typedef pair<int, int> pii;
  50. typedef long double ld;
  51. typedef vector<vi> matrix;
  52. template<typename T>
  53. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  54.  
  55. const ld PI = atan2(0, -1);
  56.  
  57. void seriy() {
  58. ios::sync_with_stdio(0);
  59. cin.tie(0);
  60. cout.tie(0);
  61. cout << fixed << setprecision(10);
  62. #if 1
  63. freopen("input", "r", stdin);
  64. freopen("output", "w", stdout);
  65. #endif
  66. }
  67.  
  68. vb used;
  69. vi a, p;
  70. matrix m, h;
  71.  
  72. void dfs(int u, int cnt, bool f) {
  73. if(f) m[cnt].pb(u);
  74. else h[cnt].pb(a[u]);
  75. used[u] = 1;
  76. if(!used[p[u]]) dfs(p[u], cnt, f);
  77. }
  78.  
  79.  
  80. signed main() {
  81. seriy();
  82. int n;
  83. cin >> n;
  84. used.resize(n);
  85. a.resize(n);
  86. p.resize(n);
  87. for(int i = 0; i < n; i++) {
  88. cin >> p[i];
  89. p[i]--;
  90. }
  91. for(int i = 0; i < n; i++) {
  92. cin >> a[i];
  93. a[i]--;
  94. }
  95. vi cur(n);
  96. for(int i = 0; i < n; i++) {
  97. cur[i] = i;
  98. }
  99. int cnt = 0;
  100. for(int i = 0; i < n; i++) {
  101. if(!used[i]) {
  102. m.push_back({});
  103. dfs(i, cnt, 1);
  104. cnt++;
  105. }
  106. }
  107. int cnt1 = 0;
  108. used.assign(n, 0);
  109. for(int i = 0; i < n; i++) {
  110. if(!used[i]) {
  111. h.push_back({});
  112. dfs(i, cnt1, 0);
  113. cnt1++;
  114. }
  115. }
  116. for(int i = 0; i < (int)m.size(); i++) {
  117. int pos;
  118. for(int j = 0; j < (int)h[i].size(); j++) {
  119. if(h[i][j] == m[i][0]) {
  120. pos = j;
  121. }
  122. }
  123. bool fl = 0;
  124. for(int j = 0; j < (int)m[i].size(); j++) {
  125. if(m[i][j] != h[i][(j + pos) % h[i].size()]) {
  126. fl = 1;
  127. }
  128. }
  129. if(fl) {
  130. return cout << "No", 0;
  131. }
  132. }
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement