Description
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 mdays.
Input
The first line of input contains three integers n, m 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 ≤ n, li ≠ 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.
Sample Input
3 2 3 1 2 3 1 2 2 3
2
3 2 2 1 1 2 1 2 2 1
0
Hint
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.
这道题让求需要染色的袜子的最小的数量,把需要颜色相同的袜子放到同一个集合里面,其实就是同一天的袜子放到同一个集合里面,让这个集合里面的袜子颜色相同,可能有多个集合,单独处理每个集合,然会最后每个集合的值进行想加,可以用并查集来实现,颜色相同的袜子放到同一个集合里面
这个一开始有点蒙,把自己给绕进去了,因为需要染色的是袜子的数量,一开始想复杂了,记录了每一只袜子出现的次数,同一种颜色的袜子出现的次数,然后求出出现颜色次数最多的那个颜色
#include<bits/stdc++.h>
#define INF 99999999
#define maxn 200002
using namespace std;
int n,m,k;
int f[maxn];
int color[maxn];//记录每一只袜子的颜色
map<int,map<int,int> > mp;//可以理解成一个二维数组
int getf(int i) {
if(i!=f[i])
f[i]=getf(f[i]);
return f[i];
}
int merge(int a, int b) {
int i=getf(a);
int j=getf(b);
if(i==j) return 0;
else {
f[j]=i;
return 1;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;++i) {
scanf("%d",&color[i]);
f[i]=i;
}
while(m--) {
int a,b;
scanf("%d%d",&a,&b);
merge(a,b);//这里合并的是第a只袜子和第b只袜子,而不是a和b袜子的颜色
}
for(int i=1;i<=n;++i) {
++mp[getf(i)][color[i]];//这句话实际上就已经解决了分堆问题,并且没有选中的袜子不会进去到任何一个集合里面
}
int ans=0;
for(map<int,map<int,int> >::iterator i=mp.begin();i!=mp.end();++i) {
int mx=0,sum=0;
for(map<int,int>::iterator j = i->second.begin();j!=i->second.end();++j) {
mx=max(mx,j->second);//选出出现次数最多的颜色
sum+=j->second;//记录该集合里面的一共穿了多少袜子,同一只袜子可以多次包含
}
ans+=sum-mx;//出现的所有的袜子减去颜色最多的袜子,剩余的就是需要染色的袜子的最小的数量
}
printf("%d\n",ans);
return 0;
}
/*
4 4 4
1 2 3 4
1 2
1 3
2 3
2 4
*/