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

迷之题面。。。

大体题意就是一个人穿袜子,左脚的袜子,右脚的袜子,然后要穿颜色相同的袜子,不同的怎么办呢,染色,问最少染颜色的次数。

数据第一行三个数代表有几个袜子,穿几天,几种颜色,第二行是袜子颜色,下面几行就是这些天里每天走右脚穿的袜子。例如第一组数据,1 2就是第一天左脚1右脚2,2 3是第二天左脚2,右脚3,因为两天里都有2,所以第一天染1,第二天染3,所以为2。

并查集,找出集合中穿的颜色最多的,把剩下的都染色。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
int hh[N],f[N];
vector<int>p[N]; //类似一个动态数组p[N]
int Find(int x){ //查找根节点
if(f[x]==x)
return f[x];
else
return f[x]=Find(f[x]);
}
void gg(int a,int b){
int x=Find(a);
int y=Find(b);
if(x!=y) //压缩
f[x]=y;
}
int main(){
int n,m,k,l,r,ans=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%d",&hh[i]); //袜子颜色
f[i]=i;
}
for(int i=;i<=m;i++){
scanf("%d%d",&l,&r); //左右脚袜子
gg(l,r);
}
for(int i=;i<=n;i++)
p[Find(i)].push_back(hh[i]); //将颜色这个元素添加进去
for(int i=;i<=n;i++){
if(p[i].size()<=)continue;
int maxx=-;
map<int,int> mp;
mp.clear(); //清空
for(int j=;j<p[i].size();j++){
mp[p[i][j]]++;
maxx=max(maxx,mp[p[i][j]]);
}
ans+=p[i].size()-maxx;
}
printf("%d\n",ans);
return ;
}

(T▽T)

Codeforces 731 C.Socks-并查集+STL(vector+map)的更多相关文章

  1. CodeForces 731C C - Socks 并查集

    Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...

  2. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  3. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  4. codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)

    题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...

  5. CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存

    http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...

  6. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  7. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  8. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  9. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

随机推荐

  1. RegExp & bug

    RegExp & bug translated bug // OK && tranlate `/` let new_obj_reg = new RegExp(`^(([^< ...

  2. 【bzoj1018】[SHOI2008]堵塞的交通traffic 线段树区间合并+STL-set

    题目描述 给出一张2*n的网格图,初始每条边都是不连通的.多次改变一条边的连通性或询问两个点是否连通. 输入 第一行只有一个整数C,表示网格的列数.接下来若干行,每行为一条交通信息,以单独的一行“Ex ...

  3. Powershell快速入门

    Powershell快速入门 来源: https://blog.csdn.net/u011054333/article/details/72567590 https://blog.csdn.net/u ...

  4. BZOJ 2502: 清理雪道 | 有上下界最小流

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  5. 大学本科毕业论文——LanguageTool语法校正规则库的开发

    原创率超高的毕业论文,基本没有太多抄袭的东西,论述观点完全是1年半前的我的想法,或许bug很多,仅作发布参考,不作讨论. 参考预览图: 只读pdf版本下载地址: http://download.csd ...

  6. 如何开始创建第一个基于Spring MVC的Controller

    万事开头难,良好的开端是成功的一半! 以下示例怎么开始创建我们的第一个Spring MVC控制器Controller 1.新建一个java类,命名为:MyFirstController,包含以下代码, ...

  7. 第116讲 boost::algorithm::string之替换和删除

    http://www.360doc.com/content/16/0523/18/29304643_561672752.shtml

  8. 通用adapter

    http://blog.csdn.net/lmj623565791/article/details/38902805/

  9. sublime2创建一个html5的snippets文件

    背景:跟了一个网上课程,老师哗啦啦敲代码,屏幕上只敲了几个字,键盘一操作,瞬间一大溜代码,看得我心惊肉跳连忙暂停抄抄抄. 举个简单的例子,我需要创建一个html文件.但是我不想每次都敲固定的格式.那么 ...

  10. 新手如何更换自己喜欢的背景以及此背景的css码

    以下内容为转载(对于css码可以自己写当然也可以去网上搜现成的): 更换背景教学:https://jingyan.baidu.com/album/fc07f9897c730412ffe519c0.ht ...