結果

問題 No.1370 置換門松列
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-01-29 22:27:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 2,377 bytes
コンパイル時間 1,813 ms
コンパイル使用メモリ 178,320 KB
実行使用メモリ 8,948 KB
最終ジャッジ日時 2023-10-12 21:02:50
合計ジャッジ時間 3,901 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 45 ms
8,948 KB
testcase_22 AC 33 ms
7,884 KB
testcase_23 AC 42 ms
8,272 KB
testcase_24 AC 17 ms
4,408 KB
testcase_25 AC 52 ms
8,788 KB
testcase_26 AC 52 ms
8,892 KB
testcase_27 AC 25 ms
7,760 KB
testcase_28 AC 26 ms
7,916 KB
testcase_29 AC 22 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.01.29 22:27:00
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

struct DAG {
private:
    struct Edge {
        int to;
    };
    std::vector<std::vector<Edge>> graph;
    bool is_dag = false;
    std::vector<int> sorted; 
    int V; 
public:
    DAG(int v) {
        assert(v > 0);
        V = v;
        graph.resize(v);
    }
    
    void add_edge(int from, int to) {
        graph[from].push_back({to});
    }
    
    
    std::vector<int> topological_sort() {
        std::stack<int> sta;
        
        std::vector<int> in(V, 0);
        int used_cnt = 0;
        for (int i = 0; i < V; i++) {
            for (Edge e : graph[i]) {
                in[e.to]++;
            }
        }
        for (int i = 0; i < V; i++) if (in[i] == 0) {
            sta.push(i);
            used_cnt++;
        }
        while (!sta.empty()) {
            int p = sta.top(); sta.pop();
            sorted.push_back(p);
            for (Edge e : graph[p]) {
                int v = e.to;
                in[v]--;
                
                if (in[v] == 0) {
                    sta.push(v);
                    used_cnt++;
                }
            }
        }
        if (used_cnt == V) {
            return sorted;
        }
        else {
            return std::vector<int>(0);
        }
    }
    vector<Edge>& operator[](int x) {
        return graph[x];
    }
};
bool kadomatsu(int a,int b,int c) {
    return a != c && ((a < b && b > c) || (a > b && b < c));
}
int main() {
    int n,m;cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 2; i < n; i++) {
        if (a[i] == a[i-2]) {
            puts("No");
            return 0;
        }
    }
    DAG g(m);
    for (int i = 1; i < n; i++) {
        if (i % 2 == 1) {
            g.add_edge(a[i-1]-1,a[i]-1);
        }
        else {
            g.add_edge(a[i]-1,a[i-1]-1);
        }
    }
    auto p = g.topological_sort();
    if (p.size() == 0) {
        puts("No");
    }
    else {
        puts("Yes");
        vector<int> ans(m);
        for (int i = 0; i < p.size(); i++) {
            ans[p[i]] = i+1;
        }
        for (int i = 0; i < ans.size(); i++) {
            cout << ans[i] << " ";
        }
        cout << endl;
    }
    return 0;
}
0