結果
問題 | No.1612 I hate Construct a Palindrome |
ユーザー |
![]() |
提出日時 | 2021-07-22 14:41:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 196 ms / 2,000 ms |
コード長 | 2,801 bytes |
コンパイル時間 | 1,929 ms |
コンパイル使用メモリ | 184,772 KB |
実行使用メモリ | 22,016 KB |
最終ジャッジ日時 | 2024-07-17 21:15:47 |
合計ジャッジ時間 | 5,400 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' //#define vec vector<ll> //#define mat vector<vector<ll> > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pll; //typedef long double ld; typedef complex<double> Complex; const ll INF=1e9+7; const ll MOD=998244353; const ll inf=INF*INF; const ll mod=INF; const ll MAX=1000010; const double PI=acos(-1.0); typedef vector<vector<ll> > mat; typedef vector<ll> vec; signed main(){ ll n,m;cin>>n>>m; vector<vector<pair<ll,pair<ll,char> > > >G(n); ll u=-1,v=-1; char cc='.'; ll ccc=0; rep(i,m){ ll a,b;char c; cin>>a>>b>>c;a--,b--; G[a].pb(mp(b,mp(i+1,c))); G[b].pb(mp(a,mp(i+1,c))); } rep(i,n){ for(auto e:G[i]){ if(e.se.se!=G[0][0].se.se){ u=i,v=e.fi;cc=e.se.se; ccc=e.se.fi; } } } if(u==-1){ cout<<v<<endl; return 0; } vector<ll>d(n,inf); d[0]=0; queue<ll>q; vector<ll>p(0); q.push(0); while(!q.empty()){ ll k=q.front(); q.pop(); for(auto e:G[k]){ if(d[e.fi]<inf){ continue; } d[e.fi]=d[k]+1; q.push(e.fi); } } string pre=""; while(d[u]>0){ ll nxt=-1; char c='.'; for(auto e:G[u]){ if(d[e.fi]==d[u]-1){ nxt=e.fi; c=e.se.se; p.pb(e.se.fi); break; } } pre+=c; u=nxt; } vector<ll>dd(n,inf); dd[n-1]=0; q.push(n-1); while(!q.empty()){ ll k=q.front(); q.pop(); for(auto e:G[k]){ if(dd[e.fi]<inf){ continue; } dd[e.fi]=dd[k]+1; q.push(e.fi); } } reverse(all(p)); reverse(all(pre)); p.pb(ccc); string sur=""; while(dd[v]>0){ ll nxt=-1; char c='.'; for(auto e:G[v]){ if(dd[e.fi]==dd[v]-1){ nxt=e.fi; c=e.se.se; p.pb(e.se.fi); break; } } sur+=c; v=nxt; } string ans=pre+cc+sur; string ans2=ans; reverse(all(ans)); reverse(all(p)); if(ans2==ans){ p.pb(G[0][0].se.fi); p.pb(G[0][0].se.fi); } reverse(all(p)); cout<<p.size()<<endl; rep(i,p.size()){ cout<<p[i]<<endl; } }