結果

問題 No.2494 Sum within Components
ユーザー otamay6
提出日時 2023-10-15 23:46:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 849 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 75,492 KB
最終ジャッジ日時 2025-02-17 08:00:37
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other AC * 1 RE * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:47:21: warning: ‘u’ may be used uninitialized [-Wmaybe-uninitialized]
   47 |                 int u,v;
      |                     ^
main.cpp:47:23: warning: ‘v’ may be used uninitialized [-Wmaybe-uninitialized]
   47 |                 int u,v;
      |                       ^

ソースコード

diff #

#include<iostream>
#include<vector>
#include<utility>
#define int long long

struct uf{
	std::vector<int> r;
	uf(int n):r(std::vector<int>(n, -1)){};
	void change(int u, int val){
		r[u] = -val;
	}
	int root(int u){
		if(r[u]<0){
			return u;
		}
		else{
			return r[u] = root(r[u]);
		}
	}
	int size(int u){
		return -r[root(u)];
	}
	bool connect(int a,int b){
		if(size(a) < size(b)){
			std::swap(a,b);
		}
		if(root(a) == root(b)){
			return false;
		}
		a = root(a);
		b = root(b);
		r[a] += r[b];
		r[b] = a;
		return true;
	}
};

signed main(void){
	int n,m;
	std::cin>>n>>m;
	uf g(n);
	for(int i = 0; i < n; i++){
		int a;std::cin>>a;
		g.change(i, a);
	}
	while(m--){
		int u,v;
		u--;v--;
		g.connect(u,v);
	}
	int res = 1;
	for(int i=0; i < n; i++){
		res *= g.size(i) % 998244353;
		res %= 998244353;
	}
	std::cout << res << std::endl;
}
0