codeforces731C Socks

C. Socks
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.

Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.

When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.

Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.

The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.

Input

The first line of input contains three integers nm and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.

The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.

Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ nli ≠ ri) — indices of socks which Arseniy should wear during the i-th day.

Output

Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.

Examples
input
3 2 3
1 2 3
1 2
2 3
output
2
input
3 2 2
1 1 2
1 2
2 1
output
0
Note

In the first sample, Arseniy can repaint the first and the third socks to the second color.

In the second sample, there is no need to change any colors.

题目大意:

主人公每天必须穿规定序号的袜子,而他要穿的袜子颜色可能不同,所以需要将他们都染成同一颜色(颜色数为k),问最少要染多少次?

对于每天要求穿的两只袜子,它们的颜色必须相同,所以可以建一张图,将所有在同一天穿的两只袜子连接;

之后再进行遍历,遍历的同时统计颜色数,得到最大的颜色数和总的袜子数,相减即可得到最少的染色数.

所以答案就是每次的累加。

空间复杂度O(n),时间复杂度O(n)(因为对于每个点只经过一次)

#include<cstdio>#include<algorithm>#define N 200005using namespace std;struct X{      int u,v,f,n;}x[N<<1];int c[N],s,tong[N],q[N];bool vis[N];void add(int u,int v){      x[++s].u=u;      x[s].v=v;      x[s].n=x[u].f;      x[u].f=s;}void dfs(int a){        q[++s]=a;//q数组记录这次所有走过的点        vis[a]=1;//vis表示这个点是否走过      for(int i=x[a].f;i;i=x[i].n)          if(!vis[x[i].v]) dfs(x[i].v);  }int main(){      int n,m,k,ans=0;      scanf("%d%d%d",&n,&m,&k);      for(int i=1;i<=n;i++) scanf("%d",&c[i]);      while(m--)      {                int u,v;            scanf("%d%d",&u,&v);            add(u,v);add(v,u);      }//建图      for(int i=1;i<=n;i++)            if(!vis[i])            {                  int mx=s=0;			      dfs(i);                  for(int j=1;j<=s;j++) mx=max(++tong[c[q[j]]],mx);//用桶记录出现最多的颜色                  for(int j=1;j<=s;j++) tong[c[q[j]]]=0;//清空桶                  ans+=s-mx;             }      printf("%d",ans);      return 0;}
上一篇:BZOJ2981 : [Poi2002]括号


下一篇:Python学习笔记四--字典与集合