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的更多相关文章

  1. Codeforces 731C. Socks 联通块

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

  2. Codeforces 731C Socks 并查集

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

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

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

  4. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  5. CodeForces 731C C - Socks 并查集

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

  6. 【25.23%】【codeforces 731C】Socks

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. codeforces 731C(DFS)

    题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...

  8. Codeforces 376C. Socks

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

  9. 2017ecjtu-summer training #4 CodeForces 731C

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

随机推荐

  1. PHP 哈希表碰撞攻击

    理想情况下哈希表插入和查找操作的时间复杂度均为O(1),任何一个数据项可以在一个与哈希表长度无关的时间内计算出一个哈希值(key),然后在常量时间内定位到一个桶(术语bucket,表示哈希表中的一个位 ...

  2. BC div2补题以及 复习模除 逆元__BestCoder Round #78 (div.2)

    第一题没话说 智商欠费 加老柴辅导终于过了 需要在意的是数据范围为2的63次方-1 三个数相加肯定爆了 四边形的定义 任意边小于其余三边之和 换句话说就是 最长边小于其余三边之和 这样的话问题转化为 ...

  3. synchronized关键字修饰非静态方法与静态方法的区别

    这里我们先创建ObjLock类,并实现Runnable接口.并创建一个Demo类,具有被synchronized关键字修饰的非静态方法与静态方法. 非静态方法 public class ObjLock ...

  4. Bellman-Ford与SPFA

    一.Bellman-Ford Bellman-Ford 算法是一种用于计算带权有向图中单源最短路径(当然也可以是无向图).与Dijkstra相比的优点是,也适合存在负权的图. 若存在最短路(不含负环时 ...

  5. k8s master init and add node

    目录 一. add google apt-key 二. k8s master init 三. k8s node add to master cluster(use this command when ...

  6. 响应式Web设计- 背景图片

    背景图片可以响应式调整大小或缩放,以下是三种不同的方式 1.如果 background-size 属性设置为 "contain", 背景图片将按比例自适应内容区域.图片保持其比例不 ...

  7. 使用xcode workspace 多个project协同工作

    一般的某个应用单独新建一个 project 就可以了,然后把所有的程序文件都放在里面,这个可以满足大部分普通的需求,但是有时候,项目有可能要使用其他的项目文件,或者引入其他的静态库文件,这个时候 wo ...

  8. 【动态规划】loj#2485. 「CEOI2017」Chase

    有意思的可做dp题:细节有点多,值得多想想 题目描述 在逃亡者的面前有一个迷宫,这个迷宫由 nnn 个房间和 n−1n-1n−1 条双向走廊构成,每条走廊会链接不同的两个房间,所有的房间都可以通过走廊 ...

  9. windows下升级pip失败,重新安装pip最新版本

    环境: python3.6.5 32bit,后改为python3.4.3 32bit pycharm2018旗舰版 问题: pycharm里的pip一直无法升级到10.0.1版本,在cmd中使用升级命 ...

  10. shell-code-2-传参

    #在执行shell脚本时,带参数,如./test.sh 1 2 3,则在脚本test.sh内,参数表示为$n的形式,$1为第一个参数,以此类推. echo "第一个参数为:$1"; ...