結果

問題 No.1779 Magical Swap
ユーザー SSRSSSRS
提出日時 2021-12-08 00:06:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 1,802 ms
コンパイル使用メモリ 178,360 KB
実行使用メモリ 19,540 KB
最終ジャッジ日時 2023-09-23 07:46:55
合計ジャッジ時間 4,300 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 11 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 135 ms
17,228 KB
testcase_05 AC 53 ms
9,220 KB
testcase_06 AC 60 ms
9,792 KB
testcase_07 AC 111 ms
15,112 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 31 ms
6,680 KB
testcase_10 AC 146 ms
17,844 KB
testcase_11 AC 70 ms
10,604 KB
testcase_12 AC 22 ms
5,720 KB
testcase_13 AC 110 ms
14,656 KB
testcase_14 AC 147 ms
19,528 KB
testcase_15 AC 213 ms
4,376 KB
testcase_16 AC 155 ms
19,540 KB
testcase_17 AC 103 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N;
    cin >> N;
    vector<int> A(N);
    for (int j = 0; j < N; j++){
      cin >> A[j];
    }
    vector<int> B(N);
    for (int j = 0; j < N; j++){
      cin >> B[j];
    }
    vector<vector<int>> E(N);
    for (int j = 2; j <= N; j++){
      for (int k = j * 2; k <= N; k += j){
        E[j - 1].push_back(k - 1);
        E[k - 1].push_back(j - 1);
      }
    }
    vector<bool> used(N, false);
    bool ok = true;
    for (int j = 0; j < N; j++){
      if (!used[j]){
        used[j] = true;
        vector<int> a, b;
        queue<int> Q;
        Q.push(j);
        while (!Q.empty()){
          int v = Q.front();
          Q.pop();
          a.push_back(A[v]);
          b.push_back(B[v]);
          for (int w : E[v]){
            if (!used[w]){
              used[w] = true;
              Q.push(w);
            }
          }
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        if (a != b){
          ok = false;
        }
      }
    }
    if (ok){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
0