CF731C. Socks[DFS 贪心]
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 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.
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 thei-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.
太水了 dfs然后每个cc里找出现次数最多的颜色用size-它
问题是怎么找:
1.数组标记,TLE
2.map标记,200ms
3.扔一个数组里然后排序扫描,数组初始化用memset就TLE,在扫描同时清空数组78ms
//map 200ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
const int N=2e5+,M=2e5+,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,k,c[N],ans;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int vis[N],size;
map<int,int> mp;
map<int,int>::iterator it;
void dfs(int u){
mp[c[u]]++;vis[u]=;size++;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(vis[v]) continue;
dfs(v);
}
}
int main(){
n=read();m=read();k=read();
for(int i=;i<=n;i++) c[i]=read();
for(int i=;i<=m;i++) ins(read(),read()); for(int i=;i<=n;i++) if(!vis[i]){
size=;mp.clear();
dfs(i); int t=;
for(it=mp.begin();it!=mp.end();it++) t=max(t,it->second);
ans+=size-t;
}
printf("%d",ans);
}
//扔数组 78ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=2e5+,M=2e5+,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,k,c[N],ans;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int vis[N],size;
int a[N],p;
void dfs(int u){
a[++p]=c[u];vis[u]=;size++;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(vis[v]) continue;
dfs(v);
}
}
int main(){
n=read();m=read();k=read();
for(int i=;i<=n;i++) c[i]=read();
for(int i=;i<=m;i++) ins(read(),read()); for(int i=;i<=n;i++) if(!vis[i]){
size=; p=;
dfs(i);
sort(a+,a++p);
int len=,mx=;
for(int i=;i<=p;i++){
if(a[i]==a[i+]) len++;
else mx=max(mx,len),len=;
a[i]=;
}
ans+=size-mx;
}
printf("%d",ans);
}
CF731C. Socks[DFS 贪心]的更多相关文章
- CF731C Socks
CF731C Socks 洛谷评测传送门 题目描述 Arseniy is already grown-up and independent. His mother decided to leave h ...
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)
题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...
- 【NOIP2015】斗地主 题解(DFS+贪心)
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...
- CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存
http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...
- NOIP2015斗地主[DFS 贪心]
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- NOIP2010引水入城[BFS DFS 贪心]
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...
- Aizu 2302 On or Off dfs/贪心
On or Off Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...
- [ZJOI2007]时态同步(dfs+贪心)
小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅 ...
随机推荐
- C# 项目提交过程中感受
C# 项目提交过程中感受 新到一家互联网公司,昨天第一次提交代码,遇到了不少问题,而且大多数是代码格式问题,特此将范的错误记录下来,自我警示. 1. 代码对齐,这个虽然一直也都在注意,不过还是有一行代 ...
- C#File类常用的文件操作方法(创建、移动、删除、复制等)
File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量很多时候都是路径path ...
- PHP json字符串,格式化缩进显示
PHP json字符串,格式化显示 /** * 格式化 */ class JsonFormatHelper { /** * json字符串缩进显示 * @param unknown $json * @ ...
- 关于前期在云服务器上部署TOMCAT服务器的问题
如果你要将你的项目放在服务器上(针对云服务器),首先你必须要学会前期部署Tomcat的准备, 第一步:安装jdk,配置环境变量 这个比较简单,配置java环境变量最为简单的方法是将java的 (../ ...
- 【转】php pdo连接数据库 解决中文乱码问题(wordpress mysql 问号?? ??)
原文链接:http://blog.csdn.net/ysydao/article/details/11002295 PHP 用pdo连接数据库时出现中文乱码问题解决办法 1.array(PDO::MY ...
- 更新整理本人所有博文中提供的代码与工具(Java,2013.11)
为了更方便地管理博文中涉及的各种代码与工具资源,现在把这些资源迁移到 Google Code 中,有兴趣者可前往下载. Java 1.<高效 Java Web 应用开发框架 JessMA v3. ...
- 深入浅出node(3) 异步I/O
这篇主要整理深入浅出Node.js第三章 异步I/O 一) 异步I/O的原因 二)异步I/O实现现状 2.1 异步I/O与非阻塞I/O 2.2 轮询 2.3 理想的非阻塞异步I/O 2.4 现实的异步 ...
- SharePoint 2013 图文开发系列之定义站点模板
SharePoint站点模板是一个非常好的功能,方便我们开发一类网站,然后在此基础上做二次开发,对于SharePoint的使用,有着举足轻重的作用. 因为篇幅比较长,所以加上目录,方便大家查看: 一. ...
- 关于在SharePoint 2013(2010)中Javascript如何实现批量批准的自定义操作功能?
1.概述: SharePoint 2013(包括SharePoint 2010)提供了很方便的,多选的界面,但是很多操作还是不能批量进行,比如:批准的功能.如果您要解决方案不关心代码,那么请直接联系作 ...
- AFNetworking的理解
AFNetworking的理解 使用方法 1. 新建的工程中导入AFNetworking3.0中的(AFNetworking 和UIKit+AFNetworking两个文件夹) 2. 在用到AFNet ...