結果
問題 | No.2494 Sum within Components |
ユーザー |
![]() |
提出日時 | 2023-10-06 21:46:28 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 74 ms / 2,000 ms |
コード長 | 4,899 bytes |
コンパイル時間 | 3,247 ms |
コンパイル使用メモリ | 258,096 KB |
実行使用メモリ | 18,048 KB |
最終ジャッジ日時 | 2024-07-26 15:56:50 |
合計ジャッジ時間 | 4,513 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#include <bits/stdc++.h>#include <algorithm>#include <vector>namespace nono {class UnionFind {public:UnionFind() = default;UnionFind(int size): size_(size), data_(size, -1) {}int leader(int x) {if (data_[x] < 0) {return x;} else {return data_[x] = leader(data_[x]);}}bool merge(int lhs, int rhs) {if (same(lhs, rhs)) {return false;}lhs = leader(lhs);rhs = leader(rhs);if (-data_[lhs] < -data_[rhs]) {std::swap(lhs, rhs);}data_[lhs] += data_[rhs];data_[rhs] = lhs;return true;}bool same(int lhs, int rhs) {return (leader(lhs) == leader(rhs));}int size(int x) {return -data_[leader(x)];}int size() const {return size_;}std::vector<std::vector<int>> groups() {std::vector<int> leader_buf(size_), group_size(size_);for (int i = 0; i < size_; i++) {leader_buf[i] = leader(i);group_size[leader_buf[i]]++;}std::vector<std::vector<int>> result(size_);for (int i = 0; i < size_; i++) {result[i].reserve(group_size[i]);}for (int i = 0; i < size_; i++) {result[leader_buf[i]].push_back(i);}result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }),result.end());return result;}private:int size_;std::vector<int> data_;};} // namespace nono#include <iostream>namespace nono {namespace internal {constexpr bool is_prime(unsigned long long n) {for (unsigned long long i = 2; i * i <= n; i++) {if (n % i == 0) return false;}return true;}} // namespace internaltemplate <unsigned long long MOD = 998244353>class Modint {public:constexpr Modint(unsigned long long value = 0): value_(value % MOD) {}constexpr Modint pow(long long exp) const {Modint result(1);Modint base(*this);while (exp > 0) {if (exp & 1) {result *= base;}base *= base;exp >>= 1;}return result;}constexpr Modint inv() const {return pow(MOD - 2);}void set(unsigned long long value) {if (value >= MOD) value %= MOD;value_ = value;}unsigned long long get() const {return value_;}constexpr Modint& operator+=(const Modint other) {this->value_ += other.value_;if (this->value_ >= MOD) this->value_ -= MOD;return *this;}constexpr Modint& operator-=(const Modint other) {this->value_ += MOD - other.value_;if (this->value_ >= MOD) this->value_ -= MOD;return *this;}constexpr Modint& operator*=(const Modint other) {this->value_ *= other.value_;if (this->value_ >= MOD) this->value_ %= MOD;return *this;}constexpr Modint& operator/=(const Modint other) {this->value_ *= other.inv();if (this->value_ >= MOD) this->value_ %= MOD;return *this;}constexpr friend Modint operator+(const Modint lhs, const Modint rhs) {return Modint(lhs) += rhs;}constexpr friend Modint operator-(const Modint lhs, const Modint rhs) {return Modint(lhs) -= rhs;}constexpr friend Modint operator*(const Modint lhs, const Modint rhs) {return Modint(lhs) *= rhs;}constexpr friend Modint operator/(const Modint lhs, const Modint rhs) {return Modint(lhs) /= rhs;}friend std::istream& operator>>(std::istream& stream, Modint& mint) {unsigned long long value;stream >> value;mint.set(value);return stream;}friend std::ostream& operator<<(std::ostream& stream, Modint mint) {stream << mint.get();return stream;}private:unsigned long long value_;};} // namespace nononamespace nono {void solve() {using Mint = Modint<998244353>;int n, m;std::cin >> n >> m;std::vector<Mint> a(n);for (int i = 0; i < n; i++) std::cin >> a[i];UnionFind uf(n);for (int i = 0; i < m; i++) {int u, v;std::cin >> u >> v;u--;v--;uf.merge(u, v);}Mint ans = 1;for (auto group: uf.groups()) {Mint sum = 0;for (int v: group) {sum += a[v];}ans *= sum.pow(group.size());}std::cout << ans << std::endl;}} // namespace nonoint main() {std::cin.tie(0)->sync_with_stdio(false);std::cout << std::fixed << std::setprecision(15);int t = 1;while (t--) nono::solve();}