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.

这道题比赛的时候理解错了。。。

题意:n双袜子,m天,每天穿一双袜子,在已知这m天每天穿的袜子的条件下,问至少改变多少次袜子的颜色,在改过颜色后,每天穿的两只袜子是同种颜色。。

思路:将可能在同一天穿的两只袜子放入一个集合,可以发现改变颜色后最终这个集合中的袜子的颜色是一样的(传递的关系)。所以,在每个集合中将袜子的颜色改为集合中最多的颜色即可。

dfs搜索,搜连通分支中最多的颜色:

#include<cstdio>
#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define N 200005 int sock[N];
int father[N];
int vis[N];
vector<int> v[N];
map<int ,int>mm;
int cnt=; void dfs(int x)
{
if(vis[x]==)
return;
vis[x]=;
mm[sock[x]]++;
cnt=max(cnt,mm[sock[x]]);
for(int i=;i<v[x].size();i++)
dfs(v[x][i]);
} int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
scanf("%d",&sock[i]);
for(int i=; i<m; i++)
{
int l,r;
scanf("%d%d",&l,&r);
v[l].push_back(r);
v[r].push_back(l);
}
int tmp=;
for(int i=; i<=n; i++)
{
if(vis[i]==)
{
cnt=;
mm.clear();
dfs(i);
tmp+=cnt;
}
}
printf("%d\n",n-tmp);
return ;
}

并查集:

#include<cstdio>
#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define N 200005 int sock[N];
int father[N];
int vis[N];
vector<int> v[N]; int Find(int x)
{
if(father[x]!=x)
father[x]=Find(father[x]);
return father[x];
} void Merge(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
father[fx]=fy;
} int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&sock[i]);
father[i]=i;
}
for(int i=; i<m; i++)
{
int l,r;
scanf("%d%d",&l,&r);
if(l!=r)
Merge(l,r);
}
for(int i=; i<=n; i++)
{
int fi=Find(i);
v[fi].push_back(sock[i]);
}
int res=;
for(int i=; i<=n; i++)
{ //cout<<mm[1]<<endl;
if(v[i].size()<=)
continue;
map<int ,int>mm;
int maxn=,se=;
for(int j=; j<v[i].size(); j++)
{
mm[v[i][j]]++;
maxn=max(maxn,mm[v[i][j]]);
se++;
}
res+=se-maxn;
}
printf("%d\n",res);
return ;
}

codeforces_731C_[dfs][并查集]的更多相关文章

  1. 分珠(dfs+并查集)

    1140 分珠 时间限制:500MS  内存限制:65536K提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...

  2. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

  3. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

  4. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  5. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  6. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  7. CF 115 A 【求树最大深度/DFS/并查集】

    CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...

  8. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

  9. ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

    Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB    ...

随机推荐

  1. Ubuntu 16.04设置开机关机时显示命令详细信息不显示进度条Logo

    1.编辑grub文件 sudo gedit /etc/default/grub 把 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 改成 GRU ...

  2. Ubuntu 16.04安装BleachBit清理系统垃圾文件

    在LInux下不同于Windows会产生系统垃圾,但是Linux会产生安装软件时的依赖包,比如卸载一个软件,其依赖包是不会删除的.但是可以通过以下命令解决: #清理旧版本的软件缓存 sudo apt- ...

  3. DATASNAP压缩过滤器的使用

    ZLIBCOMPRESSION FILTER 作为范例,我们使用已随D2010提供的DataSnap过滤器.可用于在客户端和服务端压缩数据流.这里说的ZlibCompression过滤器可以在DbxC ...

  4. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  5. Kafka无法消费!?究竟是bug的“沦陷”还是配置的“扭曲”?

    在一个月黑风高的夜晚,突然收到现网生产环境Kafka消息积压的告警,梦中惊醒啊,马上起来排查日志. 问题现象 消费请求卡死在查找Coordinator Coordinator为何物?Coordinat ...

  6. volatile限定符

    volatile关键字是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改,比如:操作系统.硬件或者其它线程等.由于访问寄存器的速度要快过RAM,所以编译器一般都会作减少存取外部RA ...

  7. mysql20170407代码实现

    今天用了一会儿sqlyog,感觉还是会让人懒惰,所以选择了ms-dos环境,感觉不错,原生态,敲着很爽; | test01 | | tree | | world | +---------------- ...

  8. XAML实例教程系列 - 资源(Resources)

    Kevin Fan分享开发经验,记录开发点滴 XAML实例教程系列 - 资源(Resources) 2012-08-10 12:47 by jv9, 1386 阅读, 1 评论, 收藏, 编辑 在Wi ...

  9. CodePlus 2017 12 月赛

    这场比赛跟个zz一样 div1卡在了同余方程上 心态崩了去做div2 然后被T1搞崩了 T1: 大模拟 比较像配平方程式 思路: 但是未知物质每种元素系数不能≥10 且不能为空 (如CO2+?=CO2 ...

  10. bzoj1098

    并查集+dfs 先开始想和不相连的点用并查集连起来,最后看每个连通块有多少个点就行了,但是这样是O(n*n)的,然而我并没有想到补图 其实就是求补图有多少连通块,因为补图中两个点有边,那么这两个点必须 ...