結果
| 問題 |
No.1612 I hate Construct a Palindrome
|
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2021-07-22 19:33:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 120 ms / 2,000 ms |
| コード長 | 3,970 bytes |
| コンパイル時間 | 1,957 ms |
| コンパイル使用メモリ | 183,312 KB |
| 実行使用メモリ | 19,716 KB |
| 最終ジャッジ日時 | 2024-07-17 21:17:56 |
| 合計ジャッジ時間 | 5,716 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:152:12: warning: 's' may be used uninitialized [-Wmaybe-uninitialized]
152 | bfs(s, c0, c1, n - 1);
| ~~~^~~~~~~~~~~~~~~~~~
main.cpp:131:13: note: 's' was declared here
131 | int s;
| ^
main.cpp:152:12: warning: 'c1' may be used uninitialized [-Wmaybe-uninitialized]
152 | bfs(s, c0, c1, n - 1);
| ~~~^~~~~~~~~~~~~~~~~~
main.cpp:142:17: note: 'c1' was declared here
142 | int c0, c1;
| ^~
main.cpp:152:12: warning: 'c0' may be used uninitialized [-Wmaybe-uninitialized]
152 | bfs(s, c0, c1, n - 1);
| ~~~^~~~~~~~~~~~~~~~~~
main.cpp:142:13: note: 'c0' was declared here
142 | int c0, c1;
| ^~
main.cpp:144:21: warning: 'fc' may be used uninitialized [-Wmaybe-uninitialized]
144 | if (fc != e[i].second) {
| ^~
main.cpp:132:14: note: 'fc' was declared here
132 | char fc;
| ^~
ソースコード
#include "bits/stdc++.h"
//#include "atcoder/all"
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
//const bool debug = false;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
int popcount(long long n) {
n = (n & 0x5555555555555555) + (n >> 1 & 0x5555555555555555);
n = (n & 0x3333333333333333) + (n >> 2 & 0x3333333333333333);
n = (n & 0x0f0f0f0f0f0f0f0f) + (n >> 4 & 0x0f0f0f0f0f0f0f0f);
n = (n & 0x00ff00ff00ff00ff) + (n >> 8 & 0x00ff00ff00ff00ff);
n = (n & 0x0000ffff0000ffff) + (n >> 16 & 0x0000ffff0000ffff);
n = (n & 0x00000000ffffffff) + (n >> 32 & 0x00000000ffffffff);
return n;
}
struct Edge {
int to, id, c;
Edge(int _to, int _id, char _c) :to(_to), id(_id), c(_c) {}
};
vector<Edge>g[200005];
vector<P>ans;
void bfs2(int s, int e) {
int sz = ans.size();
//cout << s << e << endl;
vector<int>d(100005, INF);
queue<int> q;
q.emplace(s);
d[s] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
for (auto e : g[p]) {
int np = e.to;
if (d[np] > (d[p] + 1)) {
d[np] = d[p] + 1;
q.emplace(np);
}
}
}
//後半復元。
//復元パート。
int np = e;
int nd = d[e];
rrep(i, d[e]) {
for (auto e : g[np]) {
if (d[np] - 1 == d[e.to]) {
ans.push_back({ e.id,e.c });
np = e.to;
nd = d[e.to];
break;
}
}
}
rep(i, d[e]) {
if (sz + i < ans.size() - 1 - i) {
swap(ans[sz + i], ans[ans.size() - 1 - i]);
}
}
}
void bfs(int s, int c0, int c1, int e) {
vector<int>d(100005, INF);
queue<int> q;
q.emplace(s);
d[s] = 0;
while (!q.empty()) {
int p = q.front();
q.pop();
for (auto e : g[p]) {
int np = e.to;
if (d[np] > (d[p] + 1)) {
d[np] = d[p] + 1;
q.emplace(np);
}
}
}
if (d[c0] > d[c1])swap(c0, c1);
//復元パート。
int np = c0;
int nd = d[c0];
rrep(i, d[c0]) {
for (auto e : g[np]) {
if (d[np] - 1 == d[e.to]) {
ans.push_back({ e.id,e.c });
np = e.to;
nd = d[e.to];
break;
}
}
}
rep(i, ans.size() - 1) {
if (1 + i < ans.size() - i - 1) {
swap(ans[i + 1], ans[ans.size() - i - 1]);
}
}
bfs2(c1, e);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
int bit = 0;
vector<pair<pair<int, int>, int>>e(m);
rep(i, m) {
int a, b; char c; cin >> a >> b >> c;
a--; b--;
g[a].emplace_back(b, i, c - 'a');
g[b].emplace_back(a, i, c - 'a');
bit |= 1 << (c - 'a');
e[i] = { {a,b},c - 'a' };
}
if (popcount(bit) == 1) {
cout << -1 << endl;
return 0;
}
vector<int>a(n);
int s;
char fc;
rep(i, m) {
if ((0 == e[i].first.first) || (0 == e[i].first.second)) {
fc = e[i].second;
if (0 != e[i].first.first)s = e[i].first.first;
else s = e[i].first.second;
ans.push_back({ i,e[i].second });
break;
}
}
int c0, c1;
rep(i, m) {
if (fc != e[i].second) {
c0 = e[i].first.first;
c1 = e[i].first.second;
ans.push_back({ i,e[i].second });
break;
}
}
//cout << s << c0 << c1 << endl;
bfs(s, c0, c1, n - 1);
//回分なら追加
bool b = true;
rep(i, ans.size()) {
if (ans[i].second != ans[ans.size() - 1 - i].second) {
b = false;
break;
}
}
if (b) {
int sz = ans.size() - 1;
ans.push_back(ans[sz]);
ans.push_back(ans[sz]);
}
cout << ans.size() << endl;
rep(i, ans.size()) {
cout << ans[i].first + 1 << endl;
}
return 0;
}
kwm_t