Codeforces731C(SummerTrainingDay06-M 并查集)
C. Socks
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 kcolors.
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.
//2017-08-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map> using namespace std; const int N = ;
int color[N], n, m, k, root[N], tot;
struct Node{
int id, fa;
bool operator<(const Node X) const{
return fa < X.fa;
}
}node[N]; void init(){
for(int i = ; i <= N; i++){
node[i].fa = i;
node[i].id = i;
}
} int getfa(int x){
if(node[x].fa == x)return x;
return node[x].fa = getfa(node[x].fa);
} void merge(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf)
node[bf].fa = af;
} int main()
{
//freopen("inputN2.txt", "r", stdin);
while(scanf("%d%d%d", &n, &m, &k)!=EOF){
for(int i = ; i <= n; i++)
scanf("%d", &color[i]);
init();
int l, r;
for(int i = ; i < m; i++){
scanf("%d%d", &l, &r);
merge(l, r);
}
for(int i = ; i <= n; i++)
getfa(i);
sort(node+, node+n+);
int ans = , maxcolor, cnt, ptr = ;
for(int i = ; i <= n;){
map<int, int> book;
maxcolor = ;
cnt = ;
while(node[ptr].fa == node[i].fa){
book[color[node[i].id]]++;
maxcolor = max(maxcolor, book[color[node[i].id]]);
i++;
cnt++;
}
ans += cnt-maxcolor;
ptr = i;
}
printf("%d\n", ans);
} return ;
}
Codeforces731C(SummerTrainingDay06-M 并查集)的更多相关文章
- CodeForces731-C.Socks-并查集
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- Angular2入门体验
好的工具往往使得开发应用更快更简单,而不是任何模块都手动开发.angualr cli就是一个支持 创建工程,添加文件,对各种任务如测试.打包.部署等迭代. 本文的目的就是基于TypeScript开发构 ...
- Oracle11g手动创建数据库方法
Oracle11g手动创建数据库方法 参考网页http://www.th7.cn/db/Oracle/201311/36926.shtml 安装路径 我的安装路径是:E:\app\admin\prod ...
- javascript 模块化学习:Class打包出来无法实例化问题
菜鸟初学前端,第一次尝试使用webpack, 目的是做一个lib,想用webpack + babel 自动化打包出来 es5 的js代码 模块中用到了Class,直接export{className} ...
- pringboot+mybatis+redis+cookie单点登录
一.基本思路 单点sso用于多系统分布式,当多个系统分布式部署后,当然需要统一的登录接口.sso应运而生. 可以想见,单点应该是提供一个服务给其他系统,当其他系统需要验证登录状态的时候,调用服务,就可 ...
- centos7搭建mysql5.7主从同步
主从基本概念 mysql主从同步定义 主从同步使得数据可以从一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave).因为复制是 ...
- 几种int类型的范围
我们在编程的过程经常会遇到数据溢出的情况,于是这个时候我们必须定义能表示更大的数的数据类型来表示这个数. 下面列出了int型的范围: unsigned int 0-4294967295 ...
- 微信公众平台主动推送消息(asp.net)
/// <summary> /// MD5 32位加密 /// </summary> /// <param name=" ...
- netty客户端源码
随笔记录. //创建一个ChannelFactory(客户端代码) ChannelFactory factory = new NioClientSocketChannelFactory( Execut ...
- Selenium自动化测试Python五:WebDriver设计模式
WebDriver 设计模式 欢迎阅读WebDriver进阶讲义.本篇讲义将会重点介绍Selenium WebDriver 自动化框架的设计,着重使用Page Object设计模式,以及使用HTML测 ...
- 全网最详细的CentOS7里安装MySQL时出现No package mysql-server available错误的解决办法(图文详解)
不多说,直接上干货! 直接yum install mysql的话会报错,原因在于yum安装库里没有直接可以用的安装包,此时需要用到MariaDB了,MariaDB是MySQL社区开发的分支,也是一个增 ...