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 ;
}

Codeforces_731_C的更多相关文章

随机推荐

  1. Spark设置Kryo序列化缓冲区大小

    背景 今天在开发SparkRDD的过程中出现Buffer Overflow错误,查看具体Yarn日志后发现是因为Kryo序列化缓冲区溢出了,日志建议调大spark.kryoserializer.buf ...

  2. 深入浅出 JVM 系列(一)什么是 JVM?它处于什么位置?

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  3. ECOS问题解决记录

    1.finder自定义列 http://club.ec-os.net/doc/ecos/framework-ecos/advance/desktop/dev.html#id5 2.命令行工具 地址 h ...

  4. 道格拉斯-普克算法(JavaScript实现)

    需求: 有时候当移动速度很慢,GPS定位的轨迹点就非常的多,这时候为了缩减数据量,需要将不突出的点去掉. 思路: (1) 在曲线首尾两点间虚连一条直线,求出其余各点到该直线的距离. (2)选其最大者与 ...

  5. QuartzCore

    QuartzCore 说起QuartzCore不知道有多少小伙伴很容易和Quartz2D.CoreGraphics等混淆在一起傻傻分不清楚?所以在下面我们先把这几个很容易混淆或者是分不清楚的框架稍加整 ...

  6. 使用wireshark 对flutter 框架APP进行抓包

    引言 最近公司开发一个APP,由于原生人力不足,直接由前端使用flutter 开发的,而使用flutter框架开发的客户端 fiddler无法抓到包,所以我采用wireshark从路由层面抓包 fid ...

  7. canal 基于Mysql数据库增量日志解析

    canal 基于Mysql数据库增量日志解析  1.前言  最近太多事情 工作的事情,以及终身大事等等 耽误更新,由于最近做项目需要同步监听 未来电视 mysql的变更了解到公司会用canal做增量监 ...

  8. java 大数的学习

    import java.math.*; import java.util.*; public class study { public static void main(String[] args) ...

  9. Oracle安装连接常见错误

    oracle安装注意:安装路径url不能带中文C:\app\59428\product\11.2.0\dbhome_1\sqldeveloper打开sqldeveloper的时候,需要输入java.e ...

  10. Window同一电脑配置多个git公钥

    前言 配置多个本地ssh-key之前,先初始化下GIt环境哦! 可以参照:https://www.cnblogs.com/poloyy/p/12185132.html 执行前两步就好啦 本地生成两个s ...