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. XJOI NOIP模拟题2

    第一题 组合计数 分析: 从前往后一位一位的计算 先算第一位比t小的数目,再算第一位与t[1]相同,第2位比t小的个数以此类推 先预处理一个数组h,h[i]表示从1~it串与s串不同的位数 对于第i位 ...

  2. 【bzoj1180】[CROATIAN2009]OTOCI LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes”,并且在结点 ...

  3. 数据结构—栈(Stack)

    栈的定义--Stack 栈是只允许在末端进行插入和删除的线性表.栈具有后进先出的特性(LIFO ,Last In Fast Out). 学过数据结构的人都知道:栈可以用两种方式来实现,一种方法是用数组 ...

  4. [洛谷P4208][JSOI2008]最小生成树计数

    题目大意:有$n$个点和$m$条边(最多有$10$条边边权相同),求最小生成树个数 题解:对于所有最小生成树,每种边权的边数是一样的.于是就可以求出每种边权在最小生成树中的个数,枚举这种边的边集,求出 ...

  5. 2018-2-6考试(COCI2014/2015 Contest#5)

    T1:FUNGHI(1s,32M,50pts)得分:50 题意:给你8个数组成一个环,要你求出其中连续的4个数,让它们的和最大 题解:暴力求出每一连续4个数之和,比较一下就好 标签:模拟 C++ Co ...

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

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

  7. Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路

    首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...

  8. vector 搜索

    http://classfoo.com/ccby/article/cIBahI #include <iostream> #include <algorithm> #includ ...

  9. 在xml文件中引入带有@Configuration的Java类

    在xml引入带有@Configuration的Java类,就是把这个带有@Configuration的Java类,当做一个普通的的类用<bean>标签引入: 核心代码如下: @Config ...

  10. javascript中top、clientTop、scrollTop、offsetTop的讲解

    下面结合各上图介绍一下各个属性的作用: 一.offsetTop属性: 此属性可以获取元素的上外缘距离最近采用定位父元素内壁的距离,如果父元素中没有采用定位的,则是获取上外边缘距离文档内壁的距离.所谓的 ...