CF731C Socks

洛谷评测传送门

题目描述

Arseniy is already grown-up and independent. His mother decided to leave him alone for mm 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 nn socks is assigned a unique integer from 11 to nn . Thus, the only thing his mother had to do was to write down two integers l_{i}l**i and r_{i}r**i for each of the days — the indices of socks to wear on the day ii (obviously, l_{i}l**i stands for the left foot and r_{i}r**i for the right). Each sock is painted in one of kk 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 kk jars with the paint — one for each of kk colors.

Arseniy wants to repaint some of the socks in such a way, that for each of mm 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 mm days.

输入格式

The first line of input contains three integers nn , mm and kk ( 2<=n<=2000002<=n<=200000 , 0<=m<=2000000<=m<=200000 , 1<=k<=2000001<=k<=200000 ) — the number of socks, the number of days and the number of available colors respectively.

The second line contain nn integers c_{1}c1 , c_{2}c2 , ..., c_{n}c**n ( 1<=c_{i}<=k1<=c**i<=k ) — current colors of Arseniy's socks.

Each of the following mm lines contains two integers l_{i}l**i and r_{i}r**i ( 1<=l_{i},r_{i}<=n1<=l**i,r**i<=n , l_{i}≠r_{i}l**ir**i ) — indices of socks which Arseniy should wear during the ii -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.

题意翻译

有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色。

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

说明/提示

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.

题解:

2019.11.5模拟赛95分场

题目翻译还挺好的,可以看一下。

我们发现,如果有一只袜子需要在\(m\)天中的多天出现,那么显然这只袜子要与所有和它配对的袜子颜色一样。

以此联想,我发现:这个关联关系是一张图,图上的节点就是袜子的编号,图的边表示这两只袜子要颜色一样。

那么我们发现,对于这张图的每一个连通块来讲,它的颜色必须是全部一样的。那么,对于这个连通块来讲,最优的决策(即修改最少)就是这个子图的全部节点数减去这个子图中出现颜色最多的颜色数。这一点不太明白的小伙伴可以看样例画图解决。

思路出来了:

依题意建图,在图上进行深搜,统计每张子图的节点数和出现次数最多的颜色出现了几次,累加答案即可。

于是T到了95感谢出题人@littleseven

一开始我使用了计数数组,每次深搜一张子图我清零一次(memset).

后来我学到了一种叫做\(map\)的容器,因为这个容器的时间复杂度是\(log\)级别的,而且它提供的映射操作能够统计题目信息,所以我们选用这个东西来优化实现复杂度/

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=2*1e5+10;
int n,m,k,tmp,ans,t;
int tot,head[maxn],nxt[maxn<<1],to[maxn<<1];
bool v[maxn];
map<int,int> mp;
map<int,int>::iterator it;
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48){if(ch=='-')f=-1;ch=nc();}
while(ch>47) x=(((x<<2)+x)<<1)+ch-48,ch=nc();
return x*f;
}
int col[maxn];
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int x)
{
v[x]=1;
mp[col[x]]++;
tmp++;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(v[y])
continue;
dfs(y);
}
}
int main()
{
n=read(),m=read(),k=read();
for(int i=1;i<=n;i++)
col[i]=read();
for(int i=1;i<=m;i++)
{
int x,y;
x=read();y=read();
add(x,y);
add(y,x);
}
for(int i=1;i<=n;i++)
if(!v[i])
{
tmp=0,mp.clear(),t=0;
dfs(i);
for(it=mp.begin();it!=mp.end();it++)
t=max(t,it->second);
ans+=(tmp-t);
}
printf("%d",ans);
return 0;
}

