結果

問題 No.1370 置換門松列
ユーザー outlineoutline
提出日時 2021-03-10 19:02:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,573 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 141,364 KB
実行使用メモリ 11,268 KB
最終ジャッジ日時 2023-10-12 21:06:53
合計ジャッジ時間 3,416 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 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 1 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,348 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 2 ms
4,348 KB
testcase_21 AC 24 ms
8,812 KB
testcase_22 AC 24 ms
11,268 KB
testcase_23 AC 22 ms
8,196 KB
testcase_24 AC 9 ms
4,728 KB
testcase_25 AC 30 ms
8,644 KB
testcase_26 AC 30 ms
8,764 KB
testcase_27 AC 19 ms
11,136 KB
testcase_28 AC 18 ms
11,020 KB
testcase_29 AC 11 ms
5,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int n, m;
int a[100005], ans[100005];
bool topological_sort(vector<vector<int>>& g){
    vector<int> indeg(m);
    for(int i = 0; i < m; ++i){
        for(int j : g[i])   indeg[j] += 1;
    }
    queue<int> que;
    for(int i = 0; i < m; ++i){
        if(indeg[i] == 0)   que.emplace(i);
    }
    vector<int> vs;
    while(!que.empty()){
        int u = que.front();
        que.pop();
        vs.emplace_back(u);
        for(int v : g[u]){
            if(--indeg[v] == 0) que.emplace(v);
        }
    }
    if(vs.size() != m)  return false;
    for(int i = 0; i < m; ++i){
        ans[vs[i]] = i + 1;
    }
    return true;
}

void output(){
    cout << "Yes\n";
    for(int i = 0; i < m; ++i){
        cout << ans[i] << (i == m - 1 ? '\n': ' ');
    }
}

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

    cin >> n >> m;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        --a[i];
    }
    for(int i = 0; i + 2 < n; ++i){
        if(a[i] == a[i + 1] || a[i + 1] == a[i + 2] || a[i + 2] == a[i]){
            cout << "No\n";
            return 0;
        }
    }

    vector<vector<int>> g(m);
    for(int i = 0; i < n; i += 2){
        if(i - 1 >= 0){
            g[a[i]].emplace_back(a[i - 1]);
        }
        if(i + 1 < n){
            g[a[i]].emplace_back(a[i + 1]);
        }
    }
    if(topological_sort(g)){
        output();
        return 0;
    }

    vector<vector<int>> g2(m);
    for(int i = 1; i < n; i += 2){
        g2[a[i]].emplace_back(a[i - 1]);
        if(i + 1 < n){
            g2[a[i]].emplace_back(a[i + 1]);
        }
    }
    if(topological_sort(g2))    output();
    else    cout << "No\n";

    return 0;
}
0