結果

問題 No.2563 色ごとのグループ
ユーザー Today03Today03
提出日時 2023-12-02 20:51:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 1,200 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 212,712 KB
実行使用メモリ 24,412 KB
最終ジャッジ日時 2023-12-02 20:51:30
合計ジャッジ時間 6,740 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 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 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 3 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 3 ms
6,548 KB
testcase_17 AC 3 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 5 ms
6,548 KB
testcase_20 AC 15 ms
6,548 KB
testcase_21 AC 9 ms
6,548 KB
testcase_22 AC 8 ms
6,548 KB
testcase_23 AC 14 ms
6,548 KB
testcase_24 AC 119 ms
11,556 KB
testcase_25 AC 101 ms
12,804 KB
testcase_26 AC 143 ms
17,504 KB
testcase_27 AC 135 ms
23,312 KB
testcase_28 AC 177 ms
19,096 KB
testcase_29 AC 229 ms
24,412 KB
testcase_30 AC 227 ms
24,412 KB
testcase_31 AC 223 ms
24,412 KB
testcase_32 AC 231 ms
24,412 KB
testcase_33 AC 225 ms
16,604 KB
testcase_34 AC 215 ms
16,604 KB
testcase_35 AC 214 ms
16,604 KB
testcase_36 AC 217 ms
16,604 KB
testcase_37 AC 215 ms
16,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include "./debug.cpp"
#else
#define debug(...)
#define print_line
#endif
using namespace std;
using ll = long long;

/**
 * @brief Disjoint Set Union
 * @docs docs/graph/dsu.md
*/

struct dsu {
	vector<int> par, sz;
	dsu(int n) {
		par.resize(n);
		sz.resize(n);
		for (int i = 0; i < n; i++) {
			par[i] = i;
			sz[i] = 1;
		}
	}
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		par[x] = find(par[x]);
		return par[x];
	}
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) {
			return;
		}
		if (sz[x] < sz[y]) {
			swap(x, y);
		}
		par[y] = x;
		sz[x] += sz[y];
	}
	int size(int x) {
		return sz[find(x)];
	}
	bool is_united(int x, int y) {
		return find(x) == find(y);
	}
};

int main() {
	int N, M;
	cin >> N >> M;
	vector<int> C(N);
	for (int i = 0; i < N; i++) {
		cin >> C[i];
		C[i]--;
	}
	dsu ds(N);
	for (int i = 0; i < M; i++) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		if (C[u] == C[v]) ds.unite(u, v);
	}
	vector<set<int>> D(N);
	for (int i = 0; i < N; i++) {
		D[C[i]].insert(ds.find(i));
	}
	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (D[i].size() > 0) ans += D[i].size() - 1;
	}
	cout << ans << endl;
}
0