Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C
思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为这堆袜子需要修改的颜色数,用同样的方法处理每堆求和即可。
#include<bits/stdc++.h>
using namespace std;
int f[],c[];
map<int,map<int,int> > mp;
int getf(int x){
if(f[x] == x)
return x;
else
return f[x] = getf(f[x]);
}
void merge(int u,int v){
int a = getf(u);
int b = getf(v);
if(a != b)
f[b] = a;
}
int main(){
int n,m,k;
cin >> n >> m >> k;
for(int i = ;i<=n;i++){
f[i] = i;
scanf("%d",&c[i]);
}
int l,r;
while(m--){
scanf("%d%d",&l,&r);
merge(l,r);
}
for(int i = ;i<=n;i++){
mp[getf(i)][c[i]]++;
}
int ans = ;
for(map<int,map<int,int> >::iterator i = mp.begin();i!=mp.end();i++){
int sum = ,mx = ;
for(map<int,int>::iterator j = i->second.begin();j!=i->second.end();j++){
mx = max(mx,j->second);
sum += j->second;
}
ans += sum-mx;
}
cout << ans;
return ;
}
Codeforces 731C Socks 并查集的更多相关文章
- CodeForces 731C C - Socks 并查集
Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...
- CodeForces 731C Socks (DFS或并查集)
题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- Codeforces 731C. Socks 联通块
C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...
- CodeForces 731C Socks
http://codeforces.com/problemset/problem/731/C 并查集+贪心 将要求颜色相同的袜子序号放入一个集合中 贪心:然后统计这个集合中出现次数最多但颜色 可以得到 ...
- codeforces 722C (并查集)
题目链接:http://codeforces.com/contest/722/problem/C 题意:每次破坏一个数,求每次操作后的最大连续子串和. 思路:并查集逆向操作 #include<b ...
- CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存
http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
随机推荐
- 做了一个sublime text插件
做了一个sublime text插件,可以方便地查看C++/python的调用图.插件的演示视频在这里: http://list.youku.com/albumlist/show?id=2820226 ...
- express中url的参数传递和获取
1,传统get参数 浏览器通过这种形式的url访问localhost/userlist?id=xxx&name=yyy,这种方式可以通过req.query.id获取参数的值 router.ge ...
- redis 学习笔记(6)-cluster集群搭建
上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...
- docker学习(1) 安装
docker是啥就不多讲了,简言之就是更轻量.更牛叉的新一代虚拟机技术.下面是安装步骤: 一.mac/windows平台的安装 docker是在linux内核基础上发展而来的,无法直接运行在mac/w ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- 编写轻量ajax组件03-实现(附源码)
前言 通过前两篇的介绍,我们知道要执行页面对象的方法,核心就是反射,是从请求获取参数并执行指定方法的过程.实际上这和asp.net mvc框架的核心思想很类似,它会解析url,从中获取controll ...
- 在Unity环境下使用抽象和接口
http://gamasutra.com/blogs/VictorBarcelo/20131217/207204/Using_abstractions_and_interfaces_with_Unit ...
- jq封装淘宝图片轮播插件
<!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/htm ...
- .net core 源码解析-web app是如何启动并接收处理请求
最近.net core 1.1也发布了,蹒跚学步的小孩又长高了一些,园子里大家也都非常积极的在学习,闲来无事,扒拔源码,涨涨见识. 先来见识一下web站点是如何启动的,如何接受请求,.net core ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...