結果

問題 No.2563 色ごとのグループ
ユーザー forest3forest3
提出日時 2024-01-07 16:41:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,056 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 174,820 KB
実行使用メモリ 13,516 KB
最終ジャッジ日時 2024-01-07 16:41:58
合計ジャッジ時間 6,853 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 21 ms
6,676 KB
testcase_21 AC 8 ms
6,676 KB
testcase_22 AC 8 ms
6,676 KB
testcase_23 AC 17 ms
6,676 KB
testcase_24 AC 112 ms
7,296 KB
testcase_25 AC 93 ms
7,936 KB
testcase_26 AC 130 ms
10,192 KB
testcase_27 AC 114 ms
13,072 KB
testcase_28 AC 150 ms
11,056 KB
testcase_29 AC 207 ms
13,516 KB
testcase_30 AC 204 ms
13,516 KB
testcase_31 AC 233 ms
13,516 KB
testcase_32 AC 206 ms
13,516 KB
testcase_33 AC 195 ms
10,536 KB
testcase_34 AC 189 ms
10,536 KB
testcase_35 AC 189 ms
10,536 KB
testcase_36 AC 218 ms
10,536 KB
testcase_37 AC 195 ms
10,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for( ll i = 0; i < n; i++ )
using ll = long long;
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main() {
	int N, M;
	cin >> N >> M;
	vector<int> c( N );
	vector<vector<int>> vc( N );
	rep(i, N) {
		cin >> c[i];
		c[i]--;
		vc[c[i]].push_back( i );
	}
	UnionFind uf( N );
	rep(i, M) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		if(c[a] == c[b]) uf.unionSet(a, b);
	}
	int ans = 0;
	rep(i, N) {
		if(vc[i].size() < 2) continue;
		for( int e : vc[i] ) {
			if(uf.findSet(vc[i][0], e)) continue;
			ans++;
			uf.unionSet(vc[i][0], e);
		}
	}
	cout << ans << endl;
}
0