結果
| 問題 |
No.1612 I hate Construct a Palindrome
|
| コンテスト | |
| ユーザー |
riano
|
| 提出日時 | 2021-07-21 22:37:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 452 ms / 2,000 ms |
| コード長 | 2,614 bytes |
| コンパイル時間 | 2,218 ms |
| コンパイル使用メモリ | 197,304 KB |
| 実行使用メモリ | 43,268 KB |
| 最終ジャッジ日時 | 2024-07-17 19:37:23 |
| 合計ジャッジ時間 | 8,786 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:34:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
34 | auto[aa,kk,dd] = G[pt][0]; a1 = kk; d1 = dd;
| ^
main.cpp:37:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
37 | auto[a,k,d] = G[pt][i];
| ^
main.cpp:55:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
55 | auto[x,k,d] = p;
| ^
main.cpp:78:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
78 | auto[x,k,d] = p;
| ^
main.cpp:94:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
94 | auto[a,k,d] = G[1][0];
| ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/allocator.h:46,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:41,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/istream:38,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/sstream:38,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/complex:45,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ccomplex:39,
from /home
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<tuple<int,int,char>>>;
const ll mod = 1000000007;
int main() {
ll N,M;
cin >> N >> M;
Graph G(N+1);
vector<tuple<ll,ll,char>> path;
vector<set<char>> cnt(N+1);
rep(i,M){
ll a,b; char c; cin >> a >> b >> c;
G[a].push_back(make_tuple(b,i+1,c));
G[b].push_back(make_tuple(a,i+1,c));
path.push_back(make_tuple(a,b,c));
cnt[a].insert(c); cnt[b].insert(c);
}
ll pt = 0;
rep(i,N){
if(cnt[i+1].size()>1){
pt = i+1; break;
}
}
if(pt==0){
cout << -1 << endl; return 0;
}
char d1,d2; int a1,a2;
auto[aa,kk,dd] = G[pt][0]; a1 = kk; d1 = dd;
int ss = aa; int tt;
rep(i,G[pt].size()){
auto[a,k,d] = G[pt][i];
if(d!=d1){
a2 = k; tt = a; d2 = d; break;
}
}
//cout << a1 << " " << a2 << endl;
//cout << ss << " " << tt << endl;
string S; vector<ll> ans;
//BFS (普通の幅優先探索)
queue<int> go; ll dist[N+1],prev[N+1],used[N+1]; // ll par[N+1];
char cc[N+1];
rep(i,N+1){
dist[i] = 2000000000; prev[i] = 0; used[i] = 0;
}
dist[tt] = 0; go.push(tt);
while(!go.empty()){
int s = go.front(); go.pop();
for(auto p:G[s]){
auto[x,k,d] = p;
if(dist[x]<=dist[s]+1) continue;
dist[x] = dist[s] + 1;
prev[x] = s; used[x] = k; cc[x] = d;
go.push(x);
}
}
int n = N;
while(prev[n]>0){
ans.push_back(used[n]); S += cc[n];
n = prev[n];
}
S += d2; S += d1;
ans.push_back(a2); ans.push_back(a1);
rep(i,N+1){
dist[i] = 2000000000; prev[i] = 0; used[i] = 0;
}
dist[1] = 0; go.push(1);
while(!go.empty()){
int s = go.front(); go.pop();
for(auto p:G[s]){
auto[x,k,d] = p;
if(dist[x]<=dist[s]+1) continue;
dist[x] = dist[s] + 1;
prev[x] = s; used[x] = k; cc[x] = d;
go.push(x);
}
}
n = ss;
while(prev[n]>0){
ans.push_back(used[n]); S += cc[n];
n = prev[n];
}
string T = S;
reverse(S.begin(),S.end());
if(S==T){
auto[a,k,d] = G[1][0];
ans.push_back(k); ans.push_back(k);
}
reverse(ans.begin(),ans.end());
cout << ans.size() << endl;
for(ll yy:ans){
cout << yy << endl;
}
//cout << ans << endl;
}
riano