Advertisement
GerONSo

F open 50

Dec 25th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 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. const int MAXN = 1e6 + 10;
  69.  
  70. vb used;
  71. vi a, p;
  72. matrix m, h;
  73. vector<set<int>> inda, indb;
  74. bool end_fl = 0;
  75. int start, ccnt = 0;
  76.  
  77. void dfs(int u, int cnt, bool f, int i) {
  78. if(f) {
  79. inda[cnt].insert(u);
  80. m[cnt].pb(u);
  81. }
  82. else {
  83. indb[cnt].insert(u);
  84. // cerr << u << " ";
  85. if(i > m[cnt].size() || a[u] != m[cnt][i]) {
  86. end_fl = 1;
  87. }
  88. if(a[u] == start) {
  89. ccnt = i;
  90. }
  91. h[cnt].pb(a[u]);
  92. }
  93. used[u] = 1;
  94. if(!used[p[u]]) dfs(p[u], cnt, f, i + 1);
  95. }
  96.  
  97. signed main() {
  98. seriy();
  99. vi pr(MAXN, 1);
  100. for(int i = 2; i < MAXN; i++) {
  101. if(pr[i]) {
  102. for(int j = i * i; j < MAXN; j += i) {
  103. pr[j] = 0;
  104. }
  105. }
  106. }
  107. int n;
  108. cin >> n;
  109. used.resize(n);
  110. a.resize(n);
  111. p.resize(n);
  112. vi from(MAXN);
  113. for(int i = 0; i < n; i++) {
  114. cin >> p[i];
  115. p[i]--;
  116. }
  117. for(int i = 0; i < n; i++) {
  118. cin >> a[i];
  119. a[i]--;
  120. from[a[i]] = i;
  121. }
  122. vi cur(n);
  123. for(int i = 0; i < n; i++) {
  124. cur[i] = i;
  125. }
  126. int cnt = 0;
  127. for(int i = 0; i < n; i++) {
  128. if(!used[i]) {
  129. set<int> emp;
  130. inda.pb(emp);
  131. m.push_back({});
  132. dfs(i, cnt, 1, 0);
  133. cnt++;
  134. }
  135. }
  136. vi aa, bb;
  137. int cnt1 = 0;
  138. used.assign(n, 0);
  139. for(int i = 0; i < m.size(); i++) {
  140. if(!used[from[m[i][0]]]) {
  141. start = from[m[i][0]];
  142. set<int> emp;
  143. indb.pb(emp);
  144. ccnt = 0;
  145. h.push_back({});
  146. dfs(start, cnt1, 0, 0);
  147. aa.pb(h[i].size());
  148. bb.pb(ccnt);
  149. cnt1++;
  150. // cerr << '\n';
  151. }
  152. }
  153. if(end_fl || inda.size() != indb.size()) {
  154. return cout << "No", 0;
  155. }
  156. for(int i = 0; i < inda.size(); i++) {
  157. if(inda[i] != indb[i]) {
  158. return cout << "No", 0;
  159. }
  160. }
  161. for(int i = 0; i < aa.size(); i++) {
  162. for(int j = 0; j < i; j++) {
  163. if(abs(bb[j] - bb[i]) % __gcd(aa[i], aa[j])) {
  164. return cout << "No", 0;
  165. }
  166. }
  167. }
  168. cout << "Yes";
  169. return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement