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 ofk 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 mdays.
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.
分析:每个联通块取出现颜色最多的,总个数减下即可;
清空时可以map或set,慎用memset;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fLL
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,vis[maxn],cnt[maxn],all,ma,c[maxn];
ll ans;
vi e[maxn];
set<int>q;
void dfs(int now)
{
all++;
vis[now]=;
if(ma<++cnt[c[now]])ma=cnt[c[now]];
q.insert(c[now]);
for(int x:e[now])
{
if(!vis[x])
{
dfs(x);
}
}
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&k);
rep(i,,n)c[i]=read();
while(m--)
{
j=read(),k=read();
e[j].pb(k),e[k].pb(j);
}
rep(i,,n)
{
if(!vis[i])
{
q.clear();
all=ma=;
dfs(i);
ans+=all-ma;
for(int x:q)cnt[x]=;
}
}
printf("%lld\n",ans);
//system("Pause");
return ;
}
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 ...
- Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)
Socks Problem Description: Arseniy is already grown-up and independent. His mother decided to leave ...
随机推荐
- jQuery第十一章
第十一章 一.jQuery性能优化 1.使用最新版本的jQuery类库. 2.使用合适的选择器 (1)$(“#id”) :使用id来定位DOM元素是最佳提高性能的方式. (2)$(“p”) :标签选择 ...
- CentOS安装Ruby组件
ruby安装#安装ruby组件yum install ruby ruby-irb ruby-devel rubygems rpm-build -y#安装Apache服务器yum install htt ...
- Java 序列化 对象序列化和反序列化
Java 序列化 对象序列化和反序列化 @author ixenos 对象序列化是什么 1.对象序列化就是把一个对象的状态转化成一个字节流. 我们可以把这样的字节流存储为一个文件,作为对这个对象的复制 ...
- poj 3524 Charm Bracelet(01背包)
Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...
- 使用WTL的消息反射封装CEdit实现监听控件文本改变事件
消息反射机制可以使对消息的处理都集中在控件类中,以CEdit的EN_CHANGE消息为例: /*MyEdit.h*/ class CMyEdit:public CWindowImpl<CMyEd ...
- lucene索引的创建与搜索
package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...
- localStorage请使用getItem 和setITem
最近看别人的代码,发现他们在从localStorage里面的时候喜欢用dot来操作,而不是get setItem,记得以前说过这个事.下面再说一次吧. 用dot方式来操作( 每次以'hello'= ...
- c/c++ 浮点型处理
#include <stdio.h> #include <iostream> #include <string> #include <string.h> ...
- iOS 自定义导航栏笔记
一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC ...
- 8636 跳格子(dfs+记忆化搜索)
8636 跳格子 该题有题解 时间限制:2457MS 内存限制:1000K提交次数:139 通过次数:46 题型: 编程题 语言: G++;GCC Description 地上有一个n*m 的数 ...