CodeForces - 731C

Problem
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 l i and r i for each of the days — the indices of socks to wear on the day i (obviously, l i stands for the left foot and r i 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 c 1, c 2, …, c n (1 ≤ c i ≤ k) — current colors of Arseniy’s socks.

Each of the following m lines contains two integers l i and r i (1 ≤ l i, r i ≤ n, l i ≠ r i) — 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.

并查集
AC代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int co[200005], father[2000005], num[200005];
int n, m, k, x, y;
vector<int>p[200005];
int find(int x)
{
	if (father[x] != x)
	{
	
		father[x] = find(father[x]);
	}
	return father[x];
}
int main()
{
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++)
	{
	
		father[i] = i;
		cin >> co[i];
	}
	for (int i = 1; i <= m; i++)
	{
	
		cin >> x >> y;
		int px = find(x);
		int py = find(y);
		if (px != py)
			father[px] = father[py];
	}
	for (int i = 1; i <= n; i++)
	{
	
		int px = find(i);
		p[px].push_back(co[i]);
	}
	int ans = 0, maxn;
	for (int i = 1; i <= n; i++)
	{
	
		if (p[i].size() == 1)
			continue;
		map<int, int>rema;
		maxn = 0;
		for (int j = 0; j < p[i].size(); j++)
		{
		
			rema[p[i][j]]++;
			maxn = max(maxn, rema[p[i][j]]);
		}
		ans += p[i].size() - maxn;
	}
	cout << ans << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这款X-Scan v3.3 简体中文版软件非常不错:它采用多线程方式对指定IP地址段(或单机)进行安全漏洞检测,支持插件功能,提供了图形界面和命令行两种操作方式! 理论上可运行于Windows NT系列操作系统,推荐运行于Windows 2000以上的Server版Windows系统。 X-Scan v3.3 简体中文版扫描内容包括:远程操作系统类型及版本,标准端口状态及端口BANNER信息,CGI漏洞,IIS漏洞,RPC漏洞,SQL-SERVER、FTP-SERVER、SMTP-SERVER、POP3-SERVER、NT-SERVER弱口令用户,NT服务器NETBIOS信息等,扫描结果保存在/log/目录中,index_*.htm为扫描结果索引文件 v3.3升级内容:X-Scan v3.3 简体中文版优化主程序及脚本引擎,改进存活主机检测手段并修正其他已知BUG,更新攻击测试脚本及中文描述 。 X-Scan v3.3采用多线程方式对指定IP地址段(或单机)进行安全漏洞检测,支持插件功能。扫描内容包括:远程服务类型、操作系统类型及版本,各种弱口令漏洞、后门、应用服务漏洞、网络设备漏洞、拒绝服务漏洞等二十几个大类。对于多数已知漏洞,我们给出了相应的漏洞描述、解决方案及详细描述链接,其它漏洞资料正在进一步整理完善中,您也可以通过本站的“安全文摘”和“安全漏洞”栏目查阅相关说明。 3.0及后续版本提供了简单的插件开发包,便于有编程基础的朋友自己编写或将其他调试通过的代码修改为X-Scan插件。另外Nessus攻击脚本的翻译工作已经开始,欢迎所有对网络安全感兴趣的朋友参与
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值