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]) 使得矩阵可以变换为字典序最小的矩阵 思路: 通过扫 ...
随机推荐
- BestCoder Round #78 (div.2)
因为rating不够QAQ就报了Div2.. [CA Loves Stick] CA喜欢玩木棍. 有一天他获得了四根木棍,他想知道用这些木棍能不能拼成一个四边形. Sample Input 2 1 1 ...
- BZOJ3211 花神游历各国
Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 ...
- 使用mybatis执行oracle存储过程
存储过程在小公司用的不多,但是如果业务比较复杂或者性能要求比较苛刻的时候存储过程就派上用场了,ibatis的前期的一些版本貌似不支持存储过程因此我选择了mybatis来做实验. 1.无输入和输出参数的 ...
- Video Codecs by FOURCC 视频格式编码
FOURCC Name Summary 1978 A.M.Paredes predictor This is a LossLess video codec. >>> 2VUY 2VU ...
- 用java简单的实现单链表的基本操作
package com.tyxh.link; //节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 ...
- SQL Server 收缩数据库
//删除大量数据 /*** * * BEGIN TRANSACTION; * SELECT * INTO #keep FROM Original WHERE CreateDate > '2011 ...
- swift文件上传及表单提交
var carData:NSMutableDictionary = NSMutableDictionary(); var request:NSMutableURLRequest = NSMutable ...
- Linux邮件服务器架构
// 上面的过程只是实现了简单的本地用户的文件发送功能,只需要安装mailutil,不需要安装配置sendmail,看鸟哥的Linux私房菜中写的应该是本地用户发送邮件不需要sendmail.只有当非 ...
- [转]OBOUT ASP.NET HTML Editor - Insert HTML
本文转自:http://www.obout.com/editor_new/sample_InsertHTML.aspx Example demonstrates how to access HTML ...
- Yii2 验证码不显示
siteController 中,要将captcha 列为任意用户可以访问