結果

問題 No.1370 置換門松列
ユーザー Example0911Example0911
提出日時 2021-01-31 12:40:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,207 bytes
コンパイル時間 2,157 ms
コンパイル使用メモリ 201,852 KB
最終ジャッジ日時 2025-01-18 10:16:26
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 998244353;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N, M; cin >> N >> M;
	bool ok = true;
	vector<int>a(N); for (int i = 0; i < N; i++)cin >> a[i];
	for (int i = 0; i < N; i++)a[i]--;
	for (int i = 0; i < N - 2; i++) {
		if (a[i] == a[i + 2]) {
			ok = false;
		}
	}
	vector<vector<int>>G(M);
	for (int i = 0; i < N - 1; i++) {
		if (i % 2 == 0) {
			G[a[i]].push_back(a[i + 1]);
		}
		else {
			G[a[i + 1]].push_back(a[i]);
		}
	}	
	vector<int>ind(M);
	for (int i = 0; i < M; i++) {
		for (auto j : G[i])ind[j]++;
	}
	queue<int>q;
	for (int i = 0; i < M; i++) {
		if (ind[i] == 0)q.push(i);
	}
	int cnt = 0;
	vector<int>ans(M, -1);
	while (!q.empty()) {
		int now = q.front(); q.pop();
		cnt++;
		ans[now] = cnt;
		for (auto nv : G[now]) {
			ind[nv]--;
			if (ind[nv] == 0)q.push(nv);
		}
	}
	for (int i = 0; i < M; i++) {
		if (ans[i] == -1)ok = false;
	}
	if (ok) {
		cout << "Yes" << endl;
		for (int i = 0; i < M; i++) {
			cout << ans[i];
			if (i == M - 1)cout << endl;
			else cout << " ";
		}
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}
0