結果
| 問題 | No.2563 色ごとのグループ | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-12-02 15:32:35 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 267 ms / 2,000 ms | 
| コード長 | 708 bytes | 
| コンパイル時間 | 4,656 ms | 
| コンパイル使用メモリ | 255,108 KB | 
| 最終ジャッジ日時 | 2025-02-18 04:42:02 | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 35 | 
ソースコード
#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;
}
            
            
            
        