結果

問題 No.1779 Magical Swap
ユーザー kyo1kyo1
提出日時 2021-12-09 13:12:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,112 bytes
コンパイル時間 2,379 ms
コンパイル使用メモリ 214,612 KB
実行使用メモリ 10,428 KB
最終ジャッジ日時 2023-09-24 06:28:09
合計ジャッジ時間 4,819 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 54 ms
9,372 KB
testcase_05 AC 21 ms
5,908 KB
testcase_06 AC 24 ms
6,284 KB
testcase_07 AC 44 ms
8,544 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 21 ms
4,704 KB
testcase_10 AC 104 ms
9,772 KB
testcase_11 AC 46 ms
6,484 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 74 ms
8,408 KB
testcase_14 AC 109 ms
10,428 KB
testcase_15 AC 39 ms
4,376 KB
testcase_16 AC 62 ms
10,308 KB
testcase_17 AC 50 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class DisjointSet {
 public:
  explicit DisjointSet(const std::size_t n) : nodes(n, Node(n, 1)) {}

  std::size_t size() const { return nodes.size(); }

  std::size_t size(const std::size_t x) { return nodes[find(x)].size; }

  std::size_t find(const std::size_t x) {
    if (nodes[x].parent == size()) return x;
    return nodes[x].parent = find(nodes[x].parent);
  }

  bool is_same(const std::size_t x, const std::size_t y) { return find(x) == find(y); }

  bool unite(const std::size_t x, const std::size_t y) {
    std::size_t rx = find(x), ry = find(y);
    if (rx == ry) return false;
    if (nodes[rx].size < nodes[ry].size) std::swap(rx, ry);
    nodes[rx].size += nodes[ry].size;
    nodes[ry].parent = rx;
    return true;
  }

 private:
  class Node {
   private:
    friend DisjointSet;

    Node(const std::size_t parent, const std::size_t size) : parent(parent), size(size) {}

    std::size_t parent;
    std::size_t size;
  };

  std::vector<Node> nodes;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  auto solve = [&]() {
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    for (auto &&e : A) {
      cin >> e;
    }
    for (auto &&e : B) {
      cin >> e;
    }
    DisjointSet ds(N);
    for (int k = 2; k < N; k++) {
      for (int i = 1; k * (i + 1) - 1 < N; i++) {
        ds.unite(k * i - 1, k * (i + 1) - 1);
      }
    }
    map<int, multiset<int>> mp;
    for (int i = 0; i < N; i++) {
      mp[ds.find(i)].insert(B[i]);
    }
    bool ok = true;
    for (int i = 0; i < N; i++) {
      // for (const auto &[key, value] : mp) {
      //   cerr << "[+]" << ' ' << key << endl;
      //   for (const auto &e : value) {
      //     cerr << e << '\n';
      //   }
      // }
      // cerr << "--------" << endl;
      if (mp[ds.find(i)].find(A[i]) == mp[ds.find(i)].end()) {
        ok = false;
        break;
      }
      mp[ds.find(i)].erase(mp[ds.find(i)].find(A[i]));
    }
    cout << (ok ? "Yes" : "No") << '\n';
  };
  for (int i = 0; i < T; i++) {
    solve();
  }
  return 0;
}
0