結果
問題 | No.2563 色ごとのグループ |
ユーザー |
![]() |
提出日時 | 2023-12-02 15:07:07 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 99 ms / 2,000 ms |
コード長 | 2,359 bytes |
コンパイル時間 | 6,308 ms |
コンパイル使用メモリ | 190,976 KB |
実行使用メモリ | 19,988 KB |
最終ジャッジ日時 | 2024-09-26 18:06:37 |
合計ジャッジ時間 | 7,757 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
#pragma GCC target("avx")#pragma GCC optimize("O3")#pragma GCC optimize("unroll-loops")#include <bits/extc++.h>using namespace std;using ld = long double;const vector<int> dx = {0, 0, 1, -1};const vector<int> dy = {1, -1, 0, 0};#define vec vector#define int long long#define double long double//cout<<setprecision(10)<<fixed<<..#define rep(i, n) for (int i = 0; i < (int)(n); i++)#define repp(i, x, n) for (int i = x; i < (int)(n); i++)#define pii pair<int,int>#define pq priority_queue#define all(V) begin(V),end(V)#define printpair(p) cout<<p.first<<" "<<p.second<<"\n"#define tple tuple<int,int,int>template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }#define mend(a,b) G[a].push_back(b)struct unionFind {vector<int> par;vector<int> siz;vector<int> cnt;unionFind(int N): par(N+1), siz(N+1, 1), cnt(N+1, 0) {for (int i=0; i<=N; i++) par.at(i) = i;}int root(int x) {while (par.at(x) != x) {par.at(x) = par.at(par.at(x));x = par.at(x);}return x;}bool merge(int x, int y) {x = root(x), y = root(y);if (x == y){cnt.at(x)++;return false;}if (siz.at(x) < siz.at(y)) swap(x, y);siz.at(x) += siz.at(y);cnt.at(x) += cnt.at(y);par.at(y) = x;cnt.at(x)++;return true;}bool same(int x, int y) {return root(x) == root(y);}int size(int x) {return siz.at(root(x));}};signed main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n,m;cin>>n>>m;vec<int> C(n+1);vec Cv(n+1,vec<int>());rep(i,n){cin>>C[i+1];Cv[C[i+1]].push_back(i+1);}unionFind Z(n+1);vec<int> rooot(n+1,-1);rep(i,m){int a,b;cin>>a>>b;if(C[a]==C[b]){Z.merge(a,b);rooot[C[a]]=Z.root(a);}}int ans=0;for(int i=1;i<=n;i++){if(Cv[i].size()<=1){continue;}for(int v:Cv[i]){if(rooot[i]==-1){rooot[i]=v;continue;}if(Z.root(v)!=rooot[i]){Z.merge(rooot[i],v);ans++;}}}cout<<ans<<"\n";}