問題一覧 > 通常問題

No.2563 Groups by Color

レベル : / AI推定難易度 : 750 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 204
作問者 : kusirakusira / テスター : deuteridayo 👑 AngrySadEight Kyo_s_s Magentor DeltaStruct 👑 loop0919 rotti_coder ragna マベマス(mavemas_413) けんぴん aki
お気に入りにしたユーザー ProblemId : 8680 / 自分の提出
問題文最終更新日: 2023-11-30 18:45:16
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
第2回緑以下コンテスト (順位表) の他の問題:

Problem Statement

You are given a simple undirected graph with $N$ vertices and $M$ edges, whose vertices are numbered from $1$ to $N$ and whose edges are numbered from $1$ to $M$.
The $i$-th edge $(1 \leq i \leq M)$ connects vertex $u_i$ and vertex $v_i$.
Also, the $i$-th vertex $(1 \leq i \leq N)$ is painted with color $C_i $.

Kushira-kun can perform the following operation on this graph $0$ or more times.

  • Choose a pair of integers $(a,b)$ satisfying $1 \leq a,b \leq N $, and add an edge connecting vertex $a$ and vertex $b$.

Kushira-kun's goal is to perform operations so that this graph satisfies the following condition.

  • For every pair of vertices $(i, j)$ with $C_i = C_j$, vertex $j$ can be reached from vertex $i$ by traversing only vertices of color $C_i$.
Find the minimum number of operations Kushira-kun needs to perform to achieve the goal.

Constraints

  • $ 1 \leq N \leq 2×10^5$
  • $ 0 \leq M \leq \mathrm{min} (2×10^5, N(N-1)/2)$
  • $ 1 \leq C_i \leq N$
  • $ 1 \leq u_i, v_i \leq N $
  • $ u_i \neq v_i $
  • $ i≠j \Rightarrow \{u_i, v_i\} \neq \{u_j, v_j\} $
  • All inputs are integers

Input

$N\ M$
$C_1\ C_2\ \ldots\ C_N$
$u_1\ v_1$
$u_2\ v_2$
$\vdots$
$u_M\ v_M$

Output

Output the answer.

Sample

Sample 1
Input
5 6
1 2 2 2 1
1 2
1 3
1 4
3 4
4 5
2 5
Output
2

It suffices to add edges between vertices $1-5$ and between vertices $2-3$.

Sample 2
Input
4 2
1 1 2 4
1 2
1 3
Output
0

It may be that no operations are needed.

Sample 3
Input
8 9
1 8 1 2 1 3 3 2
6 3
3 2
4 3
5 7
7 3
2 7
2 1
1 4
4 7
Output
4

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。