結果

問題 No.2563 色ごとのグループ
ユーザー yumekawayuiyumekawayui
提出日時 2023-12-02 15:07:07
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,359 bytes
コンパイル時間 3,381 ms
コンパイル使用メモリ 187,388 KB
実行使用メモリ 20,016 KB
最終ジャッジ日時 2023-12-02 15:07:14
合計ジャッジ時間 5,955 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 3 ms
6,676 KB
testcase_20 AC 7 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 AC 5 ms
6,676 KB
testcase_24 AC 45 ms
9,728 KB
testcase_25 AC 36 ms
10,752 KB
testcase_26 AC 57 ms
14,516 KB
testcase_27 AC 60 ms
19,108 KB
testcase_28 AC 65 ms
15,756 KB
testcase_29 AC 86 ms
20,016 KB
testcase_30 AC 90 ms
20,016 KB
testcase_31 AC 91 ms
20,016 KB
testcase_32 AC 87 ms
20,016 KB
testcase_33 AC 79 ms
17,488 KB
testcase_34 AC 80 ms
17,488 KB
testcase_35 AC 83 ms
17,488 KB
testcase_36 AC 83 ms
17,440 KB
testcase_37 AC 84 ms
17,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
    

}
0