結果

問題 No.2494 Sum within Components
ユーザー shobonvipshobonvip
提出日時 2023-10-06 21:30:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 844 bytes
コンパイル時間 2,305 ms
コンパイル使用メモリ 207,708 KB
実行使用メモリ 14,688 KB
最終ジャッジ日時 2023-10-06 21:30:17
合計ジャッジ時間 4,280 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 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 12 ms
4,380 KB
testcase_10 AC 14 ms
4,408 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 22 ms
4,376 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 195 ms
11,540 KB
testcase_15 AC 189 ms
10,176 KB
testcase_16 AC 80 ms
8,836 KB
testcase_17 AC 80 ms
9,352 KB
testcase_18 AC 92 ms
9,788 KB
testcase_19 AC 247 ms
14,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
int main(){
	int n, m; cin >> n >> m;
	vector<ll> a(n);
	for (int i=0; i<n; i++){
		cin >> a[i];
	}
	vector ikeru(n, vector<int>(0));
	for (int i=0; i<m; i++){
		int u, v; cin >> u >> v;
		u--; v--;
		ikeru[u].push_back(v);
		ikeru[v].push_back(u);
	}	
	vector<bool> tansaku(n);
	vector<int> mada;
	ll ret = 1;
	for (int st=0; st<n; st++){
		if (tansaku[st]) continue;
		mada.push_back(st);
		tansaku[st] = true;
		ll tmp = 0;
		int num = 0;
		while(!mada.empty()){
			int i = mada.back();
			mada.pop_back();
			tmp += a[i];
			num++;
			for (int j: ikeru[i]){
				if (tansaku[j]) continue;
				tansaku[j] = true;
				mada.push_back(j);
			}
		}
		tmp %= mod;
		for (int x=0; x<num; x++){
			ret *= tmp;
			ret %= mod;
		}
	}
	cout << ret << '\n';
}
0