CodeForces731-C.Socks-并查集
2 seconds
256 megabytes
standard input
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.
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.
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.
3 2 3
1 2 3
1 2
2 3
2
3 2 2
1 1 2
1 2
2 1
0
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.
迷之题面。。。
大体题意就是一个人穿袜子,左脚的袜子,右脚的袜子,然后要穿颜色相同的袜子,不同的怎么办呢,染色,问最少染颜色的次数。
数据第一行三个数代表有几个袜子,穿几天,几种颜色,第二行是袜子颜色,下面几行就是这些天里每天走右脚穿的袜子。例如第一组数据,1 2就是第一天左脚1右脚2,2 3是第二天左脚2,右脚3,因为两天里都有2,所以第一天染1,第二天染3,所以为2。
并查集,找出集合中穿的颜色最多的,把剩下的都染色。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
int hh[N],f[N];
vector<int>p[N]; //类似一个动态数组p[N]
int Find(int x){ //查找根节点
if(f[x]==x)
return f[x];
else
return f[x]=Find(f[x]);
}
void gg(int a,int b){
int x=Find(a);
int y=Find(b);
if(x!=y) //压缩
f[x]=y;
}
int main(){
int n,m,k,l,r,ans=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%d",&hh[i]); //袜子颜色
f[i]=i;
}
for(int i=;i<=m;i++){
scanf("%d%d",&l,&r); //左右脚袜子
gg(l,r);
}
for(int i=;i<=n;i++)
p[Find(i)].push_back(hh[i]); //将颜色这个元素添加进去
for(int i=;i<=n;i++){
if(p[i].size()<=)continue;
int maxx=-;
map<int,int> mp;
mp.clear(); //清空
for(int j=;j<p[i].size();j++){
mp[p[i][j]]++;
maxx=max(maxx,mp[p[i][j]]);
}
ans+=p[i].size()-maxx;
}
printf("%d\n",ans);
return ;
}
(T▽T)
CodeForces731-C.Socks-并查集的更多相关文章
- CodeForces 731C C - Socks 并查集
Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存
http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- 并查集【CF731C】Socks
Description 有\(n\)只袜子,\(k\)种颜色,在\(m\)天中,问最少修改几只袜子的颜色,可以使每天要穿的袜子颜色相同. Input 第一行\(n,m,k\)分别对应题目描述. 接下来 ...
- CodeForces 731C Socks (DFS或并查集)
题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...
- Codeforces 731 C.Socks-并查集+STL(vector+map)
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces_731C_[dfs][并查集]
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
随机推荐
- Hive实际应用小结
1.简介 Hive是数据仓库平台,构建在Hadoop之上用来处理结构化数据.Hive是一个SQL解析引擎,能够将SQL语句转化成MapReduce作业并在Hadoop上执行,从而使得查询和分析更加方便 ...
- kvm 随笔
1. 查看kvm虚拟机状态 # virsh list --all 2. KVM虚拟机开机 # virsh start windows 3. KVM虚拟机关机或断电 (1) 关机 virsh关机 ...
- 免费SSL&付费SSL证书,该如何选择?
近年来Google.Apple.百度等公司不断推动 HTTPS 的普及,SSL 证书作为 HTTPS 安全协议的必备配置,自然也成为了网站.App 开发者最重要部署项目之一. 又拍云于 2016 年联 ...
- Mysql 锁基础
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/53 lock与latch 在数据库中,lock与latch都可以 ...
- GAME——转圈游戏
我们在生命的路上常常绝望 大概是因为弯路走了太多 脚上的泡被磨起又磨破 像我们所有的幻想与梦 起起落落. 所以说 我这道题考场上面和题解想得一模一样啊啊啊啊啊啊啊啊啊啊!!!!!! 但是就是打复杂了啊 ...
- bzoj 4569: [Scoi2016]萌萌哒
Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条 件表示为四个数,l1,r1,l2,r2,即两个长度相同的 ...
- shader 2 : use shaderToy in unity
shadertoy 原型,https://www.shadertoy.com/view/XslGRr 先说几个概念 Shader language目前有3种主流语言:基于OpenGL的GLSL(Ope ...
- K:正则表达式之基础简介
正则表达式(regular expression 简称regex) 是一种工具,和其它工具一样是为了解决某一类问题而发明的.正则表达式是一些用来匹配和处理文本的字符串.平时主要用于查找和替换符合相应模 ...
- ASP.NET网页发布以及相关问题的解决
今天做了一个统计站点的网页,想要发布一下,中间碰到不少问题,现在和大家分享一下! 这是我想要最终的网页结果: 1.发布站点到桌面(任意路径) 2.安装IIS 3.安装好后,打开IIS, ...
- bug运输[辽宁2014年省队互测一]
奇奇怪怪的题目,不知道他要我们干什么. 我们观察一波局势,发现答案最大不过5.因为如果答案是6或以上的话,我们就至少要2^(5*5)个5*5的方格. 仔细计算一波时间复杂度,再信仰一波,坚信暴力压正解 ...