結果

問題 No.1370 置換門松列
ユーザー leaf_1415leaf_1415
提出日時 2021-01-29 22:01:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 3,769 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 92,464 KB
実行使用メモリ 11,960 KB
最終ジャッジ日時 2023-10-12 21:01:12
合計ジャッジ時間 2,599 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,476 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 28 ms
11,960 KB
testcase_22 AC 22 ms
11,536 KB
testcase_23 AC 25 ms
10,920 KB
testcase_24 AC 9 ms
4,548 KB
testcase_25 AC 34 ms
11,756 KB
testcase_26 AC 32 ms
11,752 KB
testcase_27 AC 18 ms
10,328 KB
testcase_28 AC 17 ms
10,408 KB
testcase_29 AC 11 ms
5,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;


struct SCC{
	int n;
	vector<vector<int> > G, revG, compG;
	vector<int> used, scc, topo;
	int sccid, sccnum;
	
	void tpdfs(int v)
	{
		used[v] = 1;
		for(int i = 0; i < G[v].size(); i++){
			if(!used[G[v][i]]) tpdfs(G[v][i]);
		}
		topo.push_back(v);
	}
	void sccdfs(int v, int id)
	{
		used[v] = 1;
		scc[v] = id;
		for(int i = 0; i < revG[v].size(); i++){
			if(!used[revG[v][i]]) sccdfs(revG[v][i], id);
		}
	}
	
	SCC(){}
	SCC(int n){  //V(G) = {1, 2, ..., n}, nを制約より大きくするときは注意
		this->n = n;
		G.resize(n+1);
		revG.resize(n+1);
		used.resize(n+1);
	}
	void init(){
		for(int i = 1; i <= n; i++){
			G[i].clear(), revG[i].clear();
			used[i] = 0;
		}
		topo.clear();
	}
	void add_edge(int u, int v)
	{
		G[u].push_back(v);
	}
	void tpsort()
	{
		topo.clear();
		for(int i = 1; i <= n; i++) used[i] = 0;
		for(int i = 1; i <= n; i++) if(!used[i]) tpdfs(i);
		reverse(topo.begin(), topo.end());
	}
	bool checkDAG(){ //先にtpsort()を呼ぶべし。DAGならtrueを返す
		for(int i = 1; i <= n; i++) used[i] = 0;
		for(int i = 0; i < topo.size(); i++){
			int v = topo[i];
			used[v] = 1;
			for(int j = 0; j < G[v].size(); j++){
				if(used[G[v][j]]) return false;
			}
		}
		return true;
	}
	int calcSCC(){  //先にtpsort()を呼ぶべし。戻り値はSCCの個数。SCC-IDは1-indexed
		scc.resize(n+1);
		for(int i = 1; i <= n; i++) revG[i].clear();
		for(int i = 1; i <= n; i++){
			for(int j = 0; j < G[i].size(); j++){
				revG[G[i][j]].push_back(i);
			}
		}
		sccid = 1;
		for(int i = 1; i <= n; i++) used[i] = 0;
		for(int i = 0; i < topo.size(); i++) if(!used[topo[i]]) sccdfs(topo[i], sccid++);
		return sccnum = sccid-1;
	}
	void compressSCC(bool simple = false){ //先にcalcSCC()を呼ぶべし。圧縮後のグラフはscc::compG
		compG.resize(sccnum+1);
		for(int i = 1; i <= n; i++){
			for(int j = 0; j < G[i].size(); j++){
				int u = G[i][j];
				if(scc[i] != scc[u]) compG[scc[i]].push_back(scc[u]);
			}
		}
		if(simple){
			for(int i = 1; i <= sccnum; i++){
				sort(compG[i].begin(), compG[i].end());
				compG[i].erase(unique(compG[i].begin(), compG[i].end()), compG[i].end());
			}
		}
	}
	
};

ll n, m;
ll a[100005];
ll ans[100005];

bool check(ll a, ll b, ll c)
{
	if(a == b || b == c || c == a) return false;
	if(a > b && b < c) return true;
	if(a < b && b > c) return true;
	return false;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	rep(i, 1, n) cin >> a[i];
	
	rep(i, 2, n-1){
		if(a[i-1] == a[i+1]){
			cout << "No" << endl;
			return 0;
		}
	}
	
	SCC scc(m);
	rep(i, 1, n-1){
		if(i % 2) scc.add_edge(a[i], a[i+1]);
		else scc.add_edge(a[i+1], a[i]);
	}
	scc.tpsort();
	if(scc.checkDAG()){
		rep(i, 0, m-1) ans[scc.topo[i]] = i+1;
		cout << "Yes" << endl;
		rep(i, 1, m) cout << ans[i] << " "; cout << endl;
		return 0;
	}
	
	scc.init();
	rep(i, 1, n-1){
		if(i % 2 == 0) scc.add_edge(a[i], a[i+1]);
		else scc.add_edge(a[i+1], a[i]);
	}
	scc.tpsort();
	if(scc.checkDAG()){
		rep(i, 0, m-1) ans[scc.topo[i]] = i+1;
		cout << "Yes" << endl;
		rep(i, 1, m) cout << ans[i] << " "; cout << endl;
		return 0;
	}
	
	cout << "No" << endl;
	
	return 0;
}
0