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. Linux配置临时IP地址

    # ifconfig 查看网卡信息,如下图所示: # ifconfig eth0 192.168.0.107 eth0表示第一块网卡,Linux中所有的设配都是文件,所以eth0是第一块网卡的文件名, ...

  2. IOS修改系统音量

    #import <IOKit/IOKitLib.h> #import <IOKit/hidsystem/IOHIDLib.h> #import <IOKit/hidsys ...

  3. Luogu P5352 Terrible Homework

    神仙@TheLostWeak出的题,因为他最近没时间所以我先写一下sol(其实我也没什么时间) 作为一道简单的数据结构题想必大家都能看出必须用LCT维护信息吧 一个朴素的想法就是直接维护四种操作的值, ...

  4. pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'

    http://blog.csdn.net/qq_33202928/article/details/72526710

  5. myeclipse 导入项目时no projects are found to import解决办法

    myeclipse 识别一个工程需要.classpath与.project文件,一般无需提交SVN所以项目切下来的时候是没有这两个文件的. 方法1: 1) 在myeclipse中新建一个和你要导入的项 ...

  6. 设置tableview的滚动范围--iOS开发系列---项目中成长的知识三

    设置tableview的滚动范围 有时候tableview的footerview上的内容需要向上拖动界面一定距离才能够看见, 项目中因为我需要在footerviw上添加一个按钮,而这个按钮又因为这个原 ...

  7. 【动态规划】bzoj1044: [HAOI2008]木棍分割

    需要滚动优化或者short int卡空间 Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍 ...

  8. Ubuntu apt-get出现unable to locate package解决方案

    前言 刚安装好的ubuntu 17发现apt-get安装指令异常. 故经网上搜索调查发现,发现这个问题基本是因为apt-get需要更新的缘故. 解决方案 只需使用命令升级更新即可. sudo apt- ...

  9. maven 打某一个模块的包

    mvn clean mvn clean install -pl benefit-microservice-gateway -am -Dmaven.test.skip=true

  10. Knockout v3.4.0 中文版教程-10-绑定-控制文本内容和外观-visible绑定

    4.绑定 1. 控制文本内容和外观 1. visible绑定 目的 visible绑定可以根据你传入绑定的值控制关联的DOM元素显示或隐藏. 例子 <div data-bind="vi ...