結果

問題 No.1612 I hate Construct a Palindrome
ユーザー milanis48663220milanis48663220
提出日時 2021-07-21 23:11:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 3,189 bytes
コンパイル時間 1,499 ms
コンパイル使用メモリ 119,320 KB
実行使用メモリ 38,916 KB
最終ジャッジ日時 2023-09-24 19:39:45
合計ジャッジ時間 8,483 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 362 ms
34,088 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 355 ms
34,148 KB
testcase_06 AC 214 ms
33,024 KB
testcase_07 AC 219 ms
33,000 KB
testcase_08 AC 191 ms
32,968 KB
testcase_09 AC 243 ms
33,284 KB
testcase_10 AC 243 ms
33,304 KB
testcase_11 AC 232 ms
33,208 KB
testcase_12 AC 268 ms
33,988 KB
testcase_13 AC 273 ms
33,932 KB
testcase_14 AC 267 ms
33,996 KB
testcase_15 AC 325 ms
38,736 KB
testcase_16 AC 322 ms
38,916 KB
testcase_17 AC 327 ms
38,800 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;
using T = tuple<int, int, int>;

struct edge{
    int to, idx, c;
};

const int INF = 1e9;

bool is_palindrome(vector<int> v){
    int n = v.size();
    for(int i = 0; i < n; i++){
        if(v[i] != v[n-i-1]) return false;
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<vector<edge>> g(n);
    set<int> st;
    vector<int> a(m), b(m), c(m);
    for(int i = 0; i < m; i++){
        char d; cin >> a[i] >> b[i] >> d;
        a[i]--; b[i]--;
        c[i] = d-'a';
        st.insert(c[i]);
    }
    if(st.size() == 1){
        cout << -1 << endl;
        return 0;
    }
    char c1 = *st.begin();
    for(int i = 0; i < m; i++){
        if(c[i] == c1) {
            g[a[i]].push_back(edge{b[i], i, 0});
            g[b[i]].push_back(edge{a[i], i, 0});
        }else{
            g[a[i]].push_back(edge{b[i], i, 1});
            g[b[i]].push_back(edge{a[i], i, 1});
        }
    }

    vector<vector<int>> dist(n, vector<int>(4, INF));
    vector<vector<int>> preve(n, vector<int>(4, INF));
    vector<vector<int>> prevv(n, vector<int>(4, INF));
    vector<vector<int>> prevs(n, vector<int>(4, INF));
    dist[0][0] = 0;
    priority_queue<T, vector<T>, greater<T>> que;
    que.push(T(0, 0, 0));
    while(!que.empty()){
        auto [d, v, s] = que.top(); que.pop();
        if(d != dist[v][s]) continue;
        for(edge e: g[v]){
            int nx_s = s|(1<<e.c);
            if(dist[e.to][nx_s] > d+1){
                dist[e.to][nx_s] = d+1;
                preve[e.to][nx_s] = e.idx;
                prevv[e.to][nx_s] = v;
                prevs[e.to][nx_s] = s;
                que.push(T(dist[e.to][nx_s], e.to, nx_s));
            }
        }
    }
    assert(dist[n-1][3] != INF);
    vector<int> path;
    vector<int> str;
    int cur_v = n-1;
    int cur_s = 3;

    while(cur_v != 0 || cur_s != 0){
        int idx = preve[cur_v][cur_s];
        path.push_back(idx);
        str.push_back(c[idx]);
        int vv = prevv[cur_v][cur_s];
        int ss = prevs[cur_v][cur_s];
        cur_v = vv;
        cur_s = ss;
    }
    reverse(path.begin(), path.end());
    reverse(str.begin(), str.end());
    // cout << is_palindrome({1}) << endl;
    // cout << is_palindrome({1, 0}) << endl;
    
    if(is_palindrome(str)){
        cout << path.size()+2 << endl;
        for(int idx: path) cout << idx+1 << endl;
        cout << path.back()+1 << endl;
        cout << path.back()+1 << endl;
    }else{
        cout << path.size() << endl;
        for(int idx: path) cout << idx+1 << endl;
    }
}
0