結果

問題 No.1370 置換門松列
ユーザー ocvret_ocvret_
提出日時 2021-01-29 22:54:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 1,813 ms
コンパイル使用メモリ 177,000 KB
実行使用メモリ 8,564 KB
最終ジャッジ日時 2023-10-12 21:03:45
合計ジャッジ時間 3,691 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 44 ms
8,484 KB
testcase_22 AC 33 ms
7,888 KB
testcase_23 AC 40 ms
8,028 KB
testcase_24 AC 18 ms
4,348 KB
testcase_25 AC 51 ms
8,564 KB
testcase_26 AC 51 ms
8,564 KB
testcase_27 AC 29 ms
7,812 KB
testcase_28 AC 30 ms
7,844 KB
testcase_29 AC 25 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>

using namespace std;
// using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

// 2021/1/14
vector<int> topological_sort(vector<vector<int>> &v){
  int n = v.size();
  vector<int> deg(n);
  vector<int> ord;
  for(int i = 0;i < n;i++){
    for(auto &to : v[i])deg[to]++;
  }
  stack<int> sck;
  for(int i = 0;i < n;i++)if(deg[i] == 0)sck.push(i);
  while(!sck.empty()){
    int ov = sck.top();sck.pop();
    ord.push_back(ov);
    for(auto nv : v[ov]){
      deg[nv]--;
      if(deg[nv] == 0)sck.push(nv);
    }
  }
  return ord;
}

int main(){
  

  int n,m;
  cin >> n >> m;
  vector<int> a(n);
  vector<vector<int>> v(m);
  rep(i,n)cin >> a[i],a[i]--;
  rep(i,n-2)if(a[i] == a[i+2]){
    cout << "No\n";
    return 0;
  }
  rep(i,n){
    if(i & 1){
      v[a[i-1]].push_back(a[i]);
      if(i+1 < n)v[a[i+1]].push_back(a[i]);
    }
  }
  rep(i,m){
    sort(ALL(v[i]));
    v[i].erase(unique(ALL(v[i])),v[i].end());
  }
  vector<int> ord(topological_sort(v));
  if(ord.size() != m){
    cout << "No\n";
  }else{
    cout << "Yes\n";
    vector<int> res(m,-1);
    int nu = 1;
    rep(i,m)res[ord[i]] = nu++;
    rep(i,m)cout << res[i] << " \n"[i+1 == m];
  }
  

  

  return 0;
}
0