結果

問題 No.2346 Replace!!
ユーザー LeoProLeoPro
提出日時 2023-06-09 22:50:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,610 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 216,920 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-30 14:00:38
合計ジャッジ時間 13,151 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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);
    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 (ac.back() != ac.front()) ac.push_back(a[ac.back()]);
            while (bc.back() != bc.front()) bc.push_back(a[bc.back()]);
            for (int x : ac) checked[x] = 1;
            if (!sub(bc, ac)) return println("No");
        }
    }
    println("Yes");
}
0