結果

問題 No.2563 色ごとのグループ
ユーザー norikamenorikame
提出日時 2023-12-03 19:17:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 1,187 bytes
コンパイル時間 4,947 ms
コンパイル使用メモリ 266,512 KB
実行使用メモリ 23,088 KB
最終ジャッジ日時 2023-12-03 19:17:13
合計ジャッジ時間 10,154 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 4 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 4 ms
6,676 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 17 ms
6,676 KB
testcase_21 AC 11 ms
6,676 KB
testcase_22 AC 9 ms
6,676 KB
testcase_23 AC 15 ms
6,676 KB
testcase_24 AC 146 ms
11,648 KB
testcase_25 AC 120 ms
12,288 KB
testcase_26 AC 186 ms
16,592 KB
testcase_27 AC 165 ms
19,448 KB
testcase_28 AC 249 ms
18,196 KB
testcase_29 AC 316 ms
23,084 KB
testcase_30 AC 310 ms
23,084 KB
testcase_31 AC 311 ms
23,088 KB
testcase_32 AC 340 ms
23,088 KB
testcase_33 AC 263 ms
20,280 KB
testcase_34 AC 259 ms
20,276 KB
testcase_35 AC 259 ms
20,280 KB
testcase_36 AC 251 ms
20,284 KB
testcase_37 AC 281 ms
20,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 

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

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const int INF = (int)(1e9);

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> c(n);
	rep(i, n) {
		cin >> c[i];
		c[i]--;
	}
	vector<vector<int>> g(n);
	rep(i, m) {
		int ui, vi;
		cin >> ui >> vi;
		--ui; --vi;
		g[ui].push_back(vi);
		g[vi].push_back(ui);
	}
	vector<vector<int>> clst(n);
	rep(i, n) clst[c[i]].push_back(i);
	int res = 0;
	vector<bool> visited(n);
	rep(i, n) if (!clst[i].empty()) {
		int cnt = 0;
		for (const int& si : clst[i]) if (!visited[si]) {
			++cnt;
			queue<int> que;
			visited[si] = true;
			que.push(si);
			while (!que.empty()) {
				int vi = que.front(); que.pop();
				for (const int& ti : g[vi]) if (c[ti] == c[vi] && !visited[ti]) {
					visited[ti] = true;
					que.push(ti);
				}
			}
		}
		res += cnt - 1;
	}
	cout << res << endl;
	return 0;
}
0