結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 15:56:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 227 ms / 2,000 ms |
| コード長 | 668 bytes |
| コンパイル時間 | 2,184 ms |
| コンパイル使用メモリ | 203,452 KB |
| 最終ジャッジ日時 | 2025-02-18 05:02:58 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/dsu>
using namespace atcoder;
int main() {
int N, M;
cin >> N >> M;
vector<int> C(N);
for (int i = 0; i < N; i++) {
cin >> C[i];
}
dsu Union(N);
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u--, v--;
if (C[u] == C[v]) {
Union.merge(u, v);
}
}
auto g = Union.groups();
vector<int> cnt(N + 1, 0);
for (auto x : g) {
// for (auto y : x) cout << y << " ";
// cout << endl;
cnt[C[x[0]]]++;
}
int ans = 0;
for (int i = 1; i <= N; i++) {
if (cnt[i] > 0) {
ans += cnt[i] - 1;
}
}
cout << ans << endl;
return 0;
}