結果

問題 No.1612 I hate Construct a Palindrome
ユーザー kotatsugamekotatsugame
提出日時 2021-07-22 05:22:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 392 ms / 2,000 ms
コード長 2,425 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 86,248 KB
実行使用メモリ 80,784 KB
最終ジャッジ日時 2023-09-24 20:27:41
合計ジャッジ時間 6,179 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
18,768 KB
testcase_01 AC 9 ms
19,064 KB
testcase_02 AC 392 ms
80,304 KB
testcase_03 AC 6 ms
19,032 KB
testcase_04 AC 8 ms
18,940 KB
testcase_05 AC 370 ms
80,784 KB
testcase_06 AC 154 ms
40,712 KB
testcase_07 AC 157 ms
44,816 KB
testcase_08 AC 135 ms
38,496 KB
testcase_09 AC 101 ms
23,980 KB
testcase_10 AC 102 ms
24,260 KB
testcase_11 AC 103 ms
24,004 KB
testcase_12 AC 110 ms
24,080 KB
testcase_13 AC 108 ms
24,416 KB
testcase_14 AC 107 ms
24,548 KB
testcase_15 AC 193 ms
27,776 KB
testcase_16 AC 198 ms
28,280 KB
testcase_17 AC 200 ms
28,056 KB
testcase_18 AC 6 ms
18,864 KB
testcase_19 AC 6 ms
18,928 KB
testcase_20 AC 6 ms
18,924 KB
testcase_21 AC 6 ms
18,868 KB
testcase_22 AC 7 ms
18,736 KB
testcase_23 AC 6 ms
18,808 KB
testcase_24 AC 6 ms
18,804 KB
testcase_25 AC 6 ms
18,836 KB
testcase_26 AC 6 ms
18,768 KB
testcase_27 AC 6 ms
18,764 KB
testcase_28 AC 5 ms
18,680 KB
testcase_29 AC 5 ms
18,696 KB
testcase_30 AC 5 ms
18,688 KB
testcase_31 AC 5 ms
18,748 KB
testcase_32 AC 5 ms
18,984 KB
testcase_33 AC 5 ms
18,680 KB
testcase_34 AC 6 ms
18,948 KB
testcase_35 AC 6 ms
18,916 KB
testcase_36 AC 5 ms
18,696 KB
testcase_37 AC 5 ms
18,792 KB
testcase_38 AC 6 ms
18,756 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:12:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   12 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int N,M;
vector<pair<int,pair<int,char> > >G[1<<17];
int p1[1<<17],pN[1<<17];
int e1[1<<17],eN[1<<17];
int U1[1<<17][26],V1[1<<17][26],E1[1<<17][26];
int UN[1<<17][26],VN[1<<17][26],EN[1<<17][26];
main()
{
	cin>>N>>M;
	for(int i=1;i<=M;i++)
	{
		int u,v;char c;cin>>u>>v>>c;
		G[u].push_back(make_pair(v,make_pair(i,c)));
		G[v].push_back(make_pair(u,make_pair(i,c)));
	}
	{
		queue<int>P;
		P.push(1);
		vector<int>d(N+1,1e9);
		d[1]=0;
		p1[1]=0;
		e1[1]=0;
		while(!P.empty())
		{
			int u=P.front();
			P.pop();
			for(pair<int,pair<int,char> >e:G[u])
			{
				int v=e.first;
				char c=e.second.second;
				int nd=d[u]+1;
				U1[nd][c-'a']=u;
				V1[nd][c-'a']=v;
				E1[nd][c-'a']=e.second.first;
				if(d[v]>nd)
				{
					d[v]=nd;
					p1[v]=u;
					e1[v]=e.second.first;
					P.push(v);
				}
			}
		}
	}
	{
		queue<int>P;
		P.push(N);
		vector<int>d(N+1,1e9);
		d[N]=0;
		pN[N]=0;
		eN[N]=0;
		while(!P.empty())
		{
			int u=P.front();
			P.pop();
			for(pair<int,pair<int,char> >e:G[u])
			{
				int v=e.first;
				char c=e.second.second;
				int nd=d[u]+1;
				UN[nd][c-'a']=u;
				VN[nd][c-'a']=v;
				EN[nd][c-'a']=e.second.first;
				if(d[v]>nd)
				{
					d[v]=nd;
					pN[v]=u;
					eN[v]=e.second.first;
					P.push(v);
				}
			}
		}
	}
	for(int d=1;d<=N;d++)
	{
		for(int i=0;i<26;i++)if(U1[d][i])
		{
			for(int j=0;j<26;j++)if(i!=j&&UN[d][j])
			{
				vector<int>ans;
				{
					vector<int>D(N+1,1e9),p(N+1),E(N+1);
					D[V1[d][i]]=0;
					queue<int>P;
					P.push(V1[d][i]);
					while(!P.empty())
					{
						int u=P.front();P.pop();
						for(pair<int,pair<int,char> >e:G[u])
						{
							int v=e.first;
							int ei=e.second.first;
							if(D[v]>D[u]+1)
							{
								D[v]=D[u]+1;
								p[v]=u;
								E[v]=ei;
								P.push(v);
							}
						}
					}
					int v=VN[d][j];
					int u=V1[d][i];
					while(v!=u)
					{
						ans.push_back(E[v]);
						v=p[v];
					}
				}
				{
					ans.push_back(E1[d][i]);
					int u=U1[d][i];
					while(p1[u])
					{
						ans.push_back(e1[u]);
						u=p1[u];
					}
				}
				reverse(ans.begin(),ans.end());
				{
					ans.push_back(EN[d][j]);
					int u=UN[d][j];
					while(pN[u])
					{
						ans.push_back(eN[u]);
						u=pN[u];
					}
				}
				cout<<ans.size()<<endl;
				for(int ei:ans)cout<<ei<<endl;
				return 0;
			}
		}
	}
	cout<<-1<<endl;
}
0