結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
4O4
|
| 提出日時 | 2023-12-02 15:19:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 262 ms / 2,000 ms |
| コード長 | 1,822 bytes |
| コンパイル時間 | 2,107 ms |
| コンパイル使用メモリ | 177,192 KB |
| 実行使用メモリ | 25,216 KB |
| 最終ジャッジ日時 | 2024-09-26 18:29:29 |
| 合計ジャッジ時間 | 6,569 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
int ctoi(char c){return c-'0';}
ll ctoll(char c){return c-'0';}
struct UnionFind{
vector<int> par,siz,dig;
//閉路の有無
bool has_loop = false;
int loop_count = 0;
//初期化
UnionFind(int n) : par(n, -1) , siz(n, 1), dig(n, 0) {}
//根を求める
int root(int x){
if(par[x] == -1)return x;
else return par[x] = root(par[x]);
}
//xとyが同じグループに属するかどうか(根が一致するかどうか)
bool issame(int x, int y){
return root(x) == root(y);
}
//xを含むグループとyを含むグループを合併する
bool unite(int x, int y){
dig[x]++;dig[y]++; //次数記録しておく
x = root(x); y = root(y);
if(x == y) {
has_loop = true;
loop_count++;
return false;
}
if(siz[x] < siz[y]) swap(x,y);
par[y] = x;
siz[x] += siz[y];
return true;
}
//xを含むグループのサイズ
int size(int x){
return siz[root(x)];
}
//各点の次数を返す
int digree(int x){
return dig[x];
}
};
int main(){
int n,m;
cin >> n >> m;
vector<int>c(n);
rep(i,n){
cin >> c[i];
c[i]--;
}
UnionFind uf(n);
rep(i,m){
int u,v;
cin >> u >> v;
u--;v--;
if(c[u] == c[v])uf.unite(u,v);
}
vector<set<int> >cr(n);
rep(i,n){
if(!cr[c[i]].count(uf.root(i))){
cr[c[i]].insert(uf.root(i));
}
}
int ans = 0;
rep(i,n){
ans += max((int)cr[i].size()-1,0);
}
cout << ans << endl;
}
4O4