結果

問題 No.1370 置換門松列
ユーザー chocoruskchocorusk
提出日時 2021-02-06 22:30:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 139,592 KB
実行使用メモリ 16,368 KB
最終ジャッジ日時 2023-10-12 21:06:15
合計ジャッジ時間 3,780 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 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,352 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 1 ms
4,356 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 1 ms
4,368 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 58 ms
16,224 KB
testcase_22 AC 61 ms
15,984 KB
testcase_23 AC 49 ms
13,472 KB
testcase_24 AC 19 ms
5,152 KB
testcase_25 AC 77 ms
16,368 KB
testcase_26 AC 77 ms
16,316 KB
testcase_27 AC 43 ms
13,900 KB
testcase_28 AC 42 ms
13,628 KB
testcase_29 AC 26 ms
5,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct SCC{
    vector<vector<int>> g, gr;
    int n, k;
    vector<int> cmp, vs;
    vector<bool> used;
    void dfs(int x){
        used[x]=1;
        for(auto y:g[x]){
            if(!used[y]) dfs(y);
        }
        vs.push_back(x);
    }
    void rdfs(int v, int k){
        used[v]=1;
        cmp[v]=k;
        for(auto y:gr[v]){
            if(!used[y]) rdfs(y, k);
        }
    }
    SCC(const vector<vector<int>> &g):g(g), n(g.size()), cmp(n), used(n){
        gr.resize(n);
        for(int x=0; x<n; x++) for(auto y:g[x]) gr[y].push_back(x);
        for(int i=0; i<n; i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        k=0;
        for(int i=vs.size()-1; i>=0; i--){
            if(!used[vs[i]]) rdfs(vs[i], k++);
        }
    }
};
int a[100010];

int main()
{
    int n, m; cin>>n>>m;
    for(int i=0; i<n; i++){
        cin>>a[i]; a[i]--;
    }
    for(int i=0; i<n-2; i++){
        if(a[i]==a[i+2]){
            cout<<"No"<<endl;
            return 0;
        }
    }
    for(int i=0; i<n-1; i++){
        if(a[i]==a[i+1]){
            cout<<"No"<<endl;
            return 0;
        }
    }
    {
        vector<vector<int>> g(m);
        for(int i=0; i<n-1; i++){
            if(i&1){
                g[a[i+1]].push_back(a[i]);
            }else{
                g[a[i]].push_back(a[i+1]);
            }
        }
        SCC scc(g);
        if(scc.k==m){
            cout<<"Yes"<<endl;
            for(int i=0; i<m; i++){
                cout<<scc.cmp[i]+1;
                if(i<m-1) cout<<" ";
            }
            cout<<endl;
            return 0;
        }
    }
    {
        vector<vector<int>> g(m);
        for(int i=0; i<n-1; i++){
            if(!(i&1)){
                g[a[i+1]].push_back(a[i]);
            }else{
                g[a[i]].push_back(a[i+1]);
            }
        }
        SCC scc(g);
        if(scc.k==m){
            cout<<"Yes"<<endl;
            for(int i=0; i<m; i++){
                cout<<scc.cmp[i]+1;
                if(i<m-1) cout<<" ";
            }
            cout<<endl;
            return 0;
        }
    }
    cout<<"No"<<endl;
    return 0;
}
0