結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 15:20:54 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 248 ms / 2,000 ms |
| コード長 | 1,282 bytes |
| コンパイル時間 | 2,163 ms |
| コンパイル使用メモリ | 205,320 KB |
| 最終ジャッジ日時 | 2025-02-18 04:28:35 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par, siz;
UnionFind(int n) : par(n, -1), siz(n, 1) {}
int root(int x) {
if (par[x] == -1) return x;
else return par[x] = root(par[x]);
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
par[y] = x;
siz[x] += siz[y];
return true;
}
int size(int x) {
return siz[root(x)];
}
};
int main() {
int N, M;
cin >> N >> M;
vector<int> C(N);
for (int i = 0; i < N; i++) {
cin >> C[i];
C[i]--;
}
vector<vector<int>> V(N);
for (int i = 0; i < N; i++) V[C[i]].push_back(i);
UnionFind uf(N);
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
if (C[u] != C[v]) continue;
uf.unite(u, v);
}
int ans = 0;
set<int> S;
for (int i = 0; i < N; i++) {
if (V[i].size() <= 1) continue;
for (int j : V[i]) {
S.insert(uf.root(j));
}
ans += S.size()-1;
S = {};
}
cout << ans << endl;
}