Codeforces_731_C
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的更多相关文章
随机推荐
- Java零基础搭建实时直播平台
https://www.cnblogs.com/scywkl/p/12101437.html
- zm吃包子
[题目背景]: zm 喜欢上了吃包子. [题面描述]: zm 每天都要去买包子,但是为了减肥,zm 设置了一系列规则来控制他每天买包子的数量. 他随机了 n 个特殊字符串,然后用 n 个字符串来衡量接 ...
- axios中请求传值方式
日常开发中与后端联调,可能需要的数据不同,所传值也有所不同 1.如果是data方式,设置请求头为:并且直接返回data就可以 raw axios.defaults.headers['Content- ...
- cogs 1440. [NOIP2013]积木大赛 贪心水题
1440. [NOIP2013]积木大赛 ★★ 输入文件:BlockNOIP2013.in 输出文件:BlockNOIP2013.out 简单对比时间限制:1 s 内存限制:128 M ...
- cogs 2098. [SYOI 2015] Asm.Def的病毒 LCA 求两条路径是否相交
2098. [SYOI 2015] Asm.Def的病毒 ★☆ 输入文件:asm_virus.in 输出文件:asm_virus.out 简单对比时间限制:1 s 内存限制:256 M ...
- 网络流 - 最大流构图入门 bzoj 1305
一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...
- LCA - 倍增法去求第几个节点
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
- AVR单片机教程——小结
本文隶属于AVR单片机教程系列. 第一期挺让我失望的,是我太菜,没有把想讲的都讲出来.经常写了很多,然后一点一点删掉,最后就没多少了. 而且感觉难度不合适,处于很尴尬的位置.讲得简单,难的丢给库, ...
- Error:Cannot build artifact 'XXX:war exploded' because it is included into a circular dependency (artifact 'XXXX:war exploded', artifact 'XXX:war exploded') Idea启动项目报错解决方案
在Idea中使用Maven创建父子工程,第一个Model的那个项目可以很好的运行,在创建一个Model运行时报这个错.原因是tomcat部署了多个Web项目,可能最开始是两个项目的配置文件混用用,最后 ...
- C语言进阶——编译预处理指令
编译预处理指令 • #开头的是编译预处理指令 • 它们不是C语⾔的成分,但是C语⾔程序离不开它们 • #define⽤来定义⼀个宏 #define • #define <名字> <值 ...