Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃\○/
- ┛┗┛┗┛┃ / /
- ┓┏┓┏┓┃ノ
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃┓
- ┛┗┛┗┛┃┃
- MIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSIC
- */
- // #define pragma
- #ifdef pragma
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize("no-stack-protector")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC diagnostic ignored "-Wunused-result"
- #endif // pragma
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ll long long
- #define all(x) begin(x), end(x)
- #define pb push_back
- #define x first
- #define y second
- #define int long long
- #define zero(two) memset(two, 0, sizeof(two))
- using namespace std;
- using namespace __gnu_pbds;
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef pair<int, int> pii;
- typedef long double ld;
- typedef vector<vi> matrix;
- template<typename T>
- using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- const ld PI = atan2(0, -1);
- void seriy() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout << fixed << setprecision(10);
- #if 1
- freopen("input", "r", stdin);
- freopen("output", "w", stdout);
- #endif
- }
- const int MAXN = 1e6 + 10;
- vb used;
- vi a, p;
- matrix m, h;
- vector<set<int>> inda, indb;
- bool end_fl = 0;
- int start, ccnt = 0;
- void dfs(int u, int cnt, bool f, int i) {
- if(f) {
- inda[cnt].insert(u);
- m[cnt].pb(u);
- }
- else {
- indb[cnt].insert(u);
- // cerr << u << " ";
- if(i > m[cnt].size() || a[u] != m[cnt][i]) {
- end_fl = 1;
- }
- if(a[u] == start) {
- ccnt = i;
- }
- h[cnt].pb(a[u]);
- }
- used[u] = 1;
- if(!used[p[u]]) dfs(p[u], cnt, f, i + 1);
- }
- signed main() {
- seriy();
- vi pr(MAXN, 1);
- for(int i = 2; i < MAXN; i++) {
- if(pr[i]) {
- for(int j = i * i; j < MAXN; j += i) {
- pr[j] = 0;
- }
- }
- }
- int n;
- cin >> n;
- used.resize(n);
- a.resize(n);
- p.resize(n);
- vi from(MAXN);
- for(int i = 0; i < n; i++) {
- cin >> p[i];
- p[i]--;
- }
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- a[i]--;
- from[a[i]] = i;
- }
- vi cur(n);
- for(int i = 0; i < n; i++) {
- cur[i] = i;
- }
- int cnt = 0;
- for(int i = 0; i < n; i++) {
- if(!used[i]) {
- set<int> emp;
- inda.pb(emp);
- m.push_back({});
- dfs(i, cnt, 1, 0);
- cnt++;
- }
- }
- vi aa, bb;
- int cnt1 = 0;
- used.assign(n, 0);
- for(int i = 0; i < m.size(); i++) {
- if(!used[from[m[i][0]]]) {
- start = from[m[i][0]];
- set<int> emp;
- indb.pb(emp);
- ccnt = 0;
- h.push_back({});
- dfs(start, cnt1, 0, 0);
- aa.pb(h[i].size());
- bb.pb(ccnt);
- cnt1++;
- // cerr << '\n';
- }
- }
- if(end_fl || inda.size() != indb.size()) {
- return cout << "No", 0;
- }
- for(int i = 0; i < inda.size(); i++) {
- if(inda[i] != indb[i]) {
- return cout << "No", 0;
- }
- }
- for(int i = 0; i < aa.size(); i++) {
- for(int j = 0; j < i; j++) {
- if(abs(bb[j] - bb[i]) % __gcd(aa[i], aa[j])) {
- return cout << "No", 0;
- }
- }
- }
- cout << "Yes";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement