結果

問題 No.2346 Replace!!
ユーザー LeoProLeoPro
提出日時 2023-06-09 23:10:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 2,871 bytes
コンパイル時間 2,574 ms
コンパイル使用メモリ 217,872 KB
実行使用メモリ 4,428 KB
最終ジャッジ日時 2023-08-30 14:20:03
合計ジャッジ時間 12,751 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 11 ms
4,376 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 99 ms
4,380 KB
testcase_32 AC 98 ms
4,376 KB
testcase_33 AC 99 ms
4,380 KB
testcase_34 AC 100 ms
4,376 KB
testcase_35 AC 99 ms
4,380 KB
testcase_36 AC 99 ms
4,384 KB
testcase_37 AC 97 ms
4,380 KB
testcase_38 AC 98 ms
4,384 KB
testcase_39 AC 99 ms
4,380 KB
testcase_40 AC 99 ms
4,376 KB
testcase_41 AC 97 ms
4,380 KB
testcase_42 AC 98 ms
4,380 KB
testcase_43 AC 100 ms
4,376 KB
testcase_44 AC 100 ms
4,376 KB
testcase_45 AC 99 ms
4,380 KB
testcase_46 AC 97 ms
4,380 KB
testcase_47 AC 96 ms
4,380 KB
testcase_48 AC 99 ms
4,376 KB
testcase_49 AC 99 ms
4,380 KB
testcase_50 AC 98 ms
4,380 KB
testcase_51 AC 136 ms
4,376 KB
testcase_52 AC 89 ms
4,376 KB
testcase_53 AC 53 ms
4,380 KB
testcase_54 AC 292 ms
4,380 KB
testcase_55 AC 290 ms
4,384 KB
testcase_56 AC 287 ms
4,376 KB
testcase_57 AC 222 ms
4,380 KB
testcase_58 AC 291 ms
4,384 KB
testcase_59 AC 291 ms
4,428 KB
testcase_60 AC 289 ms
4,380 KB
testcase_61 AC 219 ms
4,376 KB
testcase_62 AC 290 ms
4,380 KB
testcase_63 AC 290 ms
4,380 KB
testcase_64 AC 220 ms
4,380 KB
testcase_65 AC 221 ms
4,380 KB
testcase_66 AC 286 ms
4,380 KB
testcase_67 AC 220 ms
4,376 KB
testcase_68 AC 220 ms
4,380 KB
testcase_69 AC 291 ms
4,376 KB
testcase_70 AC 289 ms
4,384 KB
testcase_71 AC 291 ms
4,384 KB
testcase_72 AC 291 ms
4,376 KB
testcase_73 AC 220 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define int long long

using namespace std;
using ll = long long;

void solve();

template<typename ...Args>
void println(Args... args) {
    apply([](auto &&... args) { ((cout << args << ' '), ...); }, tuple(args...));
    cout << '\n';
}

int32_t main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t = 1;
    cin >> t;
    for (int tc = 0; tc < t; ++tc) {
        solve();
    }
    return 0;
}
struct DSU {
    vector<int> sz, p;

    explicit DSU(int n) : sz(n, 1), p(n) { iota(p.begin(), p.end(), 0); }

    int get(int x) { return x == p[x] ? x : p[x] = get(p[x]); }

    bool unite(int x, int y) {
        x = get(x), y = get(y);
        if (x == y) return false;
        if (sz[x] > sz[y]) swap(x, y);
        sz[y] += sz[x];
        p[x] = y;
        return true;
    }
};
bool sub(vector<int> in, vector<int> a) {
    map<int, int> pos;
    for (int i = 0; i < a.size(); ++i) pos[a[i]] = i;
    in.push_back(in.front());
    int tot = 0;
    for (int i = 0; i + 1 < in.size(); ++i) {
        tot += pos[in[i + 1]] <= pos[in[i]];
    }
    assert(tot > 0);
    return tot == 1;
}

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int &x : a) cin >> x, --x;
    for (int &x : b) cin >> x, --x;
//    if (a == b) return println("No");
    vector<int> ok1(n, 1);
    DSU dsu(n);
    for (int i = 0; i < n; ++i) {
        dsu.unite(i, a[i]);
        for (int j = a[i], cnt = 0; cnt < n + 3; j = a[j], cnt++) {
            if (j == i) ok1[i] = 0;
        }
    }
    vector<int> ok2(n, 1);
    for (int i = 0; i < n; ++i) {
        {
            int x = a[i];
            bool ok = x == b[i];
            for (int k = 0; k < n + 3; ++k) {
                x = a[x];
                ok |= x == b[i];
            }
            if (!ok) return println("No");
        }
        if (!ok1[i]) {
            ok2[i] = 1;
            for (int j = b[i], cnt = 0; cnt < n + 3; j = b[j], cnt++) {
                if (dsu.get(j) != dsu.get(i) || ok1[j]) return println("No");
                if (j == i) ok2[i] = 0;
            }
        } else {
            ok2[i] = 1;
        }
    }
    vector<bool> checked(n);
    vector<bool> seen(n);
    for (int i = 0; i < n; ++i) {
        if (ok1[i] || ok2[i]) checked[i] = true;
        if (!checked[i]) {
            vector<int> ac{i}, bc{i};
            while (a[ac.back()] != ac.front()) ac.push_back(a[ac.back()]);
            while (b[bc.back()] != bc.front()) bc.push_back(b[bc.back()]);
            for (int x : bc) checked[x] = 1;
            for (int x : ac) if (seen[x]) return println("No");
            bool ok = false;
//            for (int x : ac) if (a[x] == b[x]) ok = true; if (!ok) return println("No");
            for (int x : ac) seen[x] = 1;
            if (!sub(bc, ac)) return println("No");
        }
    }
    println("Yes");
}
0