結果

問題 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  
実行時間 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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,668 KB
testcase_01 AC 5 ms
8,624 KB
testcase_02 AC 89 ms
15,376 KB
testcase_03 AC 4 ms
8,688 KB
testcase_04 AC 5 ms
8,764 KB
testcase_05 AC 84 ms
15,376 KB
testcase_06 AC 63 ms
14,920 KB
testcase_07 AC 65 ms
14,860 KB
testcase_08 AC 65 ms
14,564 KB
testcase_09 AC 63 ms
14,456 KB
testcase_10 AC 62 ms
14,512 KB
testcase_11 AC 58 ms
14,384 KB
testcase_12 AC 65 ms
14,852 KB
testcase_13 AC 67 ms
14,900 KB
testcase_14 AC 64 ms
14,992 KB
testcase_15 AC 120 ms
19,556 KB
testcase_16 AC 118 ms
19,716 KB
testcase_17 AC 112 ms
19,584 KB
testcase_18 AC 6 ms
8,588 KB
testcase_19 AC 5 ms
8,656 KB
testcase_20 AC 5 ms
8,760 KB
testcase_21 AC 5 ms
8,776 KB
testcase_22 AC 5 ms
8,712 KB
testcase_23 AC 5 ms
8,660 KB
testcase_24 AC 6 ms
8,864 KB
testcase_25 AC 6 ms
8,640 KB
testcase_26 AC 5 ms
8,712 KB
testcase_27 AC 4 ms
8,704 KB
testcase_28 AC 5 ms
8,612 KB
testcase_29 AC 4 ms
8,180 KB
testcase_30 AC 5 ms
8,656 KB
testcase_31 AC 6 ms
8,672 KB
testcase_32 AC 5 ms
8,636 KB
testcase_33 AC 5 ms
8,616 KB
testcase_34 AC 5 ms
8,588 KB
testcase_35 AC 5 ms
8,600 KB
testcase_36 AC 5 ms
8,084 KB
testcase_37 AC 4 ms
8,648 KB
testcase_38 AC 5 ms
8,524 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
      |              ^~

ソースコード

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