結果

問題 No.2563 色ごとのグループ
ユーザー viral8viral8
提出日時 2023-12-02 15:03:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 839 ms / 2,000 ms
コード長 2,114 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 78,480 KB
実行使用メモリ 80,532 KB
最終ジャッジ日時 2023-12-02 15:04:06
合計ジャッジ時間 16,261 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
54,104 KB
testcase_01 AC 55 ms
54,108 KB
testcase_02 AC 54 ms
54,116 KB
testcase_03 AC 54 ms
54,120 KB
testcase_04 AC 55 ms
54,096 KB
testcase_05 AC 55 ms
54,116 KB
testcase_06 AC 56 ms
54,104 KB
testcase_07 AC 55 ms
54,092 KB
testcase_08 AC 57 ms
54,120 KB
testcase_09 AC 59 ms
54,120 KB
testcase_10 AC 61 ms
54,080 KB
testcase_11 AC 58 ms
54,120 KB
testcase_12 AC 59 ms
54,092 KB
testcase_13 AC 57 ms
54,116 KB
testcase_14 AC 90 ms
55,180 KB
testcase_15 AC 114 ms
56,168 KB
testcase_16 AC 113 ms
56,040 KB
testcase_17 AC 112 ms
56,044 KB
testcase_18 AC 72 ms
55,160 KB
testcase_19 AC 144 ms
58,756 KB
testcase_20 AC 185 ms
59,384 KB
testcase_21 AC 161 ms
59,156 KB
testcase_22 AC 157 ms
58,616 KB
testcase_23 AC 176 ms
59,348 KB
testcase_24 AC 537 ms
68,800 KB
testcase_25 AC 480 ms
69,344 KB
testcase_26 AC 633 ms
73,796 KB
testcase_27 AC 630 ms
80,076 KB
testcase_28 AC 677 ms
74,640 KB
testcase_29 AC 811 ms
80,480 KB
testcase_30 AC 827 ms
80,216 KB
testcase_31 AC 780 ms
80,532 KB
testcase_32 AC 839 ms
80,504 KB
testcase_33 AC 773 ms
80,368 KB
testcase_34 AC 781 ms
80,512 KB
testcase_35 AC 743 ms
79,560 KB
testcase_36 AC 744 ms
80,468 KB
testcase_37 AC 721 ms
80,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
class Main{
	private static final BufferedReader br =
		new BufferedReader(new InputStreamReader(System.in));
	private static final PrintWriter out =
		new PrintWriter(System.out);
	public static void main(String[] args)throws IOException{

		String[] str = br.readLine().split(" ");
		int N = Integer.parseInt(str[0]);
		int M = Integer.parseInt(str[1]);
		int[] C = new int[N];
		str = br.readLine().split(" ");
		for(int i=0;i<N;i++)
			C[i] = Integer.parseInt(str[i]);
		HashMap<Integer,int[]> map = new HashMap<>();
		for(int i=0;i<N;i++){
			if(map.containsKey(C[i]))
				map.get(C[i])[1]++;
			else
				map.put(C[i],new int[]{0,1});
		}
		UnionFind uf = new UnionFind(N+1);
		while(M-->0){
			str = br.readLine().split(" ");
			int u = Integer.parseInt(str[0]);
			int v = Integer.parseInt(str[1]);
			if(C[u-1]==C[v-1])
				if(uf.unite(u,v))
					map.get(C[u-1])[0]++;
		}
		int ans = 0;
		for(int[] temp:map.values())
			ans += temp[1]-temp[0]-1;
		out.println(ans);

		br.close();
		out.close();
	}
}
final class UnionFind {
	private final int[] par, rank, size, path;
	private int count;

	public UnionFind ( final int N ) {
		count = N;
		par = new int[N];
		rank = new int[N];
		size = new int[N];
		path = new int[N];
		Arrays.fill( par, -1 );
		Arrays.fill( size, 1 );
	}

	public int root ( final int ind ) {
		if ( par[ind] == -1 ) {
			return ind;
		}
		else {
			return par[ind] = root( par[ind] );
		}
	}

	public boolean isSame ( final int x, final int y ) {
		return root( x ) == root( y );
	}

	public boolean unite ( final int x, final int y ) {
		int rx = root( x );
		int ry = root( y );
		++path[rx];
		if ( rx == ry ) {
			return false;
		}
		if ( rank[rx] < rank[ry] ) {
			int temp = rx;
			rx = ry;
			ry = temp;
		}
		par[ry] = rx;
		if ( rank[rx] == rank[ry] ) {
			++rank[rx];
		}
		path[rx] += path[ry];
		size[rx] += size[ry];
		--count;
		return true;
	}

	public int groupCount () {
		return count;
	}

	public int pathCount ( final int x ) {
		return path[root( x )];
	}

	public int size ( final int x ) {
		return size[root( x )];
	}
}
0