結果

問題 No.1370 置換門松列
ユーザー milanis48663220milanis48663220
提出日時 2021-02-07 00:12:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 115,676 KB
実行使用メモリ 12,072 KB
最終ジャッジ日時 2023-10-12 21:06:18
合計ジャッジ時間 3,156 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,476 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,356 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 1 ms
4,356 KB
testcase_21 AC 31 ms
12,072 KB
testcase_22 AC 22 ms
12,068 KB
testcase_23 AC 24 ms
10,280 KB
testcase_24 AC 8 ms
4,356 KB
testcase_25 AC 40 ms
11,992 KB
testcase_26 AC 40 ms
11,992 KB
testcase_27 AC 16 ms
9,984 KB
testcase_28 AC 17 ms
10,124 KB
testcase_29 AC 10 ms
4,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

bool topological_sort(vector<vector<int>> g, vector<int> &order){
    int n = g.size();
    order.clear();
    vector<bool> used(n, false);
    function<void(int)> dfs = [&](int v){
        used[v] = true;
        for(int to : g[v]){
            if(!used[to]) dfs(to);
        }
        order.push_back(v);
    };
    for(int v = 0; v < n; v++){
        if(!used[v]) dfs(v);
    }
    reverse(order.begin(), order.end());
    vector<int> inv_order(n);
    for(int i = 0; i < n; i++) inv_order[order[i]] = i;
    for(int v = 0; v < n; v++){
        for(int u : g[v]){
            if(inv_order[v] > inv_order[u]) return false;
        }
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        a[i]--;
    }
    vector<vector<int>> g(m);
    for(int i = 0; i < n-1; i++){
        if(a[i] == a[i+1]){
            cout << "No" << endl;
            return 0;
        }
        if(i < n-2 && a[i] == a[i+2]){
            cout << "No" << endl;
            return 0;
        }
        if(i%2 == 0){
            g[a[i]].push_back(a[i+1]);
        }else{
            g[a[i+1]].push_back(a[i]);
        }
    }
    vector<int> tsort;
    if(!topological_sort(g, tsort)){
        cout << "No" << endl;
        return 0;
    }
    cout << "Yes" << endl;
    vector<int> ans(m);
    for(int i = 0; i < m; i++) ans[tsort[i]] = i+1;
    for(int x : ans) cout << x << ' ';
    cout << endl;
}
0