Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C
题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只袜子,他为了让每天穿的袜子都是一个颜色的,他需要把袜子涂色,共有k种颜色,求最少需要涂多少只袜子,才能保证他每天穿的袜子都是一样的.
用并查集把所有联系在一起的袜子放到一个集合中去,然后这些袜子会变成一块一块的,我们在每一块中找一种颜色最多的,然后把当前块中的其他袜子都涂成这种颜色即可,累加即可
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
typedef long long LL;
//////////////////////////////////////////////////////////////
const int INF = 0x3f3f3f3f;
const int N = ;
const double eps = 1e-; int m, n, k, fa[N], r[N], a[N];
vector<int> G[N]; int Find(int u)
{
if(fa[u] != u)
return fa[u] = Find(fa[u]);
return fa[u];
} int main()
{
while(scanf("%d %d %d", &n, &m, &k)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
fa[i] = i;
}
for(int i=; i<=m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
int pu = Find(u);
int pv = Find(v);
if(pu != pv)///先放到一个集合中去;
fa[pu] = pv;
}
for(int i=; i<=n; i++)
G[i].clear();
for(int i=; i<=n; i++)
{
int k = Find(i);
G[k].push_back(i);///构建一个以i的根节点的集合;
}
int ans = ;
for(int i=; i<=n; i++)
{
map<int, int> cnt;///记录各种颜色的个数;
int len=G[i].size(), Max = ;///Max是颜色最多的;
for(int j=; j<len; j++)
{
int v = G[i][j];
cnt[a[v]]++;
Max = max(Max, cnt[a[v]]);
}
ans += len - Max;///累加结果;
}
printf("%d\n", ans);
}
return ;
}
Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心的更多相关文章
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Codeforces Round #376 (Div. 2) C. Socks bfs
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)
#include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...
- Codeforces Round #346 (Div. 2) E题 并查集找环
E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...
- Codeforces Round #623 (Div. 2) D.Recommendations 并查集
ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...
- Codeforces Round #812 (Div. 2) E(并查集)
种类并查集:定义种类之间的关系来判断操作是否进行 题目大意:对于题目给出的一个矩阵,我们可以进行一种操作:swap(a[i][j],a[j][i]) 使得矩阵可以变换为字典序最小的矩阵 思路: 通过扫 ...
随机推荐
- Lambda表达式可以被转换为委托类型
void Main() { //向Users类中增加两人; List<Users> user=new List<Users>{ new Users{ID=1,Name=&quo ...
- 【BZOJ】2002: [Hnoi2010]Bounce 弹飞绵羊(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 (BZOJ挂了,还没在BZOJ测,先是在wikioi测过了,,) 囧.在军训时立志要学lct! ...
- 关于后台管理linkbutton按钮几个重要属性的理解
<asp:LinkButton ID="lkbtnDelete" runat="server" CausesValidation="False& ...
- 网站建设中HTTP状态码的奥秘
在网络营销中,站长经常会遇到一些HTTP状态码的问题,不懂HTTP状态码那么做SEO优化就无从谈起,下面是脉凌网络对HTTP状态码总结的一览表. 1xx:请求收到,继续处理 2xx:操作成功收到,分析 ...
- OpenCV Open Camera 打开摄像头
这是一个用OpenCV2.4.10打开摄像头的一个例子,参见代码如下: #include <iostream> #include <stdio.h> #include < ...
- java web工程之Hibernate
java web添加structs特性后再添加Hibernate特性,这可以通过右键工程->my eclipse出现工具条选中相应的条目,添加相应的属性, 添加完Hibernate后建立与数据库 ...
- django 1.8 日志配置
django 1.8 日志配置 以下为setings配置logging代码片段 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(_ ...
- TCP协议中的三次握手和四次挥手(图解)(转载http://blog.csdn.net/whuslei/article/details/6667471)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源 ...
- maven run as(debug as)没有运行的选项时
run as - run configration -maven build- goal目录下填上:tomcat:run即可
- Advanced Packaging Tool
https://en.wikipedia.org/wiki/Advanced_Packaging_Tool Eventually, a new team picked up the project, ...