Codeforces 731 C.Socks-并查集+STL(vector+map)
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)
Codeforces 731 C.Socks-并查集+STL(vector+map)的更多相关文章
- 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 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- codeforces 766 D. Mahmoud and a Dictionary(种类并查集+stl)
题目链接:http://codeforces.com/contest/766/problem/D 题意:给你n个单词,m个关系(两个单词是反义词还是同义词),然后问你所给的关系里面有没有错的,最后再给 ...
- CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存
http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
随机推荐
- 配合JAVA的AJAX使用
概要 Ajax是“Asynchronous JavaScript and XML”的简称,即异步的JavaScript和XML. readyState属性用来返回当前的请求状态,有五个可选值.分别是0 ...
- 常见的位运算技巧总结(膜wys)
看了wys的论文,感觉获得了不少新姿势 这里总结一下 #include <iostream> using namespace std; typedef unsigned int u32; ...
- [洛谷P4779]【模板】单源最短路径(标准版)
题目大意:单元最短路径(卡$SPFA$) 题解:$dijkstra$($\underline{\hspace{0.5em}}\underline{\hspace{0.5em}}gnu\underlin ...
- Vim使用小记(一)常用操作
By francis_hao Sep 22,2016 vim的功能自然不止如此,这里只是把日常使用频率较高的记录下来,若想了解vim的全部功能可查阅其帮助手册:help,或者查询指定命令的用法: ...
- makefile使用笔记(一)入门
By francis_hao Mar 2,2017 makefile makefile一个很简单的例子如下,该实例完成在执行make时,将main.c编译成可执行文件main的功能. 各项的含义 ...
- xcode 10 新特性
这里主要介绍一下Xcode10 版本主要更新的内容.随着iOS12的发布,Xcode10已经可以从Mac App Store下载.Xcode10包含了iOS12.watchOS 5.macOS10.1 ...
- python最简单发送邮件
#!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...
- MyBatis的SQL语句映射文件详解(三)----多参数传递的几种方式
1.单一基本类型参数(String,int等) 单一的基本类型参数,将对应语句中的parameterType的值与参数的类型相同.然后直接 用“#{参数名}” 来获取 java代码 //String类 ...
- 51nod1031(简单斐波拉契数列)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1031 题意:中文题诶- 思路:对于第x块骨牌的情况,我们用a ...
- codefoeces problem 671D——贪心+启发式合并+平衡树
D. Roads in Yusland Mayor of Yusland just won the lottery and decided to spent money on something go ...