結果

問題 No.2563 色ごとのグループ
ユーザー 👑 SPD_9X2
提出日時 2024-02-03 01:07:10
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,631 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 2,593 ms
コンパイル使用メモリ 79,360 KB
実行使用メモリ 66,328 KB
最終ジャッジ日時 2024-09-28 10:43:31
合計ジャッジ時間 29,587 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.*;

public class Main {

    public static void vectorPrint(Vector lis){

        PrintWriter out = new PrintWriter(System.out);
        for (int i=0 ; i<lis.size() ; i++){
            out.print(lis.get(i));
            if ( i != lis.size()-1){
                out.print(' ');
            }else{
                out.print('\n');
            }
        }
        out.flush();
    }

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        int N = Integer.parseInt(sc.next());
        int M = Integer.parseInt(sc.next());

        int[] C = new int[N];
        for (int i=0; i<N ;i++){
            C[i] = Integer.parseInt(sc.next());
        }

        Dsu uf = new Dsu(N);
        for (int i=0; i<M ; i++){
            int u = Integer.parseInt(sc.next());
            int v = Integer.parseInt(sc.next());
            u--; 
            v--;
            if (C[u] == C[v]){
                uf.merge(u, v);
            }
        }

        Map<Integer,Integer> dic = new HashMap<>();

        for (int i=0; i<N ; i++){
            if (uf.leader(i) == i){
                if (!dic.containsKey(C[i])){
                    dic.put(C[i], 0);
                }
                dic.put(C[i], dic.get(C[i]) + 1);
            }
        }

        int ans = 0;
        for (Map.Entry<Integer,Integer> tup : dic.entrySet()){
            ans += tup.getValue();
            ans -= 1;
        }
        
        System.out.println(ans);
    }
}

class Dsu {
    int n;
    int[] p;
    int[] s;

    Dsu(int N) {
        this.n = N;
        this.p = new int[N];
        this.s = new int[N];
        for (int i=0; i<N ; i++){
            this.p[i] = i;
            this.s[i] = 1;
        }
    }

    public int leader(int a){
        Vector<Integer> visit = new Vector<Integer>();
        while (this.p[a] != a){
            visit.addElement(a);
            a = this.p[a];
        }
        for (Integer v: visit){
            this.p[v] = a;
        }
        return a;
    }

    public int merge(int a,int b){
        int pa = this.leader(a);
        int pb = this.leader(b);
        if (pa == pb){
            return pa;
        }else if (this.s[pa] >= this.s[pb]){
            this.s[pa] += this.s[pb];
            this.p[pb] = pa;
            return pa;
        }else{
            this.s[pb] += this.s[pa];
            this.p[pa] = pb;
            return pb;
        }
    }

    public Boolean same(int a, int b){
        int pa = this.leader(a);
        int pb = this.leader(b);
        return (pa == pb);
    }

    public int size(int a){
        int pa = this.leader(a);
        return this.s[pa];
    }
}
0