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距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- PICE(3):CassandraStreaming - gRPC-CQL Service
在上一篇博文里我们介绍了通过gRPC实现JDBC数据库的streaming,这篇我们介绍关于cassandra的streaming实现方式.如果我们需要从一个未部署cassandra的节点或终端上读取 ...
- flask~数据库
flask与数据库的连接基于flaks_sqlaichemy 扩展 首先要连接数据库的时候必须得先下载 pip install flask-sqlalchemy 这个扩展 flask框架与数据库的连接 ...
- postgresql-pgbench(转)
pgbench测试: pg9.6.2的pgbench报错: [thunisoft@localhost ~]$ pgbench -S -c 8 -t 60 pgbenchdb Segmentatio ...
- 文件压缩小项目haffman压缩
文件压缩的原理: 文件压缩总体可以分为有损压缩和无损压缩两类,有损压缩是指对mp3等格式的文件,忽略一些无关紧要的信息,只保留一些关键的信息,但并不因此影响用户对于这些mp3格式文件的体验度,无损压缩 ...
- 浅析XSS与CSRF
浅析XSS与CSRF 在 Web 安全方面,XSS 与 CSRF 可以说是老生常谈了. XSS XSS,即 cross site script,跨站脚本攻击,缩写原本为 CSS,但为了和层叠样式表(C ...
- 最短路变形 poj3615& poj2263
问题: 牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍. 输入N代表站点数,标记为1—N,输入M代表路径数,从站点S到E之间需要跨过高度为H的障碍. 输入T代表牛要 ...
- GCC C语言 DLL范例,含源码
作者:小白救星 编译:gcc -c -DBUILDING_HZ_DLL1 hzdll1.c gcc -shared -o hzdll1.dll hzdll1.o -Wl,--kil ...
- 基于GTK+3 开发远程控制管理软件(C语言实现)系列三 Windows7开发环境搭建
一.介绍与准备 考虑到目标客户端基本都是windows客户端,所以此次开发环境将搭建在windows7下,相关开发工具有Msys2 和 Eclipse For C/C++ Msys2 是msys的升级 ...
- (转)Python3之os模块
原文:https://www.cnblogs.com/wang-yc/p/5623981.html 一:简介 os模块主要用于提供系统高级别的操作. 二:常用方法 1 2 3 4 5 6 7 8 9 ...
- Mac 安装 Ruby, Rails 运行环境
对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境. 次安装方法同样适用于产品环境! ...