結果

問題 No.1370 置換門松列
ユーザー milanis48663220
提出日時 2021-02-07 00:12:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 109,880 KB
最終ジャッジ日時 2025-01-18 13:44:02
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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