結果

問題 No.1370 置換門松列
ユーザー kkktymkkktym
提出日時 2021-01-29 22:57:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,547 bytes
コンパイル時間 1,270 ms
コンパイル使用メモリ 112,172 KB
実行使用メモリ 16,596 KB
最終ジャッジ日時 2023-08-07 22:47:01
合計ジャッジ時間 4,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 WA -
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 WA -
testcase_22 AC 44 ms
16,324 KB
testcase_23 WA -
testcase_24 AC 19 ms
6,516 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 35 ms
16,596 KB
testcase_28 AC 35 ms
16,316 KB
testcase_29 AC 26 ms
7,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
//****************************************
// Graph template
//****************************************

// status of edge
template <typename X>
struct Edge{
  int from;
  int to;
  X cost;

  Edge() = default;

  Edge(int from, int to, X cost) : from(from), to(to), cost(cost) {}
};

// status of node
template <typename X>
struct Node{ 
  int idx;
  vector<Edge<X>> edge;
  
  Node() = default;

  explicit Node(int idx) : idx(idx) {}
};

template <typename X>
class Graph{
private:
  int n; // number of node
  int m; // number of edge
  vector<Node<X>> node; 

  void Init_Node() {
    rep(i,n) node.emplace_back(i);
  }
public:
  explicit Graph(int n) : n(n) {
    Init_Node();
  }

  // edges have no-weight 
  Graph(int n, int m, vector<int> from, vector<int> to) : n(n), m(m) {
    Init_Node();
    rep(i,m) {
      add_edge(from[i], to[i]);
      //      add_edge(to[i], from[i]);      
    }
  }  

  // edges have weight
  Graph(int n, int m, vector<int> from, vector<int> to, vector<X> cost) : n(n), m(m) {
    Init_Node();
    rep(i,m) {
      add_edge(from[i], to[i], cost[i]);
      add_edge(to[i], from[i], cost[i]);      
    }
  }

  void add_edge(int from, int to, X cost = 1) {
    node[from].edge.emplace_back(from, to, cost);
  }

  // トポロジカルソートした頂点の配列を返す
  vector<int> Tsort() {
    // 入次数のカウント
    vector<int> in(n, 0);
    rep(i,n) {
      for(auto next: node[i].edge) {
	int w = next.to;
	in[w]++;
      }
    }

    queue<int> q; // 頂点を入れるキュー
    rep(i,n) {
      if(in[i] == 0) q.push(i);
    }

    vector<int> res;
    
    while( !q.empty() ) {
      int v = q.front(); q.pop();
      res.push_back(v);
      for(auto next: node[v].edge) {
	int w = next.to;
	in[w]--;
	if(in[w] == 0) {
	  q.push(w);
	}
      }
    }
    return res;
  }
  
};


int main()
{
  int n,m; cin >> n >> m;
  vi a(n);
  rep(i,n) {
    cin >> a[i];
    a[i]--;
  }

  Graph<int> gp(m);
  rep(i,n-1) {
    if(i&1) {
      gp.add_edge(a[i], a[i+1]);
    }
    else {
      gp.add_edge(a[i+1], a[i]);
    }
    if(i+2 < n) {
      if(a[i] == a[i+1] || a[i+1] == a[i+2] || a[i+2] == a[i]) {
        cout << "No" << "\n";
        return 0;
      }
    }
  }
  vi res = gp.Tsort();
  if(sz(res) == m) {
    cout << "Yes" << "\n";
    for(auto tmp: res) cout << tmp+1 << " ";
    cout << "\n";
    return 0;
  }
  Graph<int> gp2(m);
  rep(i,n-1) {
    if(i&1) {
      gp2.add_edge(a[i+1], a[i]);
    }
    else {
      gp2.add_edge(a[i], a[i+1]);
    }
  }
  res = gp2.Tsort();
  if(sz(res) == m) {
    cout << "Yes" << "\n";
    for(auto tmp: res) cout << tmp+1 << " ";
    cout << "\n";
    return 0;
  }
  
  cout << "No" << "\n";

  return 0;
}
0