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

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

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. int n,m,k,c[],l,r,pre[];
  9. vector<int> v[];
  10.  
  11. int findd(int x)
  12. {
  13. int root = x;
  14. while(pre[root] != root) root = pre[root];
  15. int i = x,j;
  16. while(pre[i] != root)
  17. {
  18. j = pre[i];
  19. pre[i] = root;
  20. i = j;
  21. }
  22. return root;
  23. }
  24. void join(int a,int b)
  25. {
  26. int x = findd(a),y = findd(b);
  27. if(x != y) pre[x] = y;
  28. }
  29.  
  30. int main()
  31. {
  32. scanf("%d%d%d",&n,&m,&k);
  33. for(int i = ;i <= n;i++)
  34. {
  35. scanf("%d",&c[i]);
  36. pre[i] = i;
  37. }
  38. for(int i = ;i <= m;i++)
  39. {
  40. scanf("%d%d",&l,&r);
  41. join(l,r);
  42. }
  43. for(int i = ;i <= n;i++) v[findd(i)].push_back(c[i]);
  44. int ans = ;
  45. for(int i = ;i <= n;i++)
  46. {
  47. if(!v[i].size()) continue;
  48. sort(v[i].begin(),v[i].end());
  49. int endd = v[i].size(),maxx = ;
  50. for(int j = ,now = ;j < endd;++now,j = now)
  51. {
  52. while(now < endd- && v[i][now+] == v[i][j]) now++;
  53. maxx = max(maxx,now-j+);
  54. }
  55. ans += endd-maxx;
  56. }
  57. printf("%d\n",ans);
  58. return ;
  59. }

Codeforces_731_C的更多相关文章

随机推荐

  1. 怎样使我们的用户不再抵触填写Form表单?

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/8-tips-for-an-awesome-sign ...

  2. 20.用PyInstaller打包py程序的步骤及问题解决

    最近写了一个移动和复制文件的代码,代码完成之后,想将其打包成一个可以在任何电脑上使用的软件, 于是在网上查找相关资料,可以用PyInstaller打包成.exe程序,以下是具体步骤: 1.安装PyIn ...

  3. 小技巧(2) 查询自己博客的SEO(如果违规,请先提醒)

    今天,教大家查看自己博客的SEO. 首先,上网站http://seo.chinaz.com/ 接着,在网页输入框中输入自己博客地址(带后面路径,不是cnblogs.com这样的,类似https://w ...

  4. php获取本年、本月、本周时间戳和日期格式

    时间戳格式: //获取今日开始时间戳和结束时间戳 $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(0 ...

  5. MySQL查询基础

    MySQL查询 DQL(Data Query Language ) 1.排序查询 # 语法: select 字段 from 表名 order by 字段1 [降序/升序],字段2 [降序/升序],.. ...

  6. [bzoj4571] [loj#2016] [Scoi2016] 美味

    Description 一家餐厅有 \(n\) 道菜,编号 \(1\)...\(n\) ,大家对第 \(i\) 道菜的评价值为 \(ai\)( \(1 \leq i \leq n\) ).有 \(m\ ...

  7. 频繁插入(insert)的业务,用什么存储引擎更合适? | 数据库系列(转)

    本文来自微信公众号 继续回答星球水友提问: 沈老师,MyISAM只支持表锁,但网上文章却说,在并发插入量比较大的时候,比较适合使用MyISAM,这矛盾吗? 这个问题,涉及MySQL表锁的一些细节,借着 ...

  8. 机器学习环境配置系列三之Anaconda

    1.下载Anaconda文件 进入anaconda的官网 选择对应的系统 选择希望下载的版本(本人下载的是Anaconda 5.3 For Linux Installer Python 3.7 ver ...

  9. 【WPF学习】第十九章 控件类

    WPF窗口充满了各种元素,但这些元素中只有一部分是控件.在WPF领域,控件通常被描述为与用户交互的元素——能接收焦点并接受键盘或鼠标输入的元素.明显的例子包括文本框和按钮.然而,这个区别有时有些模糊. ...

  10. GP工作室—需求分析

    团队作业第二次作业--需求分析 问题 答案 这个作业属于哪个课程 软件工程 这个作业要求在哪里 作业要求 团队名称 GP工作室 这个作业的目标 ① 团队项目的需求分析 ②团队项目的原型设计 团队队员介 ...