結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー elphe
提出日時 2025-08-22 16:16:01
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,875 bytes
コンパイル時間 2,871 ms
コンパイル使用メモリ 289,524 KB
実行使用メモリ 18,528 KB
最終ジャッジ日時 2025-11-01 09:26:15
合計ジャッジ時間 6,275 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

[[nodiscard]] static inline constexpr std::vector<std::vector<uint_least32_t>> prepare_graph(const uint_fast32_t N, [[maybe_unused]] const uint_fast32_t M, const std::vector<std::pair<uint_least32_t, uint_least32_t>>& edges) noexcept
{
	std::vector<std::vector<uint_least32_t>> next_of(N + 1);
	for (const auto& [u, v] : edges)
		next_of[u].push_back(v), next_of[v].push_back(u);

	return next_of;
}

template<uint_fast32_t MAX_K>
[[nodiscard]] static inline constexpr std::array<std::vector<uint_least32_t>, MAX_K + 1> prepare_telescope([[maybe_unused]] const uint_fast32_t L, const std::vector<std::pair<uint_least32_t, uint_least32_t>>& telescopes) noexcept
{
	std::array<std::vector<uint_least32_t>, MAX_K + 1> center_of;
	for (const auto& [J, K] : telescopes)
		center_of[MAX_K - K].push_back(J);

	return center_of;
}

template<uint_fast32_t MAX_K>
[[nodiscard]] static inline std::vector<uint_least32_t> calc_safety(const uint_fast32_t N, const std::vector<std::vector<uint_least32_t>>& next_of, const std::array<std::vector<uint_least32_t>, MAX_K + 1>& center_of) noexcept
{
	std::vector<uint_least32_t> safety_of(N + 1, UINT_LEAST32_MAX);
	std::queue<uint_least32_t> q;
	for (uint_fast32_t i = 0; i <= MAX_K; ++i)
	{
		while (!q.empty() && safety_of[q.front()] < i)
		{
			for (const auto& next : next_of[q.front()])
				if (safety_of[next] == UINT_LEAST32_MAX)
					safety_of[next] = i, q.push(next);
			q.pop();
		}

		for (const auto& center : center_of[i])
			if (safety_of[center] == UINT_LEAST32_MAX)
				safety_of[center] = i, q.push(center);
	}

	return safety_of;
}

[[nodiscard]] static inline uint_least32_t solve(const uint_fast32_t N, const std::vector<std::vector<uint_least32_t>>& next_of, const std::vector<uint_least32_t>& safety_of) noexcept
{
	std::vector<uint_least32_t> dist(N + 1, UINT_LEAST32_MAX);
	std::queue<uint_least32_t> q;
	if (safety_of[1] == UINT_LEAST32_MAX)
		dist[1] = 0, q.push(1);

	while (!q.empty())
	{
		for (const auto& next : next_of[q.front()])
			if (safety_of[next] == UINT_LEAST32_MAX && dist[next] == UINT_LEAST32_MAX)
				dist[next] = dist[q.front()] + 1, q.push(next);
		q.pop();
	}

	return dist[N];
}

static inline void output(const uint_least32_t ans)
{
	if (ans == UINT_LEAST32_MAX) std::cout << "No\n";
	else std::cout << "Yes\n" << ans << '\n';
}

int main()
{
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	uint_fast32_t N, M, L;
	std::cin >> N >> M;
	std::vector<std::pair<uint_least32_t, uint_least32_t>> edges(M);
	for (auto& [u, v] : edges) std::cin >> u >> v;

	std::cin >> L;
	std::vector<std::pair<uint_least32_t, uint_least32_t>> telescopes(L);
	for (auto& [J, K] : telescopes) std::cin >> J >> K;

	const auto& next_of = prepare_graph(N, M, edges);
	output(solve(N, next_of, calc_safety<1000>(N, next_of, prepare_telescope<1000>(L, telescopes))));
	return 0;
}
0