CodeForces 731C - Socks(并查集)

【SOFA】SOFA框架+Win10+VS2019 配置 SOFA (Simulation Open Framework Architecture)官网 SOFA 是一个开源的仿真框架,用于实时的物理仿真,而且主要针对医学上的仿真。主要用于研究人员开发新的算法,也可以作为一个有效的快速成型工具或是物理引擎。 官网中给出的配置方法写的很清晰,有着各个库或者软件的版本要求,仔细看按版本要求来 官网配置地址 官网给了两种方 1.通过脚本自动配置 2. 自己下载相关库通过CMake进行生成 第一种 没使用,不知道是否好使,因为如果其中有下载的东西,国内不一定能成功,大家可 阅读详情

C. Socks
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

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

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.

题意:
给出N个袜子,袜子的颜色至多有K种.然后给你m天,每天穿第几只袜子(左右脚各一个),要求每天穿的袜子的颜色相同.
问保证上述条件的情况下,最少要改变几只袜子的颜色.

解题思路:
把具有逻辑关系的袜子归入到一个树中(并查集).然后对于每一个数,找出他的最大颜色数目.
那么此树需要改的颜色数目就是所有元素减去最大颜色数目.
这个题加深了对vector和map的理解.

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int f[maxn];
int c[maxn];
vector<int>v[maxn];
int Find(int x)
{
    return x == f[x]?x:f[x] = Find(f[x]);
}
void Union(int a,int b)
{
    int a1 = Find(a);
    int b1 = Find(b);
    if(a1 != b1)    f[a1] = b1;
}
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 1;i <= n;i++)   scanf("%d",&c[i]),f[i] = i;
    while(m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        Union(l,r);
    }
    for(int i = 1;i <= n;i++)   v[Find(i)].push_back(c[i]);
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        if(v[i].size() <= 1)    continue;
        int tmp = 0;
        map<int,int>mp;
        for(int j = 0;j < v[i].size();j++)
        {
            mp[v[i][j]]++;
            tmp = max(tmp,mp[v[i][j]]);
        }
        ans += v[i].size()-tmp;
    }
    printf("%d\n",ans);
    return 0;
}
大厂面经:大疆嵌入式面试题及参考答案(4万字长文:持续更新) 目录 Linux 系统调用的过程,中间发生了什么? 表格总结 Linux 中断流程,谈谈你对中断上下文的理解 中断流程 中断上下文理解 Linuxschedule()函数的原理和调用的时机 schedule()函数原理 调用时机 页表实现机制,分页的缺点? 页表机制 分页的缺点 介绍操作系统的多级反馈调度策略,时间片轮转,在项目中如何指定优先级来调度进程完成快速响应(nice命令) 多级反馈队列调度 时间片轮转 使用nice命令 聊内存分配,进程内存分配,段页式存 阅读详情

相关推荐

STM32F103 实例应用(10)——I2C读写24C32

