题目链接: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---并查集+贪心的更多相关文章

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

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

  2. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

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

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

  5. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  6. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  7. Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)

    #include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...

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

  9. Codeforces Round #623 (Div. 2) D.Recommendations 并查集

    ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...

  10. Codeforces Round #812 (Div. 2) E(并查集)

    种类并查集:定义种类之间的关系来判断操作是否进行 题目大意:对于题目给出的一个矩阵,我们可以进行一种操作:swap(a[i][j],a[j][i]) 使得矩阵可以变换为字典序最小的矩阵 思路: 通过扫 ...

随机推荐

  1. sprintf() in c

    Declaration Following is the declaration for sprintf() function. int sprintf(char *str, const char * ...

  2. CentOS6.4 配置LVS(DR模式)

    DR模式中LVS主机与实际服务器都有一块网卡连在同一物理网段上. IP分配 VIP:10.10.3.170 RIP1:10.10.3.140 RIP2:10.10.3.141 1.安装所需的依赖包 y ...

  3. c# 使用GetOleDbSchemaTable获取access数据库结构

    c# 使用GetOleDbSchemaTable获取access数据库结构 ado.net可以使用GetOleDbSchemaTable方法来获取access数据库的结构,但得到的datatable的 ...

  4. CentOS网卡配置文件

    [root@xaiofan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetONBOOT=yesNM ...

  5. 三层交换单臂路由vlan间通信综合实验之降龙要点--Lee

    单臂路由三层交换机提供vlan间的通信之菜鸟之降龙详解要点: 图示 PC:左到右依次设置IP172.16.10.1,    20.1,  30.1,   40,1  ,50,1  /24 网关10.2 ...

  6. hlg 2130 状压dp

    基本的状压dp 需要注意的是两点之间直线最短 所以不需要进行floyd 由于把dp的memset放在了初始化0的后面de了好久的bug.. #include<stdio.h> #inclu ...

  7. Linux 计划任务 Crontab 笔记与总结(5)crontab 常见错误与案例

    ① 环境变量 cd ~ pwd 就会到你登陆的那个用户的根目录下 ls -a 能够查看到 .bash_profile 这个文件 vim .bash_profile 这里面设置了一些环境变量. 可以设置 ...

  8. changing a pointer rather than erasing memory cells

    Computer Science An Overview _J. Glenn Brookshear _11th Edition In a data structure based on a point ...

  9. MVC程序实现Autocomplete功能

    为了加强MVC的功力,加强练习是少不了的事情.Insus.NET这次想实现文本框的autocomplete功能. 在数据创建一张表[PinYin]: 插入一些数据: INSERT INTO [dbo] ...

  10. 【转】 使用Redis的Pub/Sub来实现类似于JMS的消息持久化

    http://blog.csdn.net/canot/article/details/52040415 关于个人对Redis提供的Pub/Sub机制的认识在上一篇博客中涉及到了,也提到了关于如何避免R ...