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(并查集)的更多相关文章

  1. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  2. CodeForces 731C C - Socks 并查集

    Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...

  3. CodeForces 731C Socks (DFS或并查集)

    题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...

  4. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  5. Codeforces 731C. Socks 联通块

    C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...

  6. CodeForces 731C Socks

    http://codeforces.com/problemset/problem/731/C 并查集+贪心 将要求颜色相同的袜子序号放入一个集合中 贪心:然后统计这个集合中出现次数最多但颜色 可以得到 ...

  7. codeforces 722C (并查集)

    题目链接:http://codeforces.com/contest/722/problem/C 题意:每次破坏一个数,求每次操作后的最大连续子串和. 思路:并查集逆向操作 #include<b ...

  8. CF731C Socks并查集(森林),连边,贪心,森林遍历方式,动态开点释放内存

    http://codeforces.com/problemset/problem/731/C 这个题的题意是..小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c 代表第i只袜子的颜色,小明 ...

  9. 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 ...

  10. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

随机推荐

  1. phpv6_css

    global @charset "utf-8"; /* CSS Document */ /*格式化样式*/ body,div,dl,dt,dd,ul,ol,li,h1,h2,h3, ...

  2. [lua大坑]一个莫名其妙的lua执行时崩溃引出的堆栈大小问题

    这是一个坑,天坑!如果不是我随手删除了一个本地变量,这个问题直到现在我应该也没有头绪. 首先,写了一个新的lua脚本,载入,执行.在执行的时候,出了这么一个莫名其妙的问题: EXC_BAD_ACCES ...

  3. .net web 小基累

    获取当前网站的根目录:HttpContext.Current.Request.PhysicalApplicationPath+“Content”

  4. angular模块和组件之间传递信息和操作流程的方法(笔记)

    angular的模块之间,以及controller.directive等组件之间,是相对独立的,用以实现解耦合. 为实现相互之间传递信息及操作流程,有以下一些机制: 1.事件机制: $scope.$b ...

  5. 转:PostgreSQL Cheat Sheet

    PostgreSQL Cheat Sheet CREATE DATABASE CREATE DATABASE dbName; CREATE TABLE (with auto numbering int ...

  6. node-webkit 桌面开发 初入1

    node-webkit  是什么就不介绍了 注意官网的一句话 ”node-webkit is based on Chromium and node.js " 所以node-webkit 实际 ...

  7. windows + python + dlib

    我试了网上的各种教程,结果都是屁话 pip install dlib

  8. linux io stack

  9. Tomcat-问题解决

    1,两种方法解决tomcat的 Failed to initialize end point associated with ProtocolHandler ["http-apr-8080& ...

  10. win7怎么彻底关闭全/半角转换快捷键? imetool.exe

    from:http://bbs.csdn.net/topics/370040889 全半角转换最tm的烦人,快捷键是shift+space,不小心按到,就各种不爽, 系统看着是可以重新设置快捷键的,但 ...