結果

問題 No.1612 I hate Construct a Palindrome
ユーザー 👑 potato167potato167
提出日時 2022-02-09 23:10:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,884 ms / 2,000 ms
コード長 3,164 bytes
コンパイル時間 3,570 ms
コンパイル使用メモリ 227,908 KB
実行使用メモリ 304,028 KB
最終ジャッジ日時 2023-09-07 09:14:25
合計ジャッジ時間 22,405 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 7 ms
5,192 KB
testcase_02 AC 827 ms
212,360 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 7 ms
5,220 KB
testcase_05 AC 813 ms
212,536 KB
testcase_06 AC 841 ms
226,476 KB
testcase_07 AC 869 ms
226,604 KB
testcase_08 AC 877 ms
226,784 KB
testcase_09 AC 873 ms
229,680 KB
testcase_10 AC 892 ms
229,644 KB
testcase_11 AC 855 ms
229,640 KB
testcase_12 AC 977 ms
234,728 KB
testcase_13 AC 1,002 ms
234,712 KB
testcase_14 AC 957 ms
234,624 KB
testcase_15 AC 1,884 ms
303,836 KB
testcase_16 AC 1,869 ms
304,028 KB
testcase_17 AC 1,882 ms
303,884 KB
testcase_18 AC 7 ms
5,436 KB
testcase_19 AC 6 ms
5,460 KB
testcase_20 AC 7 ms
5,420 KB
testcase_21 AC 8 ms
5,580 KB
testcase_22 AC 7 ms
5,672 KB
testcase_23 AC 7 ms
5,580 KB
testcase_24 AC 11 ms
6,304 KB
testcase_25 AC 10 ms
6,300 KB
testcase_26 AC 10 ms
6,172 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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,-1);
    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};
		G[a*L+26].push_back({b*L+26,1});
		G[b*L+26].push_back({a*L+26,1});
		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);
	/*rep(i,N){
		rep(j,L){
			if(s[i*L+j]!=INF) cout<<s[i*L+j]<<" ";
			else cout<<"* ";
		}
		cout<<"\n";
	}*/
	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";
}
0