結果

問題 No.1612 I hate Construct a Palindrome
ユーザー 👑 potato167potato167
提出日時 2022-02-09 22:59:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,981 bytes
コンパイル時間 2,727 ms
コンパイル使用メモリ 228,516 KB
実行使用メモリ 297,800 KB
最終ジャッジ日時 2023-09-07 09:01:15
合計ジャッジ時間 26,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 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);
    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";
}
0