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. CSS3制作的一款按钮特效

    /*.btn { width:230px; height:70px; font-size:70px; font-weight:bold; overflow:hidden; font: "He ...

  2. kafka 安装以及测试

    1,下载kafka 并进行解压 http://mirrors.cnnic.cn/apache/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz 2,启动Zookeeper  ...

  3. A. Pride (emmmm练习特判的好题)

    题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...

  4. off-by-one&doublefree. 看雪10月ctf2017 TSRC 第四题赛后学习

    off-by-one 0x00 发现漏洞 1.off-by-one 在massage函数中,如图所示,可以修改的字节数比原内存大小多了一个字节 2.悬挂指针 可以看到,在free堆块的时候,没有清空指 ...

  5. Ueditor1.4.3上传视频IE下无法播放的问题

    一:百度编辑器插入视频后,自动生成一段代码: <video class="edui-upload-video vjs-default-skin video-js" contr ...

  6. 企业版https

    http://www.cocoachina.com/bbs/read.php?tid=194213

  7. path.join()与path.resolve()区别

    1.path.resolve([...paths]) path.resolve() 方法会把一个路径或路径片段的序列解析为一个绝对路径. 给定的路径的序列是从右往左被处理的,后面每个 path 被依次 ...

  8. 初涉tarjan缩点

    tarjan缩点:口胡过好多题,不过从来没写过…… 什么是缩点 tarjan和Kosaraju.Gabow算法一样,是为了求有向图中的强连通分量.因为有向图中大多数情况下会有环存在,而有环是一个不甚好 ...

  9. vim中使用pydiction对python代码进行补全

    在配置完vim插件YouCompleteMe之后,在vim编写python的时候按tab键会报错,E121: Undefined variable: g:pydiction_location Pydi ...

  10. (61)zabbix网络发现规则配置实战/详解

    开始配置.首先,我们需要定义发现规则,用于扫描.步骤如下 第一步 Configuration >>Discovery>>Create rule,编辑网络发现规则 如上配置,za ...