結果

問題 No.1612 I hate Construct a Palindrome
ユーザー kwm_tkwm_t
提出日時 2021-07-22 19:33:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 3,970 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 181,388 KB
実行使用メモリ 19,588 KB
最終ジャッジ日時 2023-09-24 20:40:29
合計ジャッジ時間 6,153 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,596 KB
testcase_01 AC 6 ms
8,480 KB
testcase_02 AC 74 ms
15,160 KB
testcase_03 AC 5 ms
8,552 KB
testcase_04 AC 6 ms
8,552 KB
testcase_05 AC 79 ms
15,208 KB
testcase_06 AC 62 ms
14,820 KB
testcase_07 AC 69 ms
14,752 KB
testcase_08 AC 64 ms
14,444 KB
testcase_09 AC 66 ms
14,264 KB
testcase_10 AC 59 ms
14,252 KB
testcase_11 AC 53 ms
14,288 KB
testcase_12 AC 68 ms
14,984 KB
testcase_13 AC 70 ms
14,860 KB
testcase_14 AC 68 ms
14,872 KB
testcase_15 AC 111 ms
19,588 KB
testcase_16 AC 103 ms
19,520 KB
testcase_17 AC 111 ms
19,568 KB
testcase_18 AC 6 ms
8,492 KB
testcase_19 AC 6 ms
8,444 KB
testcase_20 AC 5 ms
8,552 KB
testcase_21 AC 5 ms
8,496 KB
testcase_22 AC 5 ms
8,544 KB
testcase_23 AC 6 ms
8,496 KB
testcase_24 AC 6 ms
8,496 KB
testcase_25 AC 6 ms
8,452 KB
testcase_26 AC 6 ms
8,492 KB
testcase_27 AC 5 ms
8,508 KB
testcase_28 AC 5 ms
8,540 KB
testcase_29 AC 5 ms
8,092 KB
testcase_30 AC 5 ms
8,524 KB
testcase_31 AC 5 ms
8,444 KB
testcase_32 AC 6 ms
8,556 KB
testcase_33 AC 5 ms
8,532 KB
testcase_34 AC 6 ms
8,596 KB
testcase_35 AC 5 ms
8,548 KB
testcase_36 AC 4 ms
8,132 KB
testcase_37 AC 6 ms
8,616 KB
testcase_38 AC 6 ms
8,548 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:152:12: 警告: ‘s’ may be used uninitialized [-Wmaybe-uninitialized]
  152 |         bfs(s, c0, c1, n - 1);
      |         ~~~^~~~~~~~~~~~~~~~~~
main.cpp:131:13: 備考: ‘s’ はここで定義されています
  131 |         int s;
      |             ^
main.cpp:152:12: 警告: ‘c1’ may be used uninitialized [-Wmaybe-uninitialized]
  152 |         bfs(s, c0, c1, n - 1);
      |         ~~~^~~~~~~~~~~~~~~~~~
main.cpp:142:17: 備考: ‘c1’ はここで定義されています
  142 |         int c0, c1;
      |                 ^~
main.cpp:152:12: 警告: ‘c0’ may be used uninitialized [-Wmaybe-uninitialized]
  152 |         bfs(s, c0, c1, n - 1);
      |         ~~~^~~~~~~~~~~~~~~~~~
main.cpp:142:13: 備考: ‘c0’ はここで定義されています
  142 |         int c0, c1;
      |             ^~
main.cpp:144:21: 警告: ‘fc’ may be used uninitialized [-Wmaybe-uninitialized]
  144 |                 if (fc != e[i].second) {
      |                     ^~
main.cpp:132:14: 備考: ‘fc’ はここで定義されています
  132 |         char fc;
      |              ^~

ソースコード

diff #

#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;
}
0