結果

問題 No.1612 I hate Construct a Palindrome
ユーザー nok0nok0
提出日時 2021-06-18 00:07:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,006 bytes
コンパイル時間 2,382 ms
コンパイル使用メモリ 219,796 KB
実行使用メモリ 14,724 KB
最終ジャッジ日時 2023-09-24 14:30:19
合計ジャッジ時間 8,258 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 378 ms
11,824 KB
testcase_06 AC 152 ms
11,376 KB
testcase_07 AC 161 ms
11,064 KB
testcase_08 AC 145 ms
11,076 KB
testcase_09 AC 105 ms
10,908 KB
testcase_10 AC 104 ms
10,972 KB
testcase_11 AC 102 ms
10,676 KB
testcase_12 AC 115 ms
11,012 KB
testcase_13 AC 117 ms
11,000 KB
testcase_14 AC 115 ms
11,068 KB
testcase_15 AC 204 ms
14,624 KB
testcase_16 AC 215 ms
14,724 KB
testcase_17 AC 210 ms
14,552 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,380 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,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:95:22: 警告: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
   95 |         tmp = restore(x, n - 1);
      |               ~~~~~~~^~~~~~~~~~
main.cpp:73:20: 備考: ‘x’ はここで定義されています
   73 |         int s = 0, x, y, xy_id;
      |                    ^
main.cpp:86:25: 警告: ‘sc’ may be used uninitialized [-Wmaybe-uninitialized]
   86 |                 if(c != sc) {
      |                         ^~
main.cpp:74:14: 備考: ‘sc’ はここで定義されています
   74 |         char sc;
      |              ^~

ソースコード

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 restore = [&](int s, int t) {
		vector<int> res;
		auto dist = bfs(s);
		int now = t;
		while(now != s) {
			for(auto [to, id] : g[now]) {
				if(dist[to] + 1 == dist[now]) {
					now = to;
					res.push_back(id);
					break;
				}
			}
		}
		reverse(res.begin(), res.end());
		return res;
	};

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

	//方針
	//0-s s-x x-y y-x x-(n-1)
	vector<int> res;
	int s = 0, x, y, xy_id;
	char sc;
	rep(i, m) {
		auto [u, v, ig] = edges[i];
		if(!u or !v) {
			s = max(u, v);
			res.push_back(i);
			sc = get<2>(edges[i]);
			break;
		}
	}
	rep(i, m) {
		auto [u, v, c] = edges[i];
		if(c != sc) {
			x = u, y = v, xy_id = i;
			break;
		}
	}

	auto tmp = restore(s, x);
	res.insert(res.end(), tmp.begin(), tmp.end());
	rep(i, 2) res.push_back(xy_id);
	tmp = restore(x, n - 1);
	res.insert(res.end(), tmp.begin(), tmp.end());

	string str = "";
	for(auto v : res) str.push_back(get<2>(edges[v]));
	if(is_palindrome(str)) rep(i, 2) res.push_back(res.back());

	cout << res.size() << endl;
	for(auto &v : res) cout << v + 1 << endl;

	return 0;
}
0