結果

問題 No.2346 Replace!!
ユーザー LeoProLeoPro
提出日時 2023-06-09 23:06:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,869 bytes
コンパイル時間 2,655 ms
コンパイル使用メモリ 218,676 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-30 14:17:10
合計ジャッジ時間 12,998 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 4 ms
4,376 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 4 ms
4,384 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 WA -
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 5 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 12 ms
4,380 KB
testcase_30 AC 7 ms
4,376 KB
testcase_31 AC 98 ms
4,380 KB
testcase_32 AC 98 ms
4,376 KB
testcase_33 AC 98 ms
4,384 KB
testcase_34 AC 100 ms
4,376 KB
testcase_35 AC 98 ms
4,376 KB
testcase_36 AC 98 ms
4,376 KB
testcase_37 AC 97 ms
4,380 KB
testcase_38 AC 97 ms
4,380 KB
testcase_39 AC 98 ms
4,380 KB
testcase_40 AC 96 ms
4,376 KB
testcase_41 AC 97 ms
4,376 KB
testcase_42 AC 98 ms
4,376 KB
testcase_43 AC 98 ms
4,376 KB
testcase_44 AC 100 ms
4,376 KB
testcase_45 AC 99 ms
4,380 KB
testcase_46 AC 98 ms
4,380 KB
testcase_47 AC 97 ms
4,376 KB
testcase_48 AC 99 ms
4,380 KB
testcase_49 AC 99 ms
4,380 KB
testcase_50 AC 98 ms
4,380 KB
testcase_51 AC 134 ms
4,380 KB
testcase_52 AC 88 ms
4,376 KB
testcase_53 AC 52 ms
4,376 KB
testcase_54 AC 282 ms
4,376 KB
testcase_55 AC 282 ms
4,376 KB
testcase_56 AC 276 ms
4,376 KB
testcase_57 AC 228 ms
4,380 KB
testcase_58 AC 283 ms
4,380 KB
testcase_59 AC 284 ms
4,380 KB
testcase_60 AC 282 ms
4,380 KB
testcase_61 AC 219 ms
4,376 KB
testcase_62 AC 281 ms
4,392 KB
testcase_63 AC 282 ms
4,376 KB
testcase_64 AC 220 ms
4,380 KB
testcase_65 AC 222 ms
4,376 KB
testcase_66 AC 277 ms
4,376 KB
testcase_67 AC 220 ms
4,380 KB
testcase_68 AC 221 ms
4,380 KB
testcase_69 AC 282 ms
4,376 KB
testcase_70 AC 282 ms
4,384 KB
testcase_71 AC 282 ms
4,376 KB
testcase_72 AC 283 ms
4,380 KB
testcase_73 AC 220 ms
4,376 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