結果
問題 |
No.2563 色ごとのグループ
|
ユーザー |
![]() |
提出日時 | 2023-12-02 16:46:13 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,528 bytes |
コンパイル時間 | 3,996 ms |
コンパイル使用メモリ | 234,408 KB |
実行使用メモリ | 16,920 KB |
最終ジャッジ日時 | 2024-09-26 20:42:05 |
合計ジャッジ時間 | 9,075 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 TLE * 1 -- * 13 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; using ll = long long; struct unionfind{ //par=親 siz=グループの頂点数 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]); } //xとyが同グループか(根が同じか) bool issame(int x, int y){ return root(x) == root(y); } //xを含むグループとyを含むグループを併合する bool unite(int x, int y){ x=root(x), y=root(y); if(x == y)return false; //y側のサイズが小さくなるようにする if(siz[x] < siz[y]) swap(x, y); //yをxの子とする par[y] = x; siz[x] += siz[y]; return true; } int size(int x){ return siz[root(x)]; } }; int main(){ // 宣言 unionfind 変数名(頂点数) 各関数を利用 変数名.unite(x, y) int n, m; cin >> n >> m; vector<int> c (n); set<int> c_num; for(int i = 0; i < n; ++i){ cin >> c[i]; c_num.insert(c[i]); } vector<vector<int>> g (n); for(int i = 0; i < m; ++i){ int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } int ans = 0; for(auto x : c_num){ unionfind uni(n); for(int j = 0; j < n; ++j){ if(c[j] == x){ for(auto y : g[j]){ if(c[y] == c[j])uni.unite(j, y); } } } int count = 0; for(int j = 0; j < n; ++j){ if(c[j] == x && uni.root(j) == j) count ++; } ans += count - 1; } cout << ans << endl; }