結果

問題 No.2563 色ごとのグループ
ユーザー 4O44O4
提出日時 2023-12-02 15:19:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 240 ms / 2,000 ms
コード長 1,822 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 178,148 KB
実行使用メモリ 25,212 KB
最終ジャッジ日時 2023-12-02 15:19:59
合計ジャッジ時間 6,230 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 1 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 1 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 3 ms
6,548 KB
testcase_17 AC 3 ms
6,548 KB
testcase_18 AC 3 ms
6,548 KB
testcase_19 AC 5 ms
6,548 KB
testcase_20 AC 14 ms
6,548 KB
testcase_21 AC 9 ms
6,548 KB
testcase_22 AC 8 ms
6,548 KB
testcase_23 AC 13 ms
6,548 KB
testcase_24 AC 117 ms
11,780 KB
testcase_25 AC 98 ms
13,124 KB
testcase_26 AC 138 ms
18,048 KB
testcase_27 AC 125 ms
24,160 KB
testcase_28 AC 158 ms
19,760 KB
testcase_29 AC 213 ms
25,212 KB
testcase_30 AC 240 ms
25,212 KB
testcase_31 AC 214 ms
25,212 KB
testcase_32 AC 212 ms
25,212 KB
testcase_33 AC 212 ms
17,404 KB
testcase_34 AC 206 ms
17,404 KB
testcase_35 AC 207 ms
17,404 KB
testcase_36 AC 239 ms
17,404 KB
testcase_37 AC 212 ms
17,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0