結果
| 問題 | No.1779 Magical Swap | 
| コンテスト | |
| ユーザー |  ytft | 
| 提出日時 | 2021-12-26 12:43:08 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 290 ms / 2,000 ms | 
| コード長 | 1,578 bytes | 
| コンパイル時間 | 2,078 ms | 
| コンパイル使用メモリ | 184,340 KB | 
| 実行使用メモリ | 18,048 KB | 
| 最終ジャッジ日時 | 2024-09-22 20:46:58 | 
| 合計ジャッジ時間 | 5,942 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 18 | 
ソースコード
#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();
    }
}
            
            
            
        