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

题干:

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 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 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只袜子,m天,k个颜色。(n,m,k<=2e5)

第一行n个数,代表每只袜子的颜色。接下来m行给出m天中,每天穿的两只袜子的编号,每只袜子可能不同颜色,现在要让每天穿的两只袜子是相同颜色的,要重新染色的袜子数最少是多少。

解题报告:

直接并查集,然后找出并查集中的颜色最多的袜子是多少,因为并查集后属于不同集合的元素一定没有在m天中同时出现过,所以集合之间互不影响,所以这样做,也不会出现一只袜子被重复染色多次的情况。

因为这里只要最大值,所以不需要对每个集合都先排序然后找最大,而是直接开数组维护就好了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,k;
int c[MAX],f[MAX],cnt[MAX];
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1;
}
vector<int> vv[MAX];
int mx,ans;
int main()
{cin>>n>>m>>k;for(int i = 1; i<=n; i++) scanf("%d",c+i),f[i]=i;for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);merge(u,v);}for(int i = 1; i<=n; i++) getf(i);for(int i = 1; i<=n; i++) {vv[f[i]].pb(c[i]);}for(int i = 1; i<=n; i++) {if(vv[i].size() <= 1) continue;mx = 0;for(auto x : vv[i]) cnt[x]++,mx = max(mx,cnt[x]); for(auto x : vv[i]) cnt[x] = 0;//initans += vv[i].size() - mx;}cout << ans << endl;return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439715.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

50个最有用的Matplotlib数据分析与可视化图

本文介绍了数据分析与可视化中最有用的50个数据分析图&#xff0c;共分为7大类&#xff1a;Correlation、Deviation、RankIng、Distribution、Composition、Change、Groups 原文链接&#xff1a;https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-t…

一步步编写操作系统 42 用c语言编写内核

在这之前&#xff0c;我们一直用汇编语言直接与机器对话&#xff0c;如果大家不知道这个世界上有高级语言的话&#xff0c;我想大家也不会觉得写汇编代码的过程很辛苦&#xff0c;哈哈&#xff0c;幸福确实是比较出来的。相对于汇编语言&#xff0c;用c 语言写内核是非常爽的事…

【CodeForces - 722C】Destroying Array(并查集,时光倒流)

题干&#xff1a; 给定一个有n个数的序列a1,a2, ..., an 你每次可以将序列中一个数删去&#xff0c;剩下的数会被分割为一段一段连续的数列 给定一个删除数的顺序&#xff0c;问在每次删除之后&#xff0c;剩下的连续的数列中&#xff0c;数列和的最大值为多少 Input 第…

视觉SLAM十四讲(1):预备知识

最近在学习高翔博士的《视觉SLAM十四讲》&#xff08;第二版&#xff09;&#xff0c;算是初学本书&#xff0c;配套资源还算蛮丰富的&#xff0c;有代码&#xff08;第一版和第二版都有&#xff09;&#xff0c;B站上也有高翔博士对第一版录制的讲解视频&#xff0c;真的是很贴…

一步步编写操作系统 43 汇编语言和c语言的理解

也许有的同学喜欢用汇编语言来实现操作系统&#xff0c;觉得用汇编来写程序似乎更简单直接&#xff0c;可控性比较强&#xff0c;有种“一切尽在掌握”的赶脚。而用c语言实现操作系统这件事&#xff0c;虽然轻松很多&#xff0c;但似乎隐约感觉到有些慌张。因为虽然c语言相对来…

视觉SLAM十四讲(2):初识SLAM

这一讲主要介绍视觉SLAM的结构&#xff0c;并完成第一个SLAM程序&#xff1a;HelloSLAM。 目录 2.1 小萝卜的例子 单目相机 双目相机 深度相机 2.2 经典视觉SLAM框架 2.3 SLAM问题的数学表述 2.4 编程实践 Hello SLAM 使用cmake 使用库 【高翔】视觉SLAM十四讲2.1 小…

【CodeForces - 467C】George and Job(dp,思维)

题干&#xff1a; The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didnt have enough money, so George was going to work as a programmer. Now he faced the following problem at the work. Given a sequence of …

一步步编写操作系统 44 用c语言编写内核1

先来个简单的&#xff0c;欢迎我们神秘嘉宾——main.c。这是我们第一个c语言代码。 1 int main(void) { 2 while(1); 3 return 0; 4 }它没法再简单啦&#xff0c;简单的程序似乎能帮助咱们更容易的理解所学的知识&#xff0c;哈哈&#xff0c;我说的是似乎&#xff0c;其实&…

从零实现一个3D目标检测算法(1):3D目标检测概述

本文是根据github上的开源项目&#xff1a;https://github.com/open-mmlab/OpenPCDet整理而来&#xff0c;在此表示感谢&#xff0c;强烈推荐大家去关注。使用的预训练模型也为此项目中提供的模型&#xff0c;不过此项目已更新为v0.2版&#xff0c;与本文中代码略有不同。 本文…

【Codeforces - 900C】Remove Extra One(思维,STLset,tricks)

题干&#xff1a; You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers a1, a2, ..., ak the element ai is a record if for every integer…

一步步编写操作系统 45 用c语言编写内核2

在linux下用于链接的程序是ld&#xff0c;链接有一个好处&#xff0c;可以指定最终生成的可执行文件的起始虚拟地址。它是用-Ttext参数来指定的&#xff0c;所以咱们可以执行以下命令完成链接&#xff1a; ld kernel/main.o -Ttext 0xc0001500 -e main -o kernel/kernel.bin …

【Codeforces - 977F】Consecutive Subsequence(STLmap,输出路径,dp)

题干&#xff1a; You are given an integer array of length nn. You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should …

使用OpenCV库快速求解相机内参

本文主要介绍如何使用OpenCV库函数求解相机内参。具体可查阅官网&#xff1a;https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html。 关于相机内参的求解还有很多其它的工具&#xff0c;如使用MATLAB求解会更方便&#xff0c;直接调用MATLAB中的APP即可。 1.背…

一步步编写操作系统 46 用c语言编写内核3

再把上节代码贴出来&#xff0c; 1 //int main(void) { 2 int _start(void) { 3 while(1); 4 return 0; 5 }有没有同学想过&#xff0c;这里写一个_start函数&#xff0c;让其调用main函数如何&#xff1f;其实这是可以的&#xff0c;main函数并不是第一个函数&#xff0c;它实…

从零实现一个3D目标检测算法(2):点云数据预处理

在上一篇文章《从零实现一个3D目标检测算法&#xff08;1&#xff09;&#xff1a;3D目标检测概述》对3D目标检测研究现状和PointPillars模型进行了介绍&#xff0c;在本文中我们开始写代码一步步实现PointPillars&#xff0c;这里我们先实现如何对点云数据进行预处理。 在图像…

【CodeForces - 129C】Statues(思维,bfs)

题干&#xff1a; In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8  8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board h…

一步步编写操作系统 47 48 二进制程序运行方式

操作系统并不是在功能上给予用户的支持&#xff0c;这种支持是体现在机制上。也就是说&#xff0c;单纯的操作系统&#xff0c;用户拿它什么都做不了&#xff0c;用户需要的是某种功能。而操作系统仅仅是个提供支持的平台。 虽然我们是模仿linux来写一个黑屏白字的系统&#x…

百度顶会论文复现(1):课程概述

最近百度推出了一款重磅课程《全球顶会论文作者&#xff0c;28天免费手把手带你复现顶会论文》。这个课程真的是很硬核的课程&#xff0c;这里简单记录下自己的学习过程。 文章目录1. 课程设计思路和安排2. 课程大纲1. 课程设计思路和安排 课程设计思路如下&#xff0c;共分为…

【Codeforces - 127D】Password(思维,二分+字符串Hash)

题干&#xff1a; Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock be…

百度顶会论文复现(2):GAN综述

本节课主要是对GAN的发展进行了介绍&#xff0c;包括基本原理&#xff0c;训练方法&#xff0c;存在问题&#xff0c;改进以及应用场景等。实践作业则为手写数字生成。课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/education/preview/493290。 文章目录1.什么是…