一、应用简介 本文介绍24C32的读写,基于野火的软件I2C程序。 board_i2c.h /**=========================================================================== @file board_i2c.h @brief 本文件是用于I2C驱动 @author 青梅煮久 @version r0.1 @date 2021/01/13 -------------------------.

qq_34118600的博客 3450

CodeForces731C 并查集

C. Socks time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Arseniy is already grown-up and independent. His mother decided to leave him alon

NineFailure的博客 609

在 dell precision 7920 tower CentOS 8 Stream 上安裝nvidia驱动程序、Anaconda

简单记录一下如何在实验室dell precision 7920 tower CentOS 8 Stream上安装nvidia显卡驱动。

chen9046的博客 1003

js混淆加密实战,记录一下以备后用

js混淆加密

极简之美,独具匠心。 1万+

保姆级教程:用Multisim仿真快速上手二极管、MOSFET和IGBT的开关特性

本文提供了一份详细的Multisim仿真教程,帮助读者快速掌握二极管、MOSFET和IGBT的开关特性。通过搭建实验电路、观察波形特征和参数影响分析,深入解析了反向恢复过程、米勒平台现象和拖尾电流效应。适合电力电子初学者和工程师通过仿真建立直观理解,提升电路设计能力。

weixin_29290863的博客 501

玄机-Linux权限维持-后门

玄机-Linux权限维持-后门

m0_46439803的博客 773

佐藤ひろ美 - わたしのそらのいろ

[ti:こころのカケラ] [ar:佐藤ひろ美] [al:わたしのそらのいろ] [by:] [00:01.00]こころのカケラ [00:03.00]歌:佐藤ひろ美(佐藤裕美) [00:05.00]作词:佐藤裕美 作编曲:藤间仁 [00:08.00]OVA 「みずいろ」PinkPineapple ED [00:11.00] [00:21.80]心のかけら 探すように 君の事 探し続け

暮然回首,那人却在灯火阑珊处 2万+

基于Python的Booking酒店数据爬虫实战:最新技术与完整代码解析

本文将详细介绍如何使用Python构建一个高效的Booking酒店数据爬虫,涵盖从基础爬取到高级反反爬技术的完整实现。我们将使用最新的Python爬虫技术栈,包括requests-html、Playwright、智能代理轮换等技术,并提供完整的可运行代码示例。文章将深入探讨爬虫的各个组成部分,包括数据提取、存储、反反爬策略以及性能优化等方面。关键词:Python爬虫、Booking数据采集、反反爬技术、Playwright、数据存储。

2201_76125261的博客 941

Base64资源

Base64资源。

qq_43557302的博客 3万+

Matlab 曲面平滑与点云处理

曲面平滑是一种常见的数学和计算机图形学技术,可以用来消除噪声、填补数据间隙,以及改善曲面的外观。在点云处理中,曲面平滑也被广泛应用于重建、拟合和可视化等领域。本文将介绍如何使用 Matlab 进行曲面平滑和点云处理,并提供相应的源代码。通过以上步骤,我们可以使用 Matlab 进行曲面平滑和点云处理。你可以根据具体需求调整参数和算法,以获得理想的曲面平滑效果。接下来,我们可以使用平滑算法对三角化的网格进行曲面平滑。以上代码将绘制原始点云和重建后的点云,并在图形中显示坐标轴和图例。表示平滑的迭代次数。

QfcaLinux的博客 760

数据库小技能:数据导入脚本

背景:版本大升级,从2.0升级到3.0 ,服务端开发语言从c#换到java语言。数据库类型从microsalft sql server 换到mysql。数据来源于微信,用于查询卡bin。

专注于计算机研发知识和资讯分享 781

JavaScript代码规范(常用)

JavaScript代码规范 注:新手一枚,本篇博客为了给自己的学习做个记录,若有表述错误或其他错误欢迎指正,谢过各位了 ![手动抱拳] 1.嵌入规则 JavaScript程序应尽量放在.js文件中,需要调用的时候在html文件中以script标签的形式包含进来,避免在html文件中直接编写JavaScript代码<script src="filename.js"> 2.空格与...

wmdydxr的博客 4517

通过分子模板构建随机分布在溶液中的纳米颗粒(一)

通过分子模板构建随机分布在溶液中的纳米颗粒 要实现的目标: 1、可以循环建立多个水分子溶液的模型; 2、针对不同水分子数,自动创建文件夹; 3、运行完毕将所有结果自动存储在对应文件夹里; 分子模板的具体教程请见我的博客: https://blog.csdn.net/qq_43689832/article/details/104443175 1、首先,我们先建立纳米颗粒的分子模板,命名为p_30.txt: 包括两种原子类型。具体格式如下: # LAMMPS molecule file makin.

Rockyoung 2651

codeforces 731c Socks 并查集

A - Socks Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Arseniy is already grown-up and independent. His mother decided to l

WA是一笔财富 447

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Codeforces 731C Socks 并查集

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 m

a7f650ebd327889c的博客 294

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Codeforces 731C Socks(并查集)

题意:给你n条袜子,一共有k种颜色,要求每天穿的两只袜子颜色相同,求需要最少改多少只袜子的颜色才能满足条件。 把同一天穿的两只袜子利用并查集放到一块,最后形成了一个一个集合,找出每个集合中哪个颜色的袜子最多,再把集合中其他颜色的袜子变成那个颜色,最后把每个集合所求的值加起来就是答案。 #include using namespace std; int type[200005],pre[

ScorpioN___的博客 687

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

CodeForces 731C Socks 并查集

//http://codeforces.com/problemset/problem/731/C#include<bits/stdc++.h> using namespace std; const int MAX=2e5+5; int N,M,K,Color[MAX],pre[MAX],num[MAX]; vector<int> G[MAX]; int Find(int x) { int r

Katapeltes 373

Codeforces731C-Socks(并查集)

题目链接http://codeforces.com/contest/731/problem/C思路只要两只袜子同时穿,那么就放进一个集合里面,最后统计一下每个集合里面出现颜色最多的,这样这个颜色就可以不改变,这个集合里面的其他袜子全部变成这个颜色细节统计每个集合里面的出现最多的颜色的时候,如果直接用数组来统计会爆内存,于是用的数组+map代码#include <iostream> #include

博客已迁移至:lzed.github.io 465

并查集 - Socks - CodeForces - 731C

并查集 - Socks - CodeForces - 731C 题意: 首行输入n,m,k,表示有n条袜子,挑选出m对,共有k种可能的颜色。首行输入n,m,k,表示有n条袜子,挑选出m对,共有k种可能的颜色。首行输入n,m,k,表示有n条袜子,挑选出m对,共有k种可能的颜色。 第二行输入n个数字c1,c2,...,cn,ci表示第i条袜子的颜色。第二行输入n个数字c_1,c_2,...,c_n,c_i表示第i条袜子的颜色。第二行输入n个数字c1​,c2​,...,cn​,ci​表示第i条袜子的颜色。 接着输

njuptACMcxk的博客 234

CodeForces 731C Socks (并查集)

题意:n只袜子,穿m天,每只袜子有一种颜色,有k种颜料对袜子染色,接下来的m天每天给你指定两只袜子穿,问现在最少需要染色多少次才能使得接下来的m天每天穿的袜子颜色都相同。注意是一次性染色。 题解:并查集 既然是一次性染色,那么就对所在同一集合的袜子染相同颜色,我们不关心袜子出现几次,所以染的颜色取决于集合中颜色最多的是哪个。 用vector存以根为代表的集合含有的颜色,map存个数,遍历求最多即可。 #define _CRT_SECURE_NO_WARNINGS #include<iostream&g

gongyuandaye的博客 322

codeforces731——C. Socks(并查集)

原题链接 题意: 有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色。 思路: 将在一天里穿的放到一个连通块里,染成当前连通块最多的一种颜色。并查集或dfs维护。 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll>PLL; typedef pair&l

我们都有光明的未来 579

CodeForces - 731C Socks (并查集)

Socks Arseniy is already grown-up and independent. His mother decided to leave him alone formdays and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy'...

YOONGI 359

Codeforces 731C:Socks(并查集)

http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问要让每天的袜子是相同颜色的,要重新染色的袜子数最少是多少。 思路:并查集合并,将同一天的袜子合并起来,然后就形成了cnt个集合,每个集合都是独立的,因此排序,找出每个集合里面袜子颜色相同的最多的是...

weixin_30421525的博客 201

CodeForces 731C Socks (并查集+一点点的技巧)

并查集找出互为一组的袜子,它们的颜色要是一样的。 然后要把x组袜子高效的分离,使用sort重写cmp就可以很简单的给他们排序(或者用指针数组记录偏移量),相邻的为一组。 再然后通过排序后的标号找到颜色,通过all记录颜色出现的次数,通过队列把前一组的颜色置0即可。 #include <cstdio> #include <algorithm> #include <cma...

监心司的博客 313

【CodeForces - 731C】Socks(并查集,思维)

题干: Arseniy is already grown-up and independent. His mother decided to leave him alone formdays and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clo...

xuanweiace的博客 858

CodeForces 731C C - Socks 并查集

题目链接:点击打开链接 题目大意:n只袜子,并对袜子标号为1-n。然后用k种颜色对n只袜子染色. 接下来给出m天要用的袜子(每天2只),要求这使这m天,每天两只袜子的颜色相同,问至少需要染几只袜子。 思路就是: 首先用并查集,将他们分组,同一天的袜子最后要求颜色相同,所以将他们分入同一个并查集。 然后用vector数组存储每个并查集,要求同一个并查集中的袜子要是相同的颜色。那么本来有

qq_36782366的博客 411
上一篇: POJ 1321 - 棋盘问题(DFS)
下一篇: CodeForces 698B - Fix a Tree(并查集)
Snakeflute
博客等级 码龄11年 14粉丝 172原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值