結果
| 問題 |
No.2494 Sum within Components
|
| コンテスト | |
| ユーザー |
Astral__
|
| 提出日時 | 2023-10-06 21:35:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 102 ms / 2,000 ms |
| コード長 | 8,604 bytes |
| コンパイル時間 | 2,501 ms |
| コンパイル使用メモリ | 215,168 KB |
| 最終ジャッジ日時 | 2025-02-17 04:54:11 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
#include<bits/stdc++.h>
#define PPque priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, greater<tuple<ll, ll, ll>>>
#define Pque priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
#define pque priority_queue<ll, vector<ll>, greater<ll>>
#define umap unordered_map
#define uset unordered_set
#define rep(i, s, f) for(ll i = s; i <= f; i++)
#define per(i, s, f) for(ll i = s; i >= f; i--)
#define all0(x) (x).begin() ,(x).end()
#define all(x) (x).begin() + 1, (x).end()
#define vvvvl vector<vvvl>
#define vvvi vector<vector<vector<int>>>
#define vvvl vector<vector<vector<ll>>>
#define vvvc vector<vector<vector<char>>>
#define vvvd vector<vector<vector<double>>>
#define vvvm vector<vector<vector<mint>>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvs vector<vector<string>>
#define vvc vector<vector<char>>
#define vvp vector<vector<pair<ll, ll>>>
#define vvb vector<vector<bool>>
#define vvd vector<vector<double>>
#define vvm vector<vector<mint>>
#define vp vector<pair<ll, ll>>
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vd vector<double>
#define vm vector<mint>
#define P pair<ll, ll>
#define TU tuple<ll, ll, ll>
#define rrr(l, r) mt()%(r-l+1)+l
#define ENDL '\n'
#define ull unsigned long long
typedef long long ll;
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//これが本当の組み込み関数ってね(笑)
template <typename T>
T or_less(vector<T> &A, T x) { //x以下で最大要素の添字 前提: sort済み 存在しない: -1
return distance(A.begin(), upper_bound(A.begin(), A.end(), x)-1);
}
template <typename T>
T under(vector<T> &A, T x) { //x未満の最大要素の添字 前提: sort済み 存在しない: -1
return distance(A.begin(), lower_bound(A.begin(), A.end(), x)-1);
}
template <typename T>
T or_more(vector<T> &A, T x) { //x以上で最小要素の添字 前提: sort済み 存在しない: N . //distanceのA.beginは添字を出すために常にA.begin() NG: A.begin() + 1
return distance(A.begin(), lower_bound(A.begin(), A.end(), x));
}
template <typename T>
T over(vector<T> &A, T x) { //xより大きい最小要素の添字前提: sort済み 存在しない: N
return distance(A.begin(), upper_bound(A.begin(), A.end(), x));
}
void compress(vector<ll> &A) {//小さい順に順位、大きい順にしたいならreverseはNG最後に変換 //重複無し //最初は番兵
vector<ll> temp = A;
sort(temp.begin()+1, temp.end());
temp.erase(unique(temp.begin()+1, temp.end()), temp.end());
for (int i = 1; i <= int(A.size()-1); i++) {
A.at(i) = distance(temp.begin(), lower_bound(temp.begin()+1, temp.end(), A.at(i)));
}
}
template <typename T>
void UNIQUE(vector<T> &A) {
sort(all0(A));
return A.erase(unique(A.begin(), A.end()), A.end());
}
template <typename T>
void rev90(vector<vector<T>> &A, int indexed = 1) {
reverse(A.begin() + indexed, A.end());
int n = A.size();
rep(i, indexed, n-1) {
rep(j, i+1, n-1) {
swap(A.at(i).at(j), A.at(j).at(i));
}
}
}
void chmin(ll &a, ll b) {
a = min(a, b);
}
void chmax(ll &a, ll b) {
a = max(a, b);
}
//////////////////////////////////////////////////////////////////////
//数学系
///////////////////////////////////////////////////////////////////////
ll round(ll x, ll i) {return ll(x + 5 * pow(10, i-1))/ll(pow(10, i)) * ll(pow(10, i));}
vp insu_bunkai(ll N) {
vp res;
for (int i = 2; i * i <= N; i++) {
ll cnt = 0;
while(N % i == 0) {
cnt++;
N /= i;
}
if(cnt != 0) res.push_back(P(i, cnt));
}
if(N != 1) res.push_back(P(N, 1));
return res;
}
ll extgcd (ll a, ll b, ll &x, ll &y) {
if(b == 0) { x = 1;y = 0;return a;}
ll d = extgcd(b, a%b, y, x);
y -= a/b * x;
return d;
}
template <typename T>
T ceil(T a, T b) {
assert(b != 0);
if(a % b == 0) return a / b;
if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b + 1;
else return a / b;
}
template <typename T>
T floor(T a, T b) {
assert(b != 0);
if(a % b == 0) return a / b;
if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b;
else return a/b - 1;
}
ll modpow(ll x, ll y) {
const ll mod = 9223372036854775807LL;
if(x > mod) x %= mod;
if(y == 0) return 1;
ll res = modpow(x, y >> 1);
res = res * res % mod;
if(y & 1) res *= x, res %= mod;
return res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//グローバル変数を置くところ(情報工学意識高め)
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
const ll int_max = 1001001001;
const ll ll_max = 1001001001001001001LL;
const double pi = 3.141592653589793;
vl dx{0, 1, 0, -1, 0, 1, 1, -1, -1}; // (番兵) → ↓ ← ↑ ※ 右から時計回り
vl dy{0, 0, -1, 0, 1, 1, -1, -1, 1};
//const ll mod = 1000000007;
const ll mod = 998244353;
vl A(200001);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct mint{
ll x;
constexpr mint(ll x=0) noexcept : x((x%mod+mod)%mod){}
constexpr ll val() const noexcept {return x;}
constexpr mint& operator+=(const mint& a) noexcept {if((x += a.x) >= mod) x -= mod;return *this;}
constexpr mint& operator-=(const mint& a) noexcept {if((x += mod - a.x) >= mod) x -= mod;return *this;}
constexpr mint& operator*=(const mint& a) noexcept {(x *= a.x) %= mod;return *this;}
constexpr mint operator+(const mint& a) const noexcept {mint res(*this);return res+=a;}
constexpr mint operator-(const mint& a) const noexcept {mint res(*this);return res-=a;}
constexpr mint operator*(const mint& a) const noexcept {mint res(*this);return res*=a;}
constexpr mint pow(const ll& y) const {
if(!y) return 1;
mint res = pow(y >> 1);
res *= res;
if(y & 1) res *= *this;
return res;
}
//以下、modが素数の時に限る
/*
constexpr mint inv() const {return pow(mod-2);}
constexpr mint& operator/=(const mint& a) {return (*this) *= a.inv();}
constexpr mint operator/(const mint& a) const {mint res(*this);return res /= a;}
*/
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
struct Union {
vector<int> Par;
vector<int> size;
vector<int> edge;
vector<mint> sum;
Union(int N) {
Par.resize(N+1);
for (int i = 1; i <= N; i++) {
Par.at(i) = i;
}
size.assign(N+1, 1);
edge.assign(N+1, 0);
sum.resize(N+1);
rep(i, 1, N) {
sum.at(i) = A.at(i);
}
}
int root(int u) {
if (Par.at(u) != u) {
return Par.at(u) = root(Par.at(u));
}
return u;
}
bool same(int a, int b) {
if (root(a) == root(b)) {
return true;
}
return false;
}
void unit(int a, int b) {
int rootA = root(a);
int rootB = root(b);
if (rootA == rootB) {
edge.at(rootA)++;
return ;
}
if (size.at(rootA) < size.at(rootB)) {//どっちを親にするかの条件
swap(a, b);
rootA = root(a);
rootB = root(b); //常にAにBがくっつくようにする
}
Par.at(rootB) = rootA;
size.at(rootA) += size.at(rootB);
edge.at(rootA) += edge.at(rootB);
edge.at(rootA) ++;
sum.at(rootA) += sum.at(rootB);
}
};
void solve() {
ll N, M;
cin >> N >> M;
rep(i, 1, N) cin >> A.at(i);
Union uni(N);
vvl G(N+1);
rep(i, 1, M) {
ll u, v;
cin >> u >> v;
G.at(u).push_back(v);
G.at(v).push_back(u);
uni.unit(u, v);
}
mint s = 1;
rep(i, 1, N) {
s *= uni.sum.at(uni.root(i));
}
cout << s << endl;
}
//座標を2で割った時の”切り捨て側(左側)”を求めるには 誤:(x / 2) マイナスの時!!! 正:floor<ll>(x, 2);
//stringでの数字の下から1桁目は 正:S.at(N-1) 誤:S.at(0)
//if(S.at(i) == 1) ← charなのに1...?
// modは取りましたか...?(´・ω・`)
//sortの比較関数は、 a == b ならば falseを返す必要がある(そうで無いとRE)
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
ll T = 1;
//cin >> T;
rep(i, 1, T) {
solve();
}
return 0;
}
Astral__