結果

問題 No.2346 Replace!!
ユーザー rniyarniya
提出日時 2023-06-10 15:42:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,507 bytes
コンパイル時間 2,034 ms
コンパイル使用メモリ 205,896 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-30 22:41:10
合計ジャッジ時間 6,831 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 4 ms
4,380 KB
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 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
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 2 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 WA -
testcase_32 AC 2 ms
4,380 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 WA -
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 3 ms
4,500 KB
testcase_51 AC 38 ms
4,376 KB
testcase_52 AC 26 ms
4,380 KB
testcase_53 AC 16 ms
4,376 KB
testcase_54 AC 31 ms
4,380 KB
testcase_55 AC 5 ms
4,500 KB
testcase_56 AC 51 ms
4,380 KB
testcase_57 WA -
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 8 ms
4,380 KB
testcase_60 AC 3 ms
4,376 KB
testcase_61 WA -
testcase_62 AC 3 ms
4,380 KB
testcase_63 AC 20 ms
4,376 KB
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 AC 3 ms
4,380 KB
testcase_72 AC 2 ms
4,380 KB
testcase_73 AC 55 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

/*
functional graph
連結成分に限定して良い
到達可能な頂点にしか変更できない
ひげの部分は葉から順に揃えていけば OK
サイクル部分は?
サイクルに対して操作すると?
頂点が削除されてサイクルから外れる
i -> B[i] のグラフを考える
入次数 0 の点から決めていける
サイクルができたら OUT
*/

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--;

    vector<int> deg(N, 0);
    for (int i = 0; i < N; i++) {
        if (A[i] == B[i]) continue;
        deg[B[i]]++;
    }
    queue<int> que;
    for (int i = 0; i < N; i++) {
        if (A[i] == B[i]) continue;
        if (deg[i] == 0) que.emplace(i);
    }
    while (not que.empty()) {
        int v = que.front();
        que.pop();
        vector<bool> seen(N, false);
        for (int cur = v; not seen[cur]; cur = A[cur]) seen[cur] = true;
        debug(v, B[v], seen);
        if (not seen[B[v]]) {
            cout << "No\n";
            return;
        }
        if (--deg[B[v]] == 0) que.emplace(B[v]);
    }
    debug(deg);

    for (int i = 0; i < N; i++) {
        if (deg[i] > 0) {
            cout << "No\n";
            return;
        }
    }
    cout << "Yes\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    for (; T--;) solve();
    return 0;
}
0