您的位置:首页 > 其它

CodeForces - 731C Socks(并查集)(贪心)

2017-03-12 15:59 429 查看
Socks

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 enumerat
4000ed. 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 kcolors.

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 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.

Examples

input
3 2 3
1 2 3
1 2
2 3


output
2


input
3 2 21 1 21 22 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.

题意:有n个袜子,要穿m天,共有k个颜色,袜子的颜色不一定相同,且每天穿的袜子的序号已经确定,问要使每天穿的袜子的颜色相同,最少要给多少个袜子染色

思路:因为每天穿的袜子的颜色必须相同,所以可以用并查集将每天穿的两个袜子加入同一个集合,每个集合中的颜色要统一,所以每一个集合最少的染色方案就是集合中元素的总数量-颜色出现次数最多的那种颜色的次数

代码:
#include<stdio.h>
#include<math.h>
#include<vector>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define maxn 200000+10#define LL long long int
const int inf=0x3f3f3f3f;
int a[maxn],pre[maxn];
vector<int>v[maxn];
int n,m,k;

void init()
{
for(int i=1; i<=n; i++)
pre[i]=i;
}

int Find(int x)
{
if(x==pre[x])
return x;
pre[x]=Find(pre[x]);
return pre[x];
}

void join(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
{
pre[fy]=fx;
}
}

int main()
{
scanf("%d%d%d",&n,&m,&k);
init();
int i,j,x,y;
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
for(i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
join(x,y);
}
for(i=1;i<=n;i++)//通过vector数组来确定每一个集合中的元素所对应的颜色
v[Find(i)].push_back(a[i]);
int ans=0;
for(i=1;i<=n;i++)
{
if(v[i].size()<=1)
continue;
map<int,int>mp;
int maxx=0;
for(j=0;j<v[i].size();j++)//遍历集合中的颜色,找出出现次数最多的颜色
{
mp[v[i][j]]++;
maxx=max(maxx,mp[v[i][j]]);
}
ans+=v[i].size()-maxx;
}
printf("%d\n",ans);
return 0;
}


思路问题:
一开始只觉得每改变一个袜子的颜色,那么就可能会出现多种情况,非常麻烦,并没有想到要用并查集,
后来看到提示要用并查集,也并没有想到集合中的颜色要统一这种关键性的思路
再后来看了下别人的思路,写了个n*n的遍历集合的方法,果断超时了0.0最后看了下别人的代码,才知道可以用vector先存储每个集合中的元素,然后再遍历


总结:
学到了vector存储并查集元素的方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: