結果

問題 No.2346 Replace!!
ユーザー t98slidert98slider
提出日時 2023-06-09 23:34:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,361 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 188,864 KB
実行使用メモリ 4,920 KB
最終ジャッジ日時 2023-08-30 14:41:39
合計ジャッジ時間 6,229 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

namespace internal_scc{
template <class E> struct csr {
    std::vector<int> start;
    std::vector<E> elist;
    csr(int n, const std::vector<std::pair<int, E>>& edges)
        : start(n + 1), elist(edges.size()) {
        for (auto e : edges) {
            start[e.first + 1]++;
        }
        for (int i = 1; i <= n; i++) {
            start[i] += start[i - 1];
        }
        auto counter = start;
        for (auto e : edges) {
            elist[counter[e.first]++] = e.second;
        }
    }
};
struct scc_graph {
  public:
    scc_graph(int n) : _n(n) {}
    int num_vertices() { return _n; }
    void add_edge(int from, int to) { edges.push_back({from, {to}}); }
    std::pair<int, std::vector<int>> scc_ids() {
        auto g = csr<edge>(_n, edges);
        int now_ord = 0, group_num = 0;
        std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);
        visited.reserve(_n);
        auto dfs = [&](auto self, int v) -> void {
            low[v] = ord[v] = now_ord++;
            visited.push_back(v);
            for (int i = g.start[v]; i < g.start[v + 1]; i++) {
                auto to = g.elist[i].to;
                if (ord[to] == -1) {
                    self(self, to);
                    low[v] = std::min(low[v], low[to]);
                } else {
                    low[v] = std::min(low[v], ord[to]);
                }
            }
            if (low[v] == ord[v]) {
                while (true) {
                    int u = visited.back();
                    visited.pop_back();
                    ord[u] = _n;
                    ids[u] = group_num;
                    if (u == v) break;
                }
                group_num++;
            }
        };
        for (int i = 0; i < _n; i++) {
            if (ord[i] == -1) dfs(dfs, i);
        }
        for (auto& x : ids) {
            x = group_num - 1 - x;
        }
        return {group_num, ids};
    }

    std::vector<std::vector<int>> scc() {
        auto ids = scc_ids();
        int group_num = ids.first;
        std::vector<int> counts(group_num);
        for (auto x : ids.second) counts[x]++;
        std::vector<std::vector<int>> groups(ids.first);
        for (int i = 0; i < group_num; i++) {
            groups[i].reserve(counts[i]);
        }
        for (int i = 0; i < _n; i++) {
            groups[ids.second[i]].push_back(i);
        }
        return groups;
    }

  private:
    int _n;
    struct edge {
        int to;
    };
    std::vector<std::pair<int, edge>> edges;
};
}

struct scc_graph {
  public:
    scc_graph() : internal(0) {}
    scc_graph(int n) : internal(n) {}
    void add_edge(int from, int to) {
        int n = internal.num_vertices();
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        internal.add_edge(from, to);
    }
    std::vector<std::vector<int>> scc() { return internal.scc(); }
  private:
    internal_scc::scc_graph internal;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        vector<int> a(n), b(n);
        for(auto &&v : a) cin >> v, v--;
        for(auto &&v : b) cin >> v, v--;

        vector<vector<int>> g(n);
        vector<int> par(n, -1), depth(n);
        scc_graph g2(n);
        for(int i = 0; i < n; i++){
            if(a[i] != b[i]){
                g2.add_edge(i, a[i]);
                g[a[i]].emplace_back(i);
                par[i] = a[i];
            }
        }
        if(g2.scc().size() != n){
            cout << "No" << '\n';
            continue;
        }
        priority_queue<pair<int,int>> pq;
        vector<int> tmp;
        bool flag = true;
        function<void(int,int)> dfs = [&](int v, int p){
            depth[v] = tmp.size();
            if(p != -1){
                for(int j = tmp.size() - 1; j >= 0; j--){
                    if(tmp[j] == b[j]){
                        pq.emplace(j, v);
                        break;
                    }
                    if(j == 0){
                        flag = false;
                        break;
                    }
                }
            }
            tmp.emplace_back(a[v]);
            for(auto &&u : g[v]){
                if(u == p) continue;
                dfs(u, v);
            }
            tmp.pop_back();
        };
        for(int i = 0; i < n; i++){
            if(par[i] == -1) dfs(i, -1);
        }

        if(!flag){
            cout << "No" << '\n';
            continue;
        }

        vector<int> rute;
        while(!pq.empty()){
            int d, v, fr;
            tie(d, v) = pq.top();
            pq.pop();

            fr = v;
            while(v != -1){
                if(a[v] == b[fr]) break;
                rute.emplace_back(v);
                v = par[v];
            }

            if(v == -1){
                flag = false;
                break;
            }

            if(depth[v] == d){
                while(!rute.empty()){
                    a[rute.back()] = b[fr];
                    rute.pop_back();
                }
            }else{
                pq.emplace(depth[v], fr);
            }
        }

        if(!flag){
            cout << "No" << '\n';
            continue;
        }

        cout << (a == b ? "Yes" : "No") << '\n';
    }
}
0