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
- }
- vb used;
- vi a, p;
- matrix m, h;
- void dfs(int u, int cnt, bool f) {
- if(f) m[cnt].pb(u);
- else h[cnt].pb(a[u]);
- used[u] = 1;
- if(!used[p[u]]) dfs(p[u], cnt, f);
- }
- signed main() {
- seriy();
- int n;
- cin >> n;
- used.resize(n);
- a.resize(n);
- p.resize(n);
- for(int i = 0; i < n; i++) {
- cin >> p[i];
- p[i]--;
- }
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- a[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]) {
- m.push_back({});
- dfs(i, cnt, 1);
- cnt++;
- }
- }
- int cnt1 = 0;
- used.assign(n, 0);
- for(int i = 0; i < n; i++) {
- if(!used[i]) {
- h.push_back({});
- dfs(i, cnt1, 0);
- cnt1++;
- }
- }
- for(int i = 0; i < (int)m.size(); i++) {
- int pos;
- for(int j = 0; j < (int)h[i].size(); j++) {
- if(h[i][j] == m[i][0]) {
- pos = j;
- }
- }
- bool fl = 0;
- for(int j = 0; j < (int)m[i].size(); j++) {
- if(m[i][j] != h[i][(j + pos) % h[i].size()]) {
- fl = 1;
- }
- }
- if(fl) {
- return cout << "No", 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement