結果
| 問題 | No.1612 I hate Construct a Palindrome |
| コンテスト | |
| ユーザー |
riano
|
| 提出日時 | 2021-07-21 22:37:03 |
| 言語 | C++14 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 264 ms / 2,000 ms |
| コード長 | 2,614 bytes |
| 記録 | |
| コンパイル時間 | 1,437 ms |
| コンパイル使用メモリ | 209,576 KB |
| 実行使用メモリ | 43,284 KB |
| 最終ジャッジ日時 | 2026-04-01 09:52:49 |
| 合計ジャッジ時間 | 5,540 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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/15.2.0_1/include/c++/15/string:56,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bitset:54,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:54,
from main.cpp:1:
In member function 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator+=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]',
inlined from 'int main()' at main.cpp:68:10:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/basic_string.h:1499:24: warning: 'd2' may be used uninitialized [-Wmaybe-uninitialized]
1499 | this->push_back(__c);
| ~~~~~~~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:33:13: note: 'd2' was declared here
33 | char d1,d2; int a1,a2;
| ^~
main.cpp:69:19: warning: 'a2' may be used uninitialized [-W
ソースコード
#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