結果

問題 No.1779 Magical Swap
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-01-16 17:09:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 1,241 ms
コンパイル使用メモリ 114,028 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2023-08-30 00:14:07
合計ジャッジ時間 5,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 10 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 145 ms
11,996 KB
testcase_05 AC 52 ms
6,788 KB
testcase_06 AC 59 ms
7,256 KB
testcase_07 AC 113 ms
10,468 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 31 ms
5,664 KB
testcase_10 AC 148 ms
12,224 KB
testcase_11 AC 68 ms
7,768 KB
testcase_12 AC 20 ms
4,944 KB
testcase_13 AC 105 ms
10,220 KB
testcase_14 AC 140 ms
13,568 KB
testcase_15 AC 189 ms
4,380 KB
testcase_16 AC 152 ms
13,048 KB
testcase_17 AC 94 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>

using namespace std;
 
struct UnionFind {
    vector<long long> par;
    vector<long long> siz;

    UnionFind(long long N) : par(N), siz(N) {
        for(long long i = 0; i < N; i++){
            par[i] = i;
            siz[i] = 1;
        }   
    }   
 
    long long root(long long x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }   
 
    long long unite(long long x, long long y) {
        long long rx = root(x);
        long long ry = root(y);
        if (rx == ry) return rx; 
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry; 
        siz[ry] += siz[rx];
        return ry; 
    }   
 
    bool same(long long x, long long y) {
        long long rx = root(x);
        long long ry = root(y);
        return rx == ry; 
    }   
 
    long long size(long long x){ 
        return siz[root(x)];
    }   
};

void solve(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    multiset<int> st1, st2;
    for (int i=0; i<N; i++){
        cin >> A[i];
        st1.insert(A[i]);
    }
    for (int i=0; i<N; i++){
        cin >> B[i];
        st2.insert(B[i]);
    }
    if (st1 != st2){
        cout << "No" << endl;
        return;
    }

    vector<bool> ok(N+1, 1);
    for (int i=2; i<=N/2; i++){
        for (int j=i; j<=N; j+=i) ok[j] = 0;
    }
    for (int i=1; i<=N; i++){
        if (ok[i]){
            if (A[i-1] != B[i-1]){
                cout << "No" << endl;
                return;
            }
        }
    }
    cout << "Yes" << endl;
    return;
}

int main(){
 
    long long T;
    cin >> T;
    while(T){
        solve();
        T--;
    }
 
    return 0;
}
0