結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
forest3
|
| 提出日時 | 2024-01-07 16:41:51 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 204 ms / 2,000 ms |
| コード長 | 1,056 bytes |
| コンパイル時間 | 1,445 ms |
| コンパイル使用メモリ | 175,668 KB |
| 実行使用メモリ | 13,440 KB |
| 最終ジャッジ日時 | 2024-09-27 19:26:25 |
| 合計ジャッジ時間 | 5,804 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for( ll i = 0; i < n; i++ )
using ll = long long;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) { }
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) {
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) {
return -data[root(x)];
}
};
int main() {
int N, M;
cin >> N >> M;
vector<int> c( N );
vector<vector<int>> vc( N );
rep(i, N) {
cin >> c[i];
c[i]--;
vc[c[i]].push_back( i );
}
UnionFind uf( N );
rep(i, M) {
int a, b;
cin >> a >> b;
a--;
b--;
if(c[a] == c[b]) uf.unionSet(a, b);
}
int ans = 0;
rep(i, N) {
if(vc[i].size() < 2) continue;
for( int e : vc[i] ) {
if(uf.findSet(vc[i][0], e)) continue;
ans++;
uf.unionSet(vc[i][0], e);
}
}
cout << ans << endl;
}
forest3