結果

問題 No.2494 Sum within Components
ユーザー nono00nono00
提出日時 2023-10-06 21:46:28
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 4,899 bytes
コンパイル時間 3,512 ms
コンパイル使用メモリ 258,040 KB
実行使用メモリ 17,968 KB
最終ジャッジ日時 2023-10-06 21:46:36
合計ジャッジ時間 4,368 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 51 ms
9,772 KB
testcase_15 AC 50 ms
7,448 KB
testcase_16 AC 36 ms
11,684 KB
testcase_17 AC 45 ms
17,968 KB
testcase_18 AC 47 ms
17,492 KB
testcase_19 AC 66 ms
13,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 internal

template <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 nono

namespace 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 nono

int main() {
    std::cin.tie(0)->sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);

    int t = 1;

    while (t--) nono::solve();
}
0