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