結果
| 問題 |
No.1612 I hate Construct a Palindrome
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-22 05:22:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 404 ms / 2,000 ms |
| コード長 | 2,425 bytes |
| コンパイル時間 | 971 ms |
| コンパイル使用メモリ | 86,336 KB |
| 実行使用メモリ | 86,792 KB |
| 最終ジャッジ日時 | 2024-07-17 21:11:18 |
| 合計ジャッジ時間 | 6,031 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
コンパイルメッセージ
main.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main()
| ^~~~
ソースコード
#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;
}