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. 内核信号处理 & CPU8个通用寄存器

    内核信号处理参考: http://www.spongeliu.com/165.html 信号本质上是在软件层次上对中断机制的一种模拟(注意区分中断.异常.信号),其主要有以下几种来源: 程序错误:除零 ...

  2. 一个JS多个数组取交集算法

    如题,多个数组中取交集(共同拥有元素),思路取第一个数组去跟每个数组中的元素对比,同时比较数据类型有救返回没有就返回null. 下面介绍到的算法数据格式是二维数组如: const parentArra ...

  3. Android 录制屏幕的实现方法

    Android 录制屏幕的实现方法 Chrome   2017-02-15 15:32:01 发布 您的评价:       5.0   收藏     0收藏 长久以来,我一直希望能够直接从Androi ...

  4. shell 例程 —— 解决redis读取稳定性

    问题背景: php读取线上redis数据,常常不稳定,数据响应时有时无. 解决方法:多次读取.每次读取全部上一次没读出的数据,直到全部获取. 本文实现用shell进行多次redis数据读取, 每次取出 ...

  5. SOJ.Output the Yanghui triangel

    Output the Yanghui triangel     总提交数量: 225 通过数量: 59                 时间限制:1秒    内存限制:256兆 题目描写叙述 Writ ...

  6. MVC异常过滤器 (错误页)

    控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  7. node npm Bower

    Download | Node.js https://nodejs.org/en/download/ wget https://nodejs.org/dist/v8.11.3/node-v8.11.3 ...

  8. ALSA声卡驱动中的DAPM详解之二:widget-具备路径和电源管理信息的kcontrol

    上一篇文章中,我们介绍了音频驱动中对基本控制单元的封装:kcontrol.利用kcontrol,我们可以完成对音频系统中的mixer,mux,音量控制,音效控制,以及各种开关量的控制,通过对各种kco ...

  9. C++11 function使用

    function是一组函数对象包装类的模板,实现了一个泛型的回调机制. 引入头文件 #include <functional>using namespace std;using names ...

  10. Factstone Benchmark(数学)

    http://poj.org/problem?id=2661 题意:Amtel在1960年发行了4位计算机,并实行每十年位数翻一番的策略,将最大整数n作为改变的等级,其中n!表示计算机的无符号整数(n ...