C. Socks
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line of input contains three integers nm 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 ≤ nli ≠ ri) — indices of socks which Arseniy should wear during thei-th day.

Output

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.

Examples
input
3 2 3
1 2 3
1 2
2 3
output
2
input
3 2 2
1 1 2
1 2
2 1
output
0
Note

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 贪心]的更多相关文章

  1. CF731C Socks

    CF731C Socks 洛谷评测传送门 题目描述 Arseniy is already grown-up and independent. His mother decided to leave h ...

  2. 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心

    题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...

  3. 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 ...

  4. 【NOIP2015】斗地主 题解(DFS+贪心)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...

  5. CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存

    http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...

  6. NOIP2015斗地主[DFS 贪心]

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  7. NOIP2010引水入城[BFS DFS 贪心]

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...

  8. 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 ...

  9. [ZJOI2007]时态同步(dfs+贪心)

    小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅 ...

随机推荐

  1. 【电脑常识】如何查看电脑是32位(X86)还是64位(X64),如何知道硬件是否支持64位系统

    开始->运行->输入cmd确定->输入systeminfo 回车 待加载完成,就会看到如下信息(不同版本略有差异): 一.如何查看电脑是32位(X86)还是64位(X64) 方法2: ...

  2. MyEclipse或者Eclipse内存溢出问题

    对于一些项目,我们经常会遇到内存溢出的问题,这个时候我们就需要进行设置JDK的各个内存的大小了. 如果是运行项目以后出现问题的话,那么主要还是jdk或者是tomcat的内存问题,至于网上说的xxx.i ...

  3. logstash VS splunk

    web 系统是典型的分布式部署,由此对其运行状况,硬件运转情况监控也显得尤为重要,这些监控数据表面上对业务运行没有多大的用处(属于基础数据),但正是这些基础数据形成了业务“流”.比如,用户搜索爱好,浏 ...

  4. 移动端UC /QQ 浏览器的部分私有Meta 属性

    <meta name="format-detection" content ="telephone=no"/>   格式检测 禁止识别我们页面中的数 ...

  5. require的总结

    前言 我们来看原生的API吧: http://www.requirejs.org/docs/api.html#config 加载javascript文件 RequireJS采用不同的方法来加载脚本,他 ...

  6. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块

    config.xml文件的配置如下: <widget left="10" top="50" config="widgets/Navigation ...

  7. Inplace Search on document libraries and lists is not working

    [http://sharepointfarmer.com/inplace-search-on-document-libraries-and-lists-is-not-working/] I ran i ...

  8. android对话框的EditText点击后不显示输入法的问题

    dialog.show(); // 以下两行代码是对话框的EditText点击后不能显示输入法的 dialog.getWindow().clearFlags( WindowManager.Layout ...

  9. include的用法例子,以及include+merge的用法例子

    [include+LinearLayout]的使用例子 AndroidIncludeLayout.java package com.AndroidIncludeLayout; import andro ...

  10. GitHub 实现多人协同提交代码并且权限分组管理

    转载请标明出处: http://www.cnblogs.com/zhaoyanjun/p/5882784.html 出自[赵彦军博客] 2016-09-19 前言: 在上一篇文章中Android gi ...