結果

問題 No.2563 色ごとのグループ
ユーザー rinrionrinrion
提出日時 2023-12-02 16:46:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,528 bytes
コンパイル時間 4,217 ms
コンパイル使用メモリ 235,356 KB
実行使用メモリ 18,196 KB
最終ジャッジ日時 2023-12-02 16:46:24
合計ジャッジ時間 9,265 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 1 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 5 ms
6,548 KB
testcase_15 AC 5 ms
6,548 KB
testcase_16 AC 5 ms
6,548 KB
testcase_17 AC 5 ms
6,548 KB
testcase_18 AC 6 ms
6,548 KB
testcase_19 AC 7 ms
6,548 KB
testcase_20 AC 391 ms
6,548 KB
testcase_21 AC 79 ms
6,548 KB
testcase_22 AC 26 ms
6,548 KB
testcase_23 AC 57 ms
6,548 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>	
using namespace std;
using namespace atcoder;
using ll = long long;

struct unionfind{
	//par=親 siz=グループの頂点数
	vector<int>par, siz;

	unionfind(int n) : par(n, -1),siz(n, 1){}

	//根を求める
	int root(int x){
		if(par[x] == -1) return x;
		else return par[x] = root(par[x]);
	}

	//xとyが同グループか(根が同じか)
	bool issame(int x, int y){
		return root(x) == root(y);
	}

	//xを含むグループとyを含むグループを併合する
	bool unite(int x, int y){
		x=root(x), y=root(y);
		if(x == y)return false;
		//y側のサイズが小さくなるようにする
		if(siz[x] < siz[y]) swap(x, y);

		//yをxの子とする
		par[y] = x;
		siz[x] += siz[y];
		return true;
	}

	int size(int x){
		return siz[root(x)];
	}
};

int main(){
	// 宣言 unionfind 変数名(頂点数) 各関数を利用 変数名.unite(x, y)
	int n, m;
	cin >> n >> m;

	vector<int> c (n);
	set<int> c_num;
	for(int i = 0; i < n; ++i){
		cin >> c[i];
		c_num.insert(c[i]);
	}
	
	vector<vector<int>> g (n);
	for(int i = 0; i < m; ++i){
		int u, v;
		cin >> u >> v;
		
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	
	int ans = 0;

	for(auto x : c_num){	
		unionfind uni(n);
		for(int j = 0; j < n; ++j){
			if(c[j] == x){
				for(auto y : g[j]){
					if(c[y] == c[j])uni.unite(j, y);
				}
			}
		}
		int count = 0;
		for(int j = 0; j < n; ++j){
			if(c[j] == x && uni.root(j) == j)  count ++;
		}

		ans += count - 1;
	}
	
	cout << ans << endl;
	
}
0