結果
| 問題 |
No.3200 Sinking Islands
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-07-12 11:52:13 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 88 ms / 2,000 ms |
| コード長 | 2,958 bytes |
| コンパイル時間 | 1,066 ms |
| コンパイル使用メモリ | 92,008 KB |
| 実行使用メモリ | 12,672 KB |
| 最終ジャッジ日時 | 2025-07-12 11:52:18 |
| 合計ジャッジ時間 | 5,157 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
#include <iostream>
#include <cstdint>
#include <vector>
class LiteralUnionFind
{
protected:
std::vector<uint_fast32_t> root_of, size_of;
uint_fast32_t group;
constexpr uint_fast32_t retrieve_true_root_of(const uint_fast32_t index) noexcept { return root_of[index] == index ? index : root_of[index] = retrieve_true_root_of(root_of[index]); }
public:
constexpr LiteralUnionFind(const uint_fast32_t N) : root_of(N), size_of(N, 1), group(N) { for (uint_fast32_t i = 0; i != N; ++i) root_of[i] = i; }
constexpr bool is_connected(const uint_fast32_t a, const uint_fast32_t b) noexcept { return retrieve_true_root_of(a) == retrieve_true_root_of(b); }
constexpr bool merge(const uint_fast32_t a, const uint_fast32_t b) noexcept
{
if (retrieve_true_root_of(a) != retrieve_true_root_of(b))
{
if (size_of_group_of(a) >= size_of_group_of(b))
{
size_of[root_of[a]] += size_of[root_of[b]];
root_of[b] = root_of[root_of[b]] = root_of[a];
}
else
{
size_of[root_of[b]] += size_of[root_of[a]];
root_of[a] = root_of[root_of[a]] = root_of[b];
}
--group;
return true;
}
else
return false;
}
constexpr uint_fast32_t size_of_group_of(const uint_fast32_t index) noexcept { return size_of[retrieve_true_root_of(index)]; }
constexpr uint_fast32_t groups() const noexcept { return group; }
};
static inline constexpr std::vector<uint_fast64_t> solve(const uint_fast32_t N, const uint_fast32_t M, const uint_fast32_t Q, const std::vector<uint_fast32_t>& U, const std::vector<uint_fast32_t>& V, const std::vector<uint_fast32_t>& B) noexcept
{
std::vector<bool> is_available_edge(M + 1, true);
is_available_edge[0] = false;
for (uint_fast32_t i = 0; i != Q; ++i)
is_available_edge[B[i]] = false;
uint_fast64_t count_unconnected = N * static_cast<uint_fast64_t>(N - 1) / 2;
LiteralUnionFind luf(N + 1);
for (uint_fast32_t i = 1; i <= M; ++i)
if (is_available_edge[i] && !luf.is_connected(U[i], V[i]))
count_unconnected -= static_cast<uint_fast64_t>(luf.size_of_group_of(U[i])) * luf.size_of_group_of(V[i]), luf.merge(U[i], V[i]);
std::vector<uint_fast64_t> ans(Q);
for (uint_fast32_t i = Q - 1; i != UINT_FAST32_MAX; --i)
{
ans[i] = count_unconnected;
if (!luf.is_connected(U[B[i]], V[B[i]]))
count_unconnected -= static_cast<uint_fast64_t>(luf.size_of_group_of(U[B[i]])) * luf.size_of_group_of(V[B[i]]), luf.merge(U[B[i]], V[B[i]]);
}
return ans;
}
static inline void output(const uint_fast32_t Q, const std::vector<uint_fast64_t>& ans) noexcept
{
for (uint_fast32_t i = 0; i != Q; ++i)
std::cout << ans[i] << '\n';
}
int main()
{
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
uint_fast32_t N, M, Q, i;
std::cin >> N >> M;
std::vector<uint_fast32_t> U(M + 1), V(M + 1);
for (i = 1; i <= M; ++i)
std::cin >> U[i] >> V[i];
std::cin >> Q;
std::vector<uint_fast32_t> B(Q);
for (i = 0; i != Q; ++i)
std::cin >> B[i];
output(Q, solve(N, M, Q, U, V, B));
return 0;
}