結果

問題 No.2346 Replace!!
ユーザー maksimmaksim
提出日時 2023-06-09 22:05:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 2,117 bytes
コンパイル時間 1,735 ms
コンパイル使用メモリ 175,548 KB
実行使用メモリ 4,804 KB
最終ジャッジ日時 2023-08-30 13:19:56
合計ジャッジ時間 7,964 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 5 ms
4,380 KB
testcase_32 AC 4 ms
4,396 KB
testcase_33 AC 4 ms
4,384 KB
testcase_34 AC 5 ms
4,488 KB
testcase_35 AC 5 ms
4,384 KB
testcase_36 AC 4 ms
4,528 KB
testcase_37 AC 4 ms
4,520 KB
testcase_38 AC 5 ms
4,376 KB
testcase_39 AC 5 ms
4,380 KB
testcase_40 AC 5 ms
4,384 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 5 ms
4,376 KB
testcase_44 AC 6 ms
4,380 KB
testcase_45 AC 4 ms
4,508 KB
testcase_46 AC 5 ms
4,380 KB
testcase_47 AC 5 ms
4,400 KB
testcase_48 AC 4 ms
4,376 KB
testcase_49 AC 5 ms
4,384 KB
testcase_50 AC 5 ms
4,384 KB
testcase_51 AC 338 ms
4,380 KB
testcase_52 AC 211 ms
4,380 KB
testcase_53 AC 110 ms
4,380 KB
testcase_54 AC 380 ms
4,436 KB
testcase_55 AC 58 ms
4,384 KB
testcase_56 AC 169 ms
4,468 KB
testcase_57 AC 134 ms
4,400 KB
testcase_58 AC 24 ms
4,564 KB
testcase_59 AC 112 ms
4,380 KB
testcase_60 AC 23 ms
4,628 KB
testcase_61 AC 134 ms
4,576 KB
testcase_62 AC 35 ms
4,536 KB
testcase_63 AC 273 ms
4,504 KB
testcase_64 AC 134 ms
4,380 KB
testcase_65 AC 136 ms
4,380 KB
testcase_66 AC 160 ms
4,432 KB
testcase_67 AC 134 ms
4,384 KB
testcase_68 AC 134 ms
4,380 KB
testcase_69 AC 411 ms
4,376 KB
testcase_70 AC 23 ms
4,524 KB
testcase_71 AC 27 ms
4,804 KB
testcase_72 AC 47 ms
4,556 KB
testcase_73 AC 136 ms
4,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define int long long
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;
        int a[n];for(int i=0;i<n;++i) {cin>>a[i];--a[i];}
        int b[n];for(int i=0;i<n;++i) {cin>>b[i];--b[i];}
        bool ok[n]={0};
        int u[n][20];for(int i=0;i<n;++i) {u[i][0]=a[i];} for(int j=1;j<20;++j) for(int i=0;i<n;++i) u[i][j]=u[u[i][j-1]][j-1];
        for(int i=0;i<n;++i) ok[u[i][19]]=true;
        bool res=true;
        bool h[n]={0};
        auto check=[&](vector<int> v)
        {
            multiset<int> d;for(int i:v) d.insert(b[i]);
            for(int i=0;i<v.size();++i)
            {
                if(!count(v.begin(),v.end(),b[v[i]])) return false;
            }
            while(v.size()>1)
            {
            bool ok1=false;
            bool ok2=true;
            for(int i=0;i<v.size();++i) ok2&=(b[v[i]]==v[(i+1)%v.size()]); if(ok2) return true;
            for(int i=0;i<v.size();++i)
            {
                if(b[v[i]]!=v[i] && !d.count(v[i]))
                {
                    d.erase(d.find(b[v[i]]));v.erase(v.begin()+i);ok1=true;break;
                }
            }
            if(!ok1) return false;
            }
            return (b[v[0]]==v[0]);
        };
        for(int i=0;i<n;++i)
        {
            if(!ok[i])
            {
                int j=a[i];
                while(true)
                {
                    if(h[j]) break;
                    h[j]=true;
                    j=a[j];
                }
                res&=h[b[i]];
                fill(h,h+n,false);
            }
        }
        for(int i=0;i<n;++i)
        {
            if(!h[i] && ok[i])
            {
                vector<int> v;
                int j=i;
                while(true)
                {
                    if(h[j]) break;
                    h[j]=true;v.push_back(j);
                    j=a[j];
                }
                res&=check(v);
            }
        }
        puts(res ? "Yes" : "No");
    }
    return 0;
}
0