結果
問題 | No.1370 置換門松列 |
ユーザー |
![]() |
提出日時 | 2021-01-29 22:27:03 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 55 ms / 2,000 ms |
コード長 | 2,377 bytes |
コンパイル時間 | 2,101 ms |
コンパイル使用メモリ | 180,464 KB |
実行使用メモリ | 9,076 KB |
最終ジャッジ日時 | 2024-09-14 19:21:23 |
合計ジャッジ時間 | 3,519 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 25 |
ソースコード
/*** @FileName a.cpp* @Author kanpurin* @Created 2021.01.29 22:27:00**/#include "bits/stdc++.h"using namespace std;typedef long long ll;struct DAG {private:struct Edge {int to;};std::vector<std::vector<Edge>> graph;bool is_dag = false;std::vector<int> sorted;int V;public:DAG(int v) {assert(v > 0);V = v;graph.resize(v);}void add_edge(int from, int to) {graph[from].push_back({to});}std::vector<int> topological_sort() {std::stack<int> sta;std::vector<int> in(V, 0);int used_cnt = 0;for (int i = 0; i < V; i++) {for (Edge e : graph[i]) {in[e.to]++;}}for (int i = 0; i < V; i++) if (in[i] == 0) {sta.push(i);used_cnt++;}while (!sta.empty()) {int p = sta.top(); sta.pop();sorted.push_back(p);for (Edge e : graph[p]) {int v = e.to;in[v]--;if (in[v] == 0) {sta.push(v);used_cnt++;}}}if (used_cnt == V) {return sorted;}else {return std::vector<int>(0);}}vector<Edge>& operator[](int x) {return graph[x];}};bool kadomatsu(int a,int b,int c) {return a != c && ((a < b && b > c) || (a > b && b < c));}int main() {int n,m;cin >> n >> m;vector<int> a(n);for (int i = 0; i < n; i++) {cin >> a[i];}for (int i = 2; i < n; i++) {if (a[i] == a[i-2]) {puts("No");return 0;}}DAG g(m);for (int i = 1; i < n; i++) {if (i % 2 == 1) {g.add_edge(a[i-1]-1,a[i]-1);}else {g.add_edge(a[i]-1,a[i-1]-1);}}auto p = g.topological_sort();if (p.size() == 0) {puts("No");}else {puts("Yes");vector<int> ans(m);for (int i = 0; i < p.size(); i++) {ans[p[i]] = i+1;}for (int i = 0; i < ans.size(); i++) {cout << ans[i] << " ";}cout << endl;}return 0;}