結果

問題 No.2563 色ごとのグループ
ユーザー may17may17
提出日時 2023-12-02 15:32:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 4,398 ms
コンパイル使用メモリ 268,872 KB
実行使用メモリ 25,264 KB
最終ジャッジ日時 2023-12-02 15:32:45
合計ジャッジ時間 9,583 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
14,512 KB
testcase_01 AC 5 ms
14,512 KB
testcase_02 AC 6 ms
14,512 KB
testcase_03 AC 6 ms
14,512 KB
testcase_04 AC 6 ms
14,512 KB
testcase_05 AC 6 ms
14,512 KB
testcase_06 AC 5 ms
14,512 KB
testcase_07 AC 5 ms
14,512 KB
testcase_08 AC 6 ms
14,512 KB
testcase_09 AC 6 ms
14,512 KB
testcase_10 AC 6 ms
14,512 KB
testcase_11 AC 5 ms
14,512 KB
testcase_12 AC 6 ms
14,512 KB
testcase_13 AC 5 ms
14,512 KB
testcase_14 AC 6 ms
14,512 KB
testcase_15 AC 7 ms
14,512 KB
testcase_16 AC 7 ms
14,512 KB
testcase_17 AC 6 ms
14,512 KB
testcase_18 AC 6 ms
14,512 KB
testcase_19 AC 9 ms
14,512 KB
testcase_20 AC 19 ms
15,408 KB
testcase_21 AC 13 ms
14,896 KB
testcase_22 AC 12 ms
14,768 KB
testcase_23 AC 18 ms
14,768 KB
testcase_24 AC 128 ms
18,480 KB
testcase_25 AC 110 ms
19,120 KB
testcase_26 AC 161 ms
21,552 KB
testcase_27 AC 187 ms
24,624 KB
testcase_28 AC 189 ms
22,448 KB
testcase_29 AC 256 ms
25,264 KB
testcase_30 AC 255 ms
25,264 KB
testcase_31 AC 259 ms
25,264 KB
testcase_32 AC 287 ms
25,264 KB
testcase_33 AC 266 ms
25,264 KB
testcase_34 AC 259 ms
25,264 KB
testcase_35 AC 262 ms
25,264 KB
testcase_36 AC 279 ms
25,264 KB
testcase_37 AC 265 ms
25,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
const ll mod = 1000000007;
//const ll mod = 998244353;
int n, m, c[200009], u[200009], v[200009];
set<int>S[200009];
int main() {
	cin >> n >> m;
	dsu UF(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		S[c[i]].insert(i);
	}
	for (int i = 1; i <= m; i++) {
		cin >> u[i] >> v[i];
		if(c[u[i]]==c[v[i]])UF.merge(u[i], v[i]);
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ll cnt = 0, par = 0;
		for (auto& s : S[i]) {
			cnt++;
			if (cnt == 1) {
				par = s;
				continue;
			}
			if (UF.same(par, s) == false) {
				ans++;
				UF.merge(par, s);
			}
		}
	}
	cout << ans << endl;
}
0