結果

問題 No.1370 置換門松列
ユーザー MisterMister
提出日時 2021-01-29 21:52:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,720 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 87,504 KB
最終ジャッジ日時 2025-01-18 09:09:32
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/topological_sort.hpp"

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp"

#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};

template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
    using std::vector<std::vector<Edge<Cost>>>::vector;

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};
#line 4 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/topological_sort.hpp"

#include <algorithm>

template <class Cost = int>
struct TopologicalSort {
    Graph<Cost> graph;
    std::vector<bool> visited;
    std::vector<int> vs, id;

    explicit TopologicalSort(const Graph<Cost>& graph)
        : graph(graph), visited(graph.size(), false), id(graph.size()) {
        for (int v = 0; v < (int)graph.size(); ++v) dfs(v);
        std::reverse(vs.begin(), vs.end());

        for (int i = 0; i < (int)graph.size(); ++i) id[vs[i]] = i;
    }

    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (const auto& e : graph[v]) dfs(e.dst);
        vs.push_back(v);
    }
};
#line 2 "main.cpp"
#include <iostream>
#line 5 "main.cpp"

using namespace std;

bool judge(int a, int b, int c) {
    if (a == c) return false;
    return (a < b && b > c) ||
           (a > b && b < c);
}

bool judge_all(const vector<int>& xs) {
    int n = xs.size();
    for (int i = 0; i + 2 < n; ++i) {
        if (!judge(xs[i], xs[i + 1], xs[i + 2])) return false;
    }
    return true;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> xs(n);
    for (auto& x : xs) {
        cin >> x;
        --x;
    }

    Graph<> graph(m);
    for (int i = 0; i + 1 < n; ++i) {
        if (i % 2 == 0) {
            graph.span(true, xs[i], xs[i + 1]);
        } else {
            graph.span(true, xs[i + 1], xs[i]);
        }
    }

    TopologicalSort ts(graph);
    auto ys = ts.id;

    vector<int> zs(n);
    for (int i = 0; i < n; ++i) zs[i] = ys[xs[i]];
    if (!judge_all(zs)) {
        cout << "No\n";
        return;
    }

    cout << "Yes\n";
    for (auto y : ys) cout << y + 1 << " ";
    cout << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0