結果

問題 No.1779 Magical Swap
ユーザー ytftytft
提出日時 2021-12-26 12:43:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 184,048 KB
実行使用メモリ 18,112 KB
最終ジャッジ日時 2023-10-24 03:49:00
合計ジャッジ時間 6,555 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 12 ms
4,348 KB
testcase_03 AC 6 ms
4,348 KB
testcase_04 AC 278 ms
17,056 KB
testcase_05 AC 103 ms
9,400 KB
testcase_06 AC 122 ms
10,192 KB
testcase_07 AC 234 ms
15,208 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 56 ms
6,988 KB
testcase_10 AC 306 ms
16,792 KB
testcase_11 AC 137 ms
10,256 KB
testcase_12 AC 38 ms
5,952 KB
testcase_13 AC 228 ms
13,624 KB
testcase_14 AC 319 ms
18,112 KB
testcase_15 AC 218 ms
4,348 KB
testcase_16 AC 169 ms
5,440 KB
testcase_17 AC 138 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
    vector<int> parent;
    vector<int> numberOfElements;
    UnionFind(int n){
        parent.resize(n);
        numberOfElements.resize(n);
        for(int i=0;i<n;++i){
            parent[i]=-1;
            numberOfElements[i]=1;
        }
    }
    int find(int a){
        vector<int> path(0);
        int pointer=a;
        while(parent[pointer]!=-1){
            path.push_back(pointer);
            pointer=parent[pointer];
        }
        for(int i:path){
            parent[i]=pointer;
        }
        return pointer;
    }
    void unite(int a,int b){
        int p=find(a);
        int q=find(b);
        if(p==q){
            return;
        }
        if(numberOfElements[p]<numberOfElements[q]){
            parent[p]=q;
            numberOfElements[q]+=numberOfElements[p];
        }else{
            parent[q]=p;
            numberOfElements[p]+=numberOfElements[q];
        }
    }
};

void solve(){
    int N,input;
    cin>>N;
    map<list<int>,int> m;
    UnionFind U(N);
    for(int j=2;j<=N;++j){
        for(int k=j*2;k<=N;k+=j){
            U.unite(j-1,k-1);
        }
    }
    for(int j=0;j<N;++j){
        cin>>input;
        ++m[{U.find(j),input}];
    }
    for(int j=0;j<N;++j){
        cin>>input;
        --m[{U.find(j),input}];
    }
    for(auto j=m.begin();j!=m.end();++j){
        if(j->second){
            cout<<"No"<<endl;
            return;
        }
    }
    cout<<"Yes"<<endl;
}

int main(){
    int T;
    cin>>T;
    for(int i=0;i<T;++i){
        solve();
    }
}
0