結果
| 問題 |
No.1612 I hate Construct a Palindrome
|
| コンテスト | |
| ユーザー |
👑 potato167
|
| 提出日時 | 2022-02-09 22:59:51 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,981 bytes |
| コンパイル時間 | 2,951 ms |
| コンパイル使用メモリ | 222,172 KB |
| 最終ジャッジ日時 | 2025-01-27 20:56:33 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 WA * 27 TLE * 3 |
ソースコード
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
using len_type=int;
struct edge{
int to;
len_type length;
};
len_type dijkstra_movement(edge e,int from,len_type t){
return t+e.length;
}
pair<vector<int>,vector<len_type>> dijkstra(vector<vector<edge>> &G,int start,len_type inf){
int M=G.size();
assert(M>start&&start>=0);
vector<len_type> seen(M,inf);
vector<int> front(M);
seen[start]=0;
queue<pair<len_type,int>> pq;
pq.push({0,start});
while(!pq.empty()){
len_type time;
int ver;
tie(time,ver)=pq.front();
pq.pop();
if(seen[ver]<time) continue;
for(edge x:G[ver]){
len_type T=dijkstra_movement(x,ver,time);
if(T<seen[x.to]){
pq.push({T,x.to});
seen[x.to]=T;
front[x.to]=ver;
}
}
}
return make_pair(front,seen);
}
void solve();
// rainy ~ 雨に打たれて ~
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
//cin>>t;
rep(i,t) solve();
}
void solve(){
int N,M,L=28;
cin>>N>>M;
map<pair<int,int>,pair<char,int>> m;
vector<vector<edge>> G(N*L);
rep(i,M){
int a,b;
char c;
cin>>a>>b>>c;
a--,b--;
m[{a,b}]={c,i};
m[{b,a}]={c,i};
rep(j,26){
if(j!=(int)(c-'a')){
G[a*L+j].push_back({b*L+26,1});
G[b*L+j].push_back({a*L+26,1});
}else{
G[a*L+j].push_back({b*L+j,1});
G[b*L+j].push_back({a*L+j,1});
G[a*L+L-1].push_back({b*L+j,1});
G[b*L+L-1].push_back({a*L+j,1});
}
}
}
vector<int> front,s;
tie(front,s)=dijkstra(G,L-1,INF);
if(s[N*L-L+26]==INF){
cout<<"-1\n";
return;
}
string S;
int ind=N*L-L+26;
vector<int> ans;
while(ind!=L-1){
//cout<<front[ind]<<endl;
S+=m[{ind/L,front[ind]/L}].first;
ans.push_back(m[{ind/L,front[ind]/L}].second);
ind=front[ind];
}
string T=S;
reverse(ans.begin(),ans.end());
reverse(T.begin(),T.end());
if(S==T){
int x=ans[(int)(ans.size())-1];
ans.push_back(x);
ans.push_back(x);
}
cout<<ans.size()<<"\n";
rep(i,ans.size()) cout<<ans[i]+1<<"\n";
}
potato167