Advertisement
GerONSo

Untitled

Dec 25th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 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 0
  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. int lcm(int x, int y) {
  80. return x * y / __gcd(x, y);
  81. }
  82.  
  83. signed main() {
  84. seriy();
  85. int n;
  86. cin >> n;
  87. used.resize(n);
  88. a.resize(n);
  89. p.resize(n);
  90. for(int i = 0; i < n; i++) {
  91. cin >> p[i];
  92. p[i]--;
  93. }
  94. for(int i = 0; i < n; i++) {
  95. cin >> a[i];
  96. a[i]--;
  97. }
  98. vi cur(n);
  99. for(int i = 0; i < n; i++) {
  100. cur[i] = i;
  101. }
  102. int cnt = 0;
  103. for(int i = 0; i < n; i++) {
  104. if(!used[i]) {
  105. m.push_back({});
  106. dfs(i, cnt, 1);
  107. cnt++;
  108. }
  109. }
  110. int cnt1 = 0;
  111. used.assign(n, 0);
  112. for(int i = 0; i < n; i++) {
  113. if(!used[i]) {
  114. h.push_back({});
  115. dfs(i, cnt1, 0);
  116. cnt1++;
  117. }
  118. }
  119. vi aa, bb;
  120. for(int i = 0; i < (int)m.size(); i++) {
  121. int pos;
  122. for(int j = 0; j < (int)h[i].size(); j++) {
  123. if(h[i][j] == m[i][0]) {
  124. pos = j;
  125. }
  126. }
  127. aa.pb(h[i].size());
  128. bb.pb(pos);
  129. bool fl = 0;
  130. for(int j = 0; j < (int)m[i].size(); j++) {
  131. if(m[i][j] != h[i][(j + pos) % h[i].size()]) {
  132. fl = 1;
  133. }
  134. }
  135. if(fl) {
  136. return cout << "No", 0;
  137. }
  138. }
  139. int lc = 1;
  140. int mx = 0, maxi;
  141. for(int i = 0; i < aa.size(); i++) {
  142. lc = lcm(lc, aa[i]);
  143. if(mx < aa[i]) {
  144. mx = aa[i];
  145. maxi = i;
  146. }
  147. }
  148. for(int i = bb[maxi]; i < lc; i += mx) {
  149. bool ff = 0;
  150. for(int j = 0; j < aa.size(); j++) {
  151. if(i % aa[j] != bb[j]) {
  152. ff = 1;
  153. }
  154. }
  155. if(!ff) {
  156. return cout << "Yes", 0;
  157. }
  158. }
  159. cout << "No";
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement