結果
問題 |
No.1370 置換門松列
|
ユーザー |
|
提出日時 | 2021-01-29 23:27:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,326 bytes |
コンパイル時間 | 3,722 ms |
コンパイル使用メモリ | 206,288 KB |
最終ジャッジ日時 | 2025-01-18 09:47:26 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 WA * 1 |
other | AC * 22 WA * 3 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < int(n); i++) using ll = long long; using P = pair<int, int>; int main() { int n, m; cin >> n >> m; vector<int> a(n); rep(i, n) { cin >> a[i]; a[i]--; } vector<vector<int>> g(m); vector<int> h(m); rep(i, n - 1) { if (i % 2 == 0) { g[a[i]].push_back(a[i + 1]); h[a[i + 1]]++; } else { g[a[i + 1]].push_back(a[i]); h[a[i]]++; } } // rep(i, m) { // cout << i << " " << h[i] << "=" << " "; // rep(j, g[i].size()) { // cout << g[i][j] << " "; // } // cout << endl; // } stack<int> st; rep(i, m) if (h[i] == 0) st.push(i); vector<P> ans; while (!st.empty()) { int now = st.top(); st.pop(); ans.push_back({now, ans.size() + 1}); for (auto ne : g[now]) { h[ne]--; if(h[ne] == 0) { st.push(ne); } } } if ((int)ans.size() != m) { cout << "No" << endl; } else { cout << "Yes" << endl; sort(ans.begin(), ans.end()); rep(i, m) { cout << ans[i].second << " "; } cout << endl; } return 0; }