CodeForces 731C Socks
http://codeforces.com/problemset/problem/731/C
并查集+贪心 将要求颜色相同的袜子序号放入一个集合中
贪心:然后统计这个集合中出现次数最多但颜色 可以得到这个集合要repain的次数
代码有难度
统计集合数
int tot;//总的集合数
for (int i = 1; i <= n; i++)
if(par[i] == i)
{
rec[tot++] = i;//记录根节点
}
//统计每个集合红颜色的个数
map<int, int> clmp;
vector<int> G[MAXN];
for(int i = 1; i <= n; i++)
{
G[par[i]].push_back(color[i]);
}
int ans = 0;
for(int i = 0; i < tot; i++)//统计颜色情况
{
clmp.clear();
for (int j = 0; j < v[rec[i]].size(); j++)
clmp[v[rec[i]][j]]++;
}
#include <bits/stdc++.h>
#define MAXN 200007
using namespace std; int par[MAXN]; int find(int x)
{
if (par[x] == x) return x;
else return par[x] = find(par[x]);
}
bool same(int x, int y)
{
int px, py;
px = find(x);
py = find(y);
return px == py;
}
void unite(int x, int y)
{
int px = find(x);
int py = find(y);
par[px] = py;
}
int color[MAXN];
int rec[MAXN];
vector<int> v[MAXN];
map<int,int> clmap;
int main()
{
int day, paint, socks, ans = ;
scanf("%d%d%d", &socks, &day, &paint);
for (int i = ; i <= socks; i++) par[i] = i;
for (int i = ; i <= socks; i++)
{
scanf("%d", &color[i]);
}
for (int i = ; i < day; i++)
{
int l, r;
scanf("%d%d", &l, &r);
unite(l, r);
}
int tot = ;
for (int i = ; i <= socks; i++)
{
if (i == find(i))
{
rec[tot++] = i;
}
}
for (int i = ; i <= socks; i++)
{
v[par[i]].push_back(color[i]);
}
for (int i = ; i < tot; i++)
{
clmap.clear();
for(int j = ; j < v[rec[i]].size(); j++)
{
clmap[v[rec[i]][j]]++;
}
map<int,int> :: iterator it;
int maxn = ;
for (it = clmap.begin(); it != clmap.end(); it++)
{
maxn = max(maxn, (*it).second);
}
ans += v[rec[i]].size() - maxn;
}
cout << ans << endl;
}
CodeForces 731C Socks的更多相关文章
- 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/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- CodeForces 731C Socks (DFS或并查集)
题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- CodeForces 731C C - Socks 并查集
Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...
- 【25.23%】【codeforces 731C】Socks
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- Codeforces 376C. Socks
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 2017ecjtu-summer training #4 CodeForces 731C
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
随机推荐
- org.springframework.beans.factory.BeanCreationException: Could not autowire
由于我在项目中引用了如下代码,增加了 @Configurationpublic class Connection { public @Bean HttpClientConfig httpClie ...
- H5新特性:
新增选择器 document.querySelector.document.querySelectorAll 拖拽释放(Drag and drop) API 媒体播放的 video 和 audio 本 ...
- python中return和yield
def wx(): a = 'wx' b = '无邪' return a, b print(wx()) print(type(wx())) -----------执行结果--------------- ...
- CPP-基础:关于多态
类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持面向对象的,其实不然,Visua ...
- CeontOS6.5安装php环境
港湾云主机重装操作系统之后xshell无法连接:重启ssh:# service sshd restart -bash: vim: command not found:输入 rpm -qa|grep v ...
- shell脚本,按字母出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- Java异常 Exception类及其子类
C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象. Java异常处理通过5个关键字try.ca ...
- 【二分】bestcoder p1m2
模型的转化和二分check的细节挺不错的 Problem Description 度度熊很喜欢数组!! 我们称一个整数数组为稳定的,若且唯若其同时符合以下两个条件: 数组里面的元素都是非负整数. 数组 ...
- 概述「并查集补集转化」模型&&luoguP1330 封锁阳光大学
奇妙的模型转化以及并查集思想 模型概述 有图$G=(V,E)$,初始所有点为白色,现在要将其中一些点染为黑色,要求染色后满足:$∀(u,v)∈E$,$∃col_u!=col_v$.求最小染色点数. 题 ...
- gnu make规则记录
1. $(shell CMD) 名称: 执行 shell 命令函数 功能: 在新的 shell 中执行 CMD 命令 返回值: CMD 在 shell 中执行的结果 例如:PLATFORM=$(she ...