Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C
题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问要让每天的袜子是相同颜色的,要重新染色的袜子数最少是多少。
思路:并查集合并,将同一天的袜子合并起来,然后就形成了cnt个集合,每个集合都是独立的,因此排序,找出每个集合里面袜子颜色相同的最多的是哪个颜色,然后把其他不属于这个颜色的都染成这个颜色,那么这样重新染色的袜子数是最少的。然后每个集合的答案加起来就是最后的结果了。还怕这样太暴力会超时,还好没有。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
typedef long long LL;
int fa[N], col[N], root[N], vis[N];
vector<int> vec[N]; int Find(int x) {
if(x == fa[x]) return x;
return fa[x] = Find(fa[x]);
} void Merge(int x, int y) {
x = Find(x), y = Find(y);
if(x == y) return ;
fa[x] = y;
} int main() {
int n, m, k;
cin >> n >> m >> k;
for(int i = ; i <= n; i++) {
fa[i] = i;
scanf("%d", &col[i]);
}
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
vis[u] = vis[v] = ;
Merge(u, v);
}
int cnt = ;
for(int i = ; i <= n; i++) {
if(vis[i]) {
if(fa[i] == i) root[++cnt] = i;
vec[Find(i)].push_back(col[i]);
}
}
int ans = ;
for(int i = ; i <= cnt; i++)
sort(vec[root[i]].begin(), vec[root[i]].end());
for(int i = ; i <= cnt; i++) {
int now = , ma = ;
for(int j = ; j < vec[root[i]].size(); j++) {
if(vec[root[i]][j] != vec[root[i]][j-]) {
ma = max(ma, now); now = ;
} else now++;
}
ma = max(now, ma);
ans += vec[root[i]].size() - ma;
}
printf("%d\n", ans);
return ;
}
Codeforces 731C:Socks(并查集)的更多相关文章
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- 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所 ...
随机推荐
- Android组件安全
今天在看有关Android组件安全的东西 1.Activity Android系统组件在指定Intent过滤器(intent-filter)后,默认是可以被外部程序(签名不同,用户ID不同)访问的,在 ...
- Oracle 教程
视频教程 Oracle DBA数据库高级工程师职业学习指南与职业规划视频课程
- 函数式中的 currying
currying 是函数式语言中经常遇到的一个概念,翻译成 柯里化,不是库里化. currying 指的是将接收多个参数的函数变换成接收一个单一参数,并且返回接收余下的参数而且返回结果的新函数的技术. ...
- python批量进行文件修改操作
python批量修改文件扩展名 在网上下载了一些文件,因为某种原因,扩展名多了一个后缀'.xxx',手动修改的话因为文件太多,改起来费时费力,于是决定写个小脚本进行修改. 1.要点: import r ...
- a 添加href后当前栏目如何高亮显示
//nav $(".nav li a").each(function() { $this = $(this); if ($this[0].href == String(window ...
- 就publish/subscribe功能看redis集群模式下的队列技术(一)
Redis 简介 Redis 是完全开源免费的,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中 ...
- VS2015 + Cordova Html5开发使用Crosswalk Web引擎
CrossWalk引擎的好处是统一不同android设备的Html5的差异性,据说速度很快. Vs2015中使用非常简单,作为一个Apache Cordova的插件安装即可: Installing t ...
- Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- VR原理讲解及开发入门
本文是作者obuil根据多年心得专门为想要入门的VR开发者所写,由52VR网站提供支持. 1. VR沉浸感和交互作用产生的原理: 在之前,我们观看一个虚拟的创造内容是通过平面显示器的,52VR ...
- a biped was detected but cannot be configured properly (Bipe导入Unity 无法正确识别)
OP stated "I export the biped with 'animation' and 'bake animation' ticked and the correct fram ...