結果
| 問題 |
No.1612 I hate Construct a Palindrome
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-07-21 23:11:16 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 332 ms / 2,000 ms |
| コード長 | 3,189 bytes |
| コンパイル時間 | 1,270 ms |
| コンパイル使用メモリ | 113,732 KB |
| 最終ジャッジ日時 | 2025-01-23 05:03:55 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#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;
}
}
milanis48663220