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

并查集,将连通的放到一个集合中,然后将一个集合中的颜色换成颜色最多的呢一个

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int f[],co[],n,k,N,x,y;
vector<int>p[];
map<int,int>m;
int find(int x)
{
if(f[x]==x) return f[x];
return f[x]=find(f[x]);
}
int joi(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y) f[x]=y;
}
int main()
{
scanf("%d%d%d",&n,&k,&N);
for(int i=;i<=n;i++)
{
scanf("%d",&co[i]);
f[i]=i;
}
for(int i=;i<=k;i++)
{
scanf("%d%d",&x,&y);
joi(x,y);
}
for(int i=;i<=n;i++)
{
p[find(i)].push_back(co[i]);
}
int ans=;
for(int i=;i<=n;i++)
{
if(p[i].size()<=) continue;
int maxn=-;
m.clear();
for(int j=;j<p[i].size();j++)
{
m[p[i][j]]++;
maxn=max(maxn,m[p[i][j]]);
}
ans+=p[i].size()-maxn;
}
printf("%d\n",ans);
}

Codefroces 731C. Socks的更多相关文章

  1. Codeforces 731C. Socks 联通块

    C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...

  2. CodeForces 731C Socks

    http://codeforces.com/problemset/problem/731/C 并查集+贪心 将要求颜色相同的袜子序号放入一个集合中 贪心:然后统计这个集合中出现次数最多但颜色 可以得到 ...

  3. Codeforces 731C Socks 并查集

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

  4. CodeForces 731C Socks (DFS或并查集)

    题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...

  5. CodeForces 731C C - Socks 并查集

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

  6. 【25.23%】【codeforces 731C】Socks

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  8. iphone使用mac上的SOCKS代理

    Step 1. Make sure the SOCKS tunnel on your work computer allows LAN connections so your iPhone/iPod ...

  9. CF731C. Socks[DFS 贪心]

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

随机推荐

  1. 使用vuex的流程随笔

    1.在建好的vue项目中新建一个vuex文件夹在此文件夹下建一个index.js文件,在此文件下引入vuex 模块(当然需要先npm下载)和vue模块,在引入你所有的自定义的module.js模块(下 ...

  2. HDU 3108 Ant Trip

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. Android控件篇

    Android中提供了丰富的UI空间.为了最大限度地发挥平台的性能.每个开发人员必须熟练掌握UI控件尤其是经常使用的UI控件.并能依据须要呈现的内容选择最恰当的控件. Android提供了XML配置和 ...

  4. jmeter名词解释之时间(Elapsed Time/ Latency Time/Connection Time)

    转载时请标注源自:http://blog.csdn.net/musen518 jmeter报告结果中会出现三个时间 1. Elapsed time    经过的时间(= Sample time = L ...

  5. Logical Operators (Transact-SQL)

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/logical-operators-transact-sql Logical ...

  6. PostgreSQL Replication之第八章 与pgbouncer一起工作(5)

    8.5 维护 pgbouncer 除了我们在本章已经说明的,pgbouncer有一个很好的能够执行基本管理和监控任务的交互式管理界面. 它是如何工作的呢?pgbouncer提供给您一个虚假的称为pgb ...

  7. 对OC中property的一点理解

    最近在看即将要加入的项目的代码,看到一个protocol里包含着几个property.之前没有写过类似的代码,看到这里的时候,突然疑惑了一下,发现自己对property的理解好像有点模糊.所以回家后又 ...

  8. 四 numpy操作数组输出图片

    一.读取一张图片,修改颜色通道后输出 # -*- coding=GBK -*- import cv2 as cv import numpy as np #numpy数组操作 def access_pi ...

  9. Hexo构建Blog系列

    Hexo是一个开源构建blog框架,基于nodejs研发.可以自由切换主题,插件等功能,实现自已酷炫博客需求. 下面是基于hexo实践所产出的一些心得,供大家参考. 基础 Hexo 搭建 Hexo 与 ...

  10. opencv——均值/中值滤波器去噪

    实验内容及实验原理: 1.用均值滤波器(即邻域平均法)去除图像中的噪声: 2.用中值滤波器去除图像中的噪声 3.比较两种方法的处理结果 实验步骤: 用原始图像lena.bmp或cameraman.bm ...