http://codeforces.com/problemset/problem/731/C

并查集,然后找每个集合里颜色的最大数量,求集合中元素数量-这个最大数量,最后总数相加即答案。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std; int n,m,k,c[],l,r,pre[];
vector<int> v[]; int findd(int x)
{
int root = x;
while(pre[root] != root) root = pre[root];
int i = x,j;
while(pre[i] != root)
{
j = pre[i];
pre[i] = root;
i = j;
}
return root;
}
void join(int a,int b)
{
int x = findd(a),y = findd(b);
if(x != y) pre[x] = y;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i = ;i <= n;i++)
{
scanf("%d",&c[i]);
pre[i] = i;
}
for(int i = ;i <= m;i++)
{
scanf("%d%d",&l,&r);
join(l,r);
}
for(int i = ;i <= n;i++) v[findd(i)].push_back(c[i]);
int ans = ;
for(int i = ;i <= n;i++)
{
if(!v[i].size()) continue;
sort(v[i].begin(),v[i].end());
int endd = v[i].size(),maxx = ;
for(int j = ,now = ;j < endd;++now,j = now)
{
while(now < endd- && v[i][now+] == v[i][j]) now++;
maxx = max(maxx,now-j+);
}
ans += endd-maxx;
}
printf("%d\n",ans);
return ;
}

最新文章

  1. SQLSERVER监控复制并使用数据库邮件功能发告警邮件
  2. 斯坦福iOS7公开课4-6笔记及演示Demo
  3. 手动启动angular
  4. [译]Autoprefixer:用最可行的方式处理浏览器前缀的CSS后处理器
  5. AXIS-web.xml里配置axis报错addChild: Child name &#39;AxisServlet&#39; is not unique 解决办法
  6. (java web后端方向)如何让你的简历为你争取到更多的面试机会,内容来自java web轻量级开发面试教程
  7. SSH Secure Shell Client最新版,解决Win10不兼容问题
  8. 关于oracle11g在window10环境下安装不满足最低要求问题:报错NS-13001
  9. 如何在Linux下查看版本信息
  10. who are we?
  11. Python编程Day4——if判断、while循环、for循环
  12. 如果merge分支出现问题,使用git方式查看日志
  13. springboot 使用JPA自动生成Entity实体类的方法
  14. Leetcode 326.3的幂 By Python
  15. 不输入密码执行sudo 命令
  16. idou老师教你学Istio :如何用istio实现监控和日志采集
  17. 页面跳转时中间参数保存(memcache/cookie)
  18. Java 组合
  19. asiHttpRequst 学习地址
  20. 前端-CSS-4-伪类选择器&amp;伪元素选择器

热门文章

  1. apache相关实验-2
  2. 入门Grunt前端构建工具
  3. ffmpeg参数编码大全
  4. Netty快速入门(05)Java NIO 介绍-Selector
  5. 速石科技携HPC混合云平台亮相AWS技术峰会2019上海站
  6. 虚拟机安装(Vmware14)
  7. React16源码解读:揭秘ReactDOM.render
  8. 区间dp - 全部按下一列石头
  9. sql if else 语句
  10. 一文熟练使用python mock