結果
問題 | No.2494 Sum within Components |
ユーザー |
![]() |
提出日時 | 2023-10-06 22:57:56 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 89 ms / 2,000 ms |
コード長 | 7,292 bytes |
コンパイル時間 | 2,972 ms |
コンパイル使用メモリ | 262,812 KB |
実行使用メモリ | 20,920 KB |
最終ジャッジ日時 | 2024-07-26 16:51:29 |
合計ジャッジ時間 | 4,336 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
// #pragma GCC target("avx")// #pragma GCC optimize("O3")// #pragma GCC optimize("unroll-loops")#include <bits/stdc++.h>using namespace std;#define rep(i,n) for(int i = 0; i < (int)n; i++)#define FOR(n) for(int i = 0; i < (int)n; i++)#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)#define all(x) x.begin(),x.end()//#define mp make_pair#define vi vector<int>#define vvi vector<vi>#define vvvi vector<vvi>#define vvvvi vector<vvvi>#define pii pair<int,int>#define vpii vector<pair<int,int>>template<typename T>bool chmax(T &a, const T b) {if(a<b) {a=b; return true;} else {return false;}}template<typename T>bool chmin(T &a, const T b) {if(a>b) {a=b; return true;} else {return false;}}using ll = long long;using ld = long double;using ull = unsigned long long;const ll INF = numeric_limits<long long>::max() / 2;const ld pi = 3.1415926535897932384626433832795028;const ll mod = 998244353;int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};#define int long longtemplate<long long MOD>struct Modular_Int {long long x;Modular_Int() = default;Modular_Int(long long x_) : x(x_ >= 0? x_%MOD : (MOD-(-x_)%MOD)%MOD) {}long long val() const {return (x%MOD+MOD)%MOD;}static long long get_mod() {return MOD;}Modular_Int<MOD>& operator^=(long long d) {Modular_Int<MOD> ret(1);long long nx = x;while(d) {if(d&1) ret *= nx;(nx *= nx) %= MOD;d >>= 1;}*this = ret;return *this;}Modular_Int<MOD> operator^(long long d) const {return Modular_Int<MOD>(*this) ^= d;}Modular_Int<MOD> pow(long long d) const {return Modular_Int<MOD>(*this) ^= d;}//use this basicallyModular_Int<MOD> inv() const {return Modular_Int<MOD>(*this) ^ (MOD-2);}//only if the module number is not prime//Don't use. This is broken.// Modular_Int<MOD> inv() const {// long long a = (x%MOD+MOD)%MOD, b = MOD, u = 1, v = 0;// while(b) {// long long t = a/b;// a -= t*b, swap(a, b);// u -= t*v, swap(u, v);// }// return Modular_Int<MOD>(u);// }Modular_Int<MOD>& operator+=(const Modular_Int<MOD> other) {if((x += other.x) >= MOD) x -= MOD;return *this;}Modular_Int<MOD>& operator-=(const Modular_Int<MOD> other) {if((x -= other.x) < 0) x += MOD;return *this;}Modular_Int<MOD>& operator*=(const Modular_Int<MOD> other) {long long z = x;z *= other.x;z %= MOD;x = z;if(x < 0) x += MOD;return *this;}Modular_Int<MOD>& operator/=(const Modular_Int<MOD> other) {return *this = *this * other.inv();}Modular_Int<MOD>& operator++() {x++;if (x == MOD) x = 0;return *this;}Modular_Int<MOD>& operator--() {if (x == 0) x = MOD;x--;return *this;}Modular_Int<MOD> operator+(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) += other;}Modular_Int<MOD> operator-(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) -= other;}Modular_Int<MOD> operator*(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) *= other;}Modular_Int<MOD> operator/(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) /= other;}Modular_Int<MOD>& operator+=(const long long other) {Modular_Int<MOD> other_(other); *this += other_; return *this;}Modular_Int<MOD>& operator-=(const long long other) {Modular_Int<MOD> other_(other); *this -= other_; return *this;}Modular_Int<MOD>& operator*=(const long long other) {Modular_Int<MOD> other_(other); *this *= other_; return *this;}Modular_Int<MOD>& operator/=(const long long other) {Modular_Int<MOD> other_(other); *this /= other_; return *this;}Modular_Int<MOD> operator+(const long long other) const {return Modular_Int<MOD>(*this) += other;}Modular_Int<MOD> operator-(const long long other) const {return Modular_Int<MOD>(*this) -= other;}Modular_Int<MOD> operator*(const long long other) const {return Modular_Int<MOD>(*this) *= other;}Modular_Int<MOD> operator/(const long long other) const {return Modular_Int<MOD>(*this) /= other;}bool operator==(const Modular_Int<MOD> other) const {return (*this).val() == other.val();}bool operator!=(const Modular_Int<MOD> other) const {return (*this).val() != other.val();}bool operator==(const long long other) const {return (*this).val() == other;}bool operator!=(const long long other) const {return (*this).val() != other;}Modular_Int<MOD> operator-() const {return Modular_Int<MOD>(0LL)-Modular_Int<MOD>(*this);}// friend constexpr istream& operator>>(istream& is, mint& x) noexcept {// long long X;// is >> X;// x = X;// return is;// }// friend constexpr ostream& operator<<(ostream& os, mint& x) {// os << x.val();// return os;// }};// const long long MOD_VAL = 1e9+7;const long long MOD_VAL = 998244353;using mint = Modular_Int<MOD_VAL>;//mint operator^ は掛け算みたいに優先されないことに注意!!//基本的にはpowを使うこと!!//cf: http://www5f.biglobe.ne.jp/~fuku-labo/library/program/cpp/1/008-1.htmstruct UnionFind {vector<int> r;UnionFind(int n) {r = vector<int>(n, -1);}int root(int x) {if(r[x] < 0) return x;return r[x] = root(r[x]);}bool unite(int x, int y) {x = root(x);y = root(y);if(x == y) return false;if(r[x] > r[y]) swap(x, y);r[x] += r[y];r[y] = x;return true;}bool issame(int x, int y) {return root(x) == root(y);}int size(int x) {return -r[root(x)];}// int number_of_set() {// unordered_set<int> st;// for(int i = 0; i < (int)r.size(); i++) st.insert(root(i));// return st.size();// }// only vertices: not including leader posvector<vector<int>> decompose() {vector<pair<int, int>> p;for(int i = 0; i < (int)r.size(); i++) p.emplace_back(root(i), i);sort(all(p));//first:root, second:verticesvector<vector<int>> ret;int pre = -1;for(pair<int, int> e : p) {if(pre != e.first) {ret.push_back(vector<int>{e.second});}else {ret.back().push_back(e.second);}pre = e.first;}return ret;}};void solve() {int n, m;cin >> n >> m;vi a(n);FOR(n) cin >> a[i];UnionFind UF(n);FOR(m) {int u, v;cin >> u >> v;--u;--v;UF.unite(u, v);}mint ans = 1;vvi decom = UF.decompose();for(auto e : decom) {mint sum = 0;for(auto f : e) sum += a[f];ans *= sum.pow((int)e.size());}cout << ans.val() << endl;}signed main() {cin.tie(nullptr);ios::sync_with_stdio(false);solve();return 0;}