CF731C Socks的更多相关文章

  1. CF731C. Socks[DFS 贪心]

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

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

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

  3. 并查集【CF731C】Socks

    Description 有\(n\)只袜子,\(k\)种颜色,在\(m\)天中,问最少修改几只袜子的颜色,可以使每天要穿的袜子颜色相同. Input 第一行\(n,m,k\)分别对应题目描述. 接下来 ...

  4. iphone使用mac上的SOCKS代理

    Step 1. Make sure the SOCKS tunnel on your work computer allows LAN connections so your iPhone/iPod ...

  5. redsocks 将socks代理转换成全局代理

    redsocks 需要手动下载编译.前置需求为libevent组件,当然gcc什么的肯定是必须的. 获取源码 git clone https://github.com/darkk/redsocks 安 ...

  6. 将 Tor socks 转换成 http 代理

    你可以通过不同的 Tor 工具来使用 Tor 服务,如 Tor 浏览器.Foxyproxy 和其它东西,像 wget 和 aria2 这样的下载管理器不能直接使用 Tor socks 开始匿名下载,因 ...

  7. Mac Aria2 使用Privoxy将socks代理转化为http代理

    安装Privoxy 打开终端安装privoxy来实现这里我是通过brew来进行的安装 brew install privoxy 看到这行已经安装成功 ==> Caveats To have la ...

  8. SOCKS 5协议详解(转)

    笔者在实际学习中,由于在有些软件用到了socks5(如oicq,icq等),对其原理不甚了解,相信很多朋友对其也不是很了解,于是仔细研读了一下rfc1928,觉得有必要译出来供大家参考. 1.介绍: ...

  9. Codeforces 731C. Socks 联通块

    C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...

随机推荐

  1. 如何让junit的测试跑多次

    对JUnit4可以使用下面的方法: @RunWith(Parameterized.class) public class RunTenTimes { @Parameterized.Parameters ...

  2. Paper | Adaptive Computation Time for Recurrent Neural Networks

    目录 1. 网络资源 2. 简介 3. 自适应运算时间 3.1 有限运算时间 3.2 误差梯度 1. 网络资源 这篇文章的写作太随意了,读起来不是很好懂(掺杂了过多的技术细节).因此有作者介绍会更好. ...

  3. navicat连接mysql报错1251解决方案,从头搭建node + mysql 8.0 (本人亲测有效)

    准备学node 好久了 一直没有动手去写,今天突发奇想,然后就安装了一个mysql (找了一个博客跟着步骤去安装的),然后打算用node 写个增删改查. 1.下载mysql安装包   地址: http ...

  4. Rails + Webpacker + Puma + Nginx 部署

    准备 ssh 登录 首先 ssh 登录服务器,免密码登录可以参考 ssh 免密码登录服务器 创建部署用户 $ sudo adduser deploy 安装依赖 Ruby 这里使用 RVM 安装和管理 ...

  5. php date获取当前时间

    结果: 结论: 本以为第一种方式最快,第三种方式竟超乎想象的快且稳定

  6. LeetCode 205:同构字符串 Isomorphic Strings

    题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字 ...

  7. IT兄弟连 Java语法教程 数据类型2

    整型 Java定义了4种整数类型:byte.short.int和long.所有这些类型都是有符号的.正或负的整数.Java不支持无符号的.只是正值的整数.许多其它计算机语言同时支持有符号和无符号整数. ...

  8. 使用pytorch时所遇到的问题总结

    使用pytorch时所遇到的问题总结 1.ubuntu vscode切换虚拟环境 在ubuntu系统上,配置工作区文件夹所使用的虚拟环境.之前笔者误以为只需要在vscode内置的终端上将虚拟环境切换过 ...

  9. C#面对对象之封装、继承、多态的简单理解

    一.封装 隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读取和修改的访问级别. 简单来多,就是讲我们所需要的代码打包封装进入一个类里面,便于我们调用,操作.这就是封装. 这样就隔离了具体 ...

  10. Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之六 || API项目整体搭建 6.1 仓储+服务+抽象接口模式

    本文梯子 本文3.0版本文章 前言 零.完成图中的粉色部分 2019-08-30:关于仓储的相关话题 一.创建实体Model数据层 二.设计仓储接口与其实现类 三.设计服务接口与其实现类 四.创建 C ...