codeforces731C 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.
题目大意:
主人公每天必须穿规定序号的袜子,而他要穿的袜子颜色可能不同,所以需要将他们都染成同一颜色(颜色数为k),问最少要染多少次?
对于每天要求穿的两只袜子,它们的颜色必须相同,所以可以建一张图,将所有在同一天穿的两只袜子连接;
之后再进行遍历,遍历的同时统计颜色数,得到最大的颜色数和总的袜子数,相减即可得到最少的染色数.
所以答案就是每次的累加。
空间复杂度O(n),时间复杂度O(n)(因为对于每个点只经过一次)
#include<cstdio>#include<algorithm>#define N 200005using namespace std;struct X{ int u,v,f,n;}x[N<<1];int c[N],s,tong[N],q[N];bool vis[N];void add(int u,int v){ x[++s].u=u; x[s].v=v; x[s].n=x[u].f; x[u].f=s;}void dfs(int a){ q[++s]=a;//q数组记录这次所有走过的点 vis[a]=1;//vis表示这个点是否走过 for(int i=x[a].f;i;i=x[i].n) if(!vis[x[i].v]) dfs(x[i].v); }int main(){ int n,m,k,ans=0; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) scanf("%d",&c[i]); while(m--) { int u,v; scanf("%d%d",&u,&v); add(u,v);add(v,u); }//建图 for(int i=1;i<=n;i++) if(!vis[i]) { int mx=s=0; dfs(i); for(int j=1;j<=s;j++) mx=max(++tong[c[q[j]]],mx);//用桶记录出现最多的颜色 for(int j=1;j<=s;j++) tong[c[q[j]]]=0;//清空桶 ans+=s-mx; } printf("%d",ans); return 0;}
codeforces731C Socks的更多相关文章
- iphone使用mac上的SOCKS代理
Step 1. Make sure the SOCKS tunnel on your work computer allows LAN connections so your iPhone/iPod ...
- CF731C. Socks[DFS 贪心]
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- redsocks 将socks代理转换成全局代理
redsocks 需要手动下载编译.前置需求为libevent组件,当然gcc什么的肯定是必须的. 获取源码 git clone https://github.com/darkk/redsocks 安 ...
- 将 Tor socks 转换成 http 代理
你可以通过不同的 Tor 工具来使用 Tor 服务,如 Tor 浏览器.Foxyproxy 和其它东西,像 wget 和 aria2 这样的下载管理器不能直接使用 Tor socks 开始匿名下载,因 ...
- Mac Aria2 使用Privoxy将socks代理转化为http代理
安装Privoxy 打开终端安装privoxy来实现这里我是通过brew来进行的安装 brew install privoxy 看到这行已经安装成功 ==> Caveats To have la ...
- SOCKS 5协议详解(转)
笔者在实际学习中,由于在有些软件用到了socks5(如oicq,icq等),对其原理不甚了解,相信很多朋友对其也不是很了解,于是仔细研读了一下rfc1928,觉得有必要译出来供大家参考. 1.介绍: ...
- Codeforces 731C. Socks 联通块
C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...
- 使用ssh正向连接、反向连接、做socks代理的方法
ssh -L 219.143.16.157:58080:172.21.163.32:8080 用户名@localhost -p 10142 在 219.143.16.157机器执行 将ssh隧 ...
- CF460 A. Vasya and Socks
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- Appium 解决不能输入中文字符问题
只需在初始化driver方法时,写这两行代码即可: capabilities.setCapability("unicodeKeyboard", "True" ...
- VNC-Server安装及配置
一.什么是VNC? VNC (Virtual Network Computer)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC ...
- 深入C#中get与set的详解(转)
转自:http://www.jb51.net/article/37960.htm 释一:属性的访问器包含与获取(读取或计算)或设置(写)属性有关的可执行语句.访问器声明可以包含 get 访问器或 se ...
- JSBinding+SharpKit / 更新的原理
首先,其实不是热更新,而是更新. 热更新意思是不重启游戏,但只要你脚本里有存储数据,就不可能.所以只能叫更新. 但大家都这么说,所以... 先举个具体的例子: 如果是C#:在 Prefab 的 Gam ...
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 一对多的观察者模式机制有什么缺点? 想要查看 ...
- selenium下拉框选择
下拉框结构如下,我需要选择的是new: html为: <select id="condition_type" name="condition_type" ...
- .NET微信通过授权获取用户的基本信息
一.填写授权回调页面的域名 二.引导用户到指定的授权页面 例如:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID& ...
- python---字符编码
获取系统默认字符编码 在Python代码中,普通字符串的编码方式与程序源文件编码方式一致的,而很多IDE在默认情况下,将程序源文件按照系统默认字符编码来保存的. 下面给出用Python获取系统默认编码 ...
- Json数据处理
1.字符串转换为Json数组:取json对象属性值. String st="[{"tradeDate":"2016-09-27","trad ...
- mysql <-> sqlite
在做程序时,sqlite数据很方便.用mysql数据导出到sqlite的步骤:(用csv文件过渡) ------------------------------- 先导出到csv文件 ------ ...