Codefroces 731C. 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.
并查集,将连通的放到一个集合中,然后将一个集合中的颜色换成颜色最多的呢一个
#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的更多相关文章
- Codeforces 731C. Socks 联通块
C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...
- CodeForces 731C Socks
http://codeforces.com/problemset/problem/731/C 并查集+贪心 将要求颜色相同的袜子序号放入一个集合中 贪心:然后统计这个集合中出现次数最多但颜色 可以得到 ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- CodeForces 731C Socks (DFS或并查集)
题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...
- CodeForces 731C C - Socks 并查集
Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...
- 【25.23%】【codeforces 731C】Socks
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- 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 ...
随机推荐
- Failed to initialize connector [Connector[HTTP/1.1-443]]
Failed to initialize connector [Connector[HTTP/1.1-443]] 出现如上错误时,是因为443端口被占用, 所以tomcat的https协议无法使用, ...
- 洛谷 P2734 游戏 A Game
P2734 游戏 A Game 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该 ...
- 三角函数之美-水波纹载入LoadingView
一.前言 学习是要总结的.近期几天学习了画图相关的,可是使用的机会较少,如今又快要遗忘了,这次看了水波纹的绘制.认为十分有意思,还是 把实现的方法记录下来.技术无他,为手熟尔.还是要多练习,空淡误国, ...
- BZOJ2179: FFT快速傅立叶 & caioj1450:【快速傅里叶变换】大整数乘法
[传送门:BZOJ2179&caioj1450] 简要题意: 给出两个超级大的整数,求出a*b 题解: Rose_max出的一道FFT例题,卡掉高精度 = =(没想到BZOJ也有) 只要把a和 ...
- UITextField限制输入长度
首先,汉字的输入时的联想词在输入到TextFiled时,并不会走 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersIn ...
- 木马——本质就是cs socket远程控制,反弹木马是作为c端向外发起网络请求
摘自:http://kczxsp.hnu.edu.cn/upload/20150504165623705.pdf 里面对于木马的实验过程写得非常清楚,值得一看. 木马是隐藏在正常程序中的具有特殊功 ...
- 7. 关于IntelliJ IDEA删除项目
转自:https://www.cnblogs.com/zhangqian27/p/7698148.html 刚开始使用IDEA . 自己创建项目玩,结果发现IDEA无法删除,我也是醉了,Eclipse ...
- centos7 配置redis
文件上传 yum -y install lrzsz 安装redis部署前操作 同时下载redis-.tar.gz安装包 yum -y install gcc-c++ yum -y install tc ...
- border:none与border:0的区别
border:none与border:0的区别体现为两点:一是理论上的性能差异,二是浏览器兼容性的差异. 性能差异: [border:0;]把border设为“0”像素效果等于border-width ...
- SparkCore基础(一)
* SparkCore基础(一) 学习Spark,首先要熟悉Scala,当然你说你会Python或者Java能不能玩Spark?能!但是不推荐,首推Scala,因为Scala非常便捷,而且Scala有 ...