結果

問題 No.1612 I hate Construct a Palindrome
ユーザー nok0nok0
提出日時 2021-06-17 23:43:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 364 ms / 2,000 ms
コード長 2,373 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 216,456 KB
実行使用メモリ 14,912 KB
最終ジャッジ日時 2023-09-24 14:30:10
合計ジャッジ時間 8,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 364 ms
12,684 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 362 ms
12,636 KB
testcase_06 AC 151 ms
11,740 KB
testcase_07 AC 150 ms
11,584 KB
testcase_08 AC 138 ms
11,548 KB
testcase_09 AC 95 ms
11,212 KB
testcase_10 AC 98 ms
11,060 KB
testcase_11 AC 96 ms
11,104 KB
testcase_12 AC 107 ms
11,332 KB
testcase_13 AC 106 ms
11,432 KB
testcase_14 AC 106 ms
11,504 KB
testcase_15 AC 193 ms
14,832 KB
testcase_16 AC 199 ms
14,828 KB
testcase_17 AC 196 ms
14,912 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, x) for(int i = 0; i < (x); i++)
constexpr int inf = 1 << 30;
using T = tuple<int, int, int>;

int bit;
int main() {
	int n, m;
	cin >> n >> m;
	vector<T> edges;
	vector<vector<pair<int, int>>> g(n);

	auto is_palindrome = [](const string &s) {
		rep(i, s.size()) {
			if(s[i] != s[s.size() - 1 - i]) return false;
		}
		return true;
	};

	auto bfs = [&](int s) {
		vector dist(n, inf);
		dist[s] = 0;
		queue<int> que;
		que.push(s);
		while(que.size()) {
			int now = que.front();
			que.pop();
			for(auto [to, ignored] : g[now]) {
				if(dist[to] <= dist[now] + 1) continue;
				dist[to] = dist[now] + 1;
				que.push(to);
			}
		}
		return dist;
	};

	auto out = [](vector<int> &res) {
		reverse(res.begin(), res.end());
		cout << res.size() << endl;
		for(auto &v : res) cout << v + 1 << endl;
		exit(0);
	};

	rep(i, m) {
		int a, b;
		char c;
		cin >> a >> b >> c;
		--a, --b;
		g[a].emplace_back(b, i);
		g[b].emplace_back(a, i);
		edges.push_back({a, b, c - 'a'});
		bit |= 1 << (c - 'a');
	}
	if(__builtin_popcount(bit) == 1) {
		puts("-1");
		return 0;
	}

	auto dist = bfs(0);
	vector<int> res;
	string tmp = "";
	{
		int now = n - 1;
		while(now) {
			for(auto [to, id] : g[now]) {
				if(dist[to] + 1 == dist[now]) {
					now = to;
					tmp.push_back(get<2>(edges[id]));
					res.push_back(id);
					break;
				}
			}
		}
	}
	if(not is_palindrome(tmp)) {
		out(res);
	}
	if(count(tmp.begin(), tmp.end(), tmp.front()) != tmp.size()) {
		rep(i, 2) res.push_back(res.back());
		out(res);
	}
	rep(i, m) {
		auto [u, v, e] = edges[i];
		if(e == tmp.front()) continue;
		res.clear();
		tmp.clear();
		if(dist[u] > dist[v]) swap(u, v);
		//0からu u->v v->u uからn
		auto d2 = bfs(u);
		{
			int now = n - 1;
			while(now != u) {
				for(auto [to, id] : g[now]) {
					if(d2[to] + 1 == d2[now]) {
						now = to;
						tmp.push_back(get<2>(edges[id]));
						res.push_back(id);
						break;
					}
				}
			}
		}
		rep(j, 2) {
			tmp.push_back((char)('a' + e));
			res.push_back(i);
		}
		{
			int now = u;
			while(now) {
				for(auto [to, id] : g[now]) {
					if(dist[to] + 1 == dist[now]) {
						now = to;
						tmp.push_back(get<2>(edges[id]));
						res.push_back(id);
						break;
					}
				}
			}
		}
		break;
	}
	if(is_palindrome(tmp)) {
		rep(i, 2) res.push_back(res.back());
	}
	out(res);
}
0