Socks

Problem Description:

Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.

Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.

When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.

Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.

The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.

Input:

The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.

The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.

Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.

Output:

Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.

Sample Input:

3 2 3

1 2 3

1 2

2 3

Sample Output:

2

当时不会,之后又看了官方英文题解,才懂的 http://codeforces.com/blog/entry/47826 话说看英文题解还是挺有意思的,能学到好多东西,比如,我是到现在才知道并查集的简称是dsu

【题目链接】Socks

【题目类型】并查集+建图+贪心

&题意:

n只袜子,m天,袜子有k种颜色。

袜子编号从1~n,你要使得他每天穿的两只袜子的颜色相同(两只袜子的编号分别是li和ri)

你可以改变袜子的颜色,问最少改变多少只袜子才能使每天穿的两只袜子的颜色相同。

&题解:

英文题解:

consider li, ri as a non-directed edge.

so the question changes into: there are some conected components, one component must be the same color, query the minimum times to modify one vector's color.

it's easy to solve with dsu , first of all, we use dsu to get all conected components. For each conected component, we use the color which has the most frequency to colour this connected component.

我的中文理解:

把li, ri看成无向边,每个袜子看成点,用并查集把整个图连起来,那么这个图就会分成几个块,我们只需要找每个块中出现次数最多的那种颜色就好了。

把所有块的出现颜色最多的个数加起来,最后在用n减去它,就是答案了,因为有一堆袜子,你要把它们变成一种颜色,你一定是把它们变成颜色出现次数最多的那种颜色,只有这种改变的方法,才能使得改变颜色的次数最小。

【时间复杂度】O(\(n\alpha {(n)}\))

&代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 5 ;
int pa[maxn],col[maxn];
bool vis[maxn];
int n,m,k;
map<int,int> maii[maxn];
int findset(int x) { return pa[x]==x?x:pa[x]=findset(pa[x]);}
void Solve(){
while(~scanf("%d%d%d",&n,&m,&k)){
for(int i=0;i<n;i++)
scanf("%d",&col[i]);
for(int i=0;i<n;i++)
pa[i]=i;
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
u--,v--;
int x=findset(u),y=findset(v);
if (x!=y){
pa[x]=y;
}
}
int ans=0;
set<int> sei;
for(int i=0;i<n;i++) maii[i].clear();
for(int i=0;i<n;i++){
int x=findset(i);
sei.insert(x);
maii[x][col[i]]++;
}
for(auto i=sei.begin();i!=sei.end();i++){
int ma=0;
for(auto j=maii[(*i)].begin();j!=maii[(*i)].end();j++){
ma=max(ma,j->second);
}
ans+=ma;
}
printf("%d\n",n-ans);
}
}
int main(){
Solve();
return 0;
}

我感觉我的代码写得够简单了,然而看了dalao的,感觉我好菜,必须膜一发,所以照着码了一遍。

&dalao代码:

#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;++i)
const int maxn=200050;
int n,m,k,mx;
int a[maxn],ans;
int l,r,fa[maxn],siz[maxn];
vector<int> G[maxn];
map<int,int> mp;
int f(int x){return fa[x]==x?x:fa[x]=f(fa[x]);}
int main()
{
scanf("%d%d%d",&n,&m,&k);
rep(i,1,n) scanf("%d",&a[i]), fa[i]=i;
rep(i,1,m)
{
scanf("%d%d",&l,&r);
if (f(l)!=f(r)) fa[f(l)]=f(r);
}
rep(i,1,n) siz[f(i)]++,G[f(i)].push_back(a[i]);
rep(i,1,n)
if (siz[i])
{
mp.clear(); mx=0;
rep(j,0,G[i].size()-1)
mp[G[i][j]]++,mx=max(mx,mp[G[i][j]]);
ans+=siz[i]-mx;
}
printf("%d",ans);
return 0;
}

Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)的更多相关文章

  1. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  2. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  3. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  4. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  5. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. 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 ...

  7. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  8. Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法

    题目链接:http://codeforces.com/contest/731/problem/D D. 80-th Level Archeology time limit per test 2 sec ...

  9. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

随机推荐

  1. json 转换错误:JSON.parse expected property name or '}'

    错误原因: 格式要为: [ { "name":"张三", "age":"20" }, { "name" ...

  2. ios批量打包

    最近我们接到了新的需求,需要打出类似xx001-xx100共100个这样的ipa渠道包,不需要签名.(这批ipa包后续会用企业证书签名,不会影响AppStore的) 这些包所有的功能.内容都是一样的, ...

  3. iOS开发网络篇—监测网络状态

    iOS开发网络篇—监测网络状态 一.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行 ...

  4. ios资源

    ios 资源 分类: ios开发2012-05-30 16:39 573人阅读 评论(0) 收藏 举报 ios文档calendar2010reference图像处理 学习过程当中查找到的资料,做一个记 ...

  5. goldengate 12c 12.2 新特性(updated)

    GoldenGate 12.2已经提供下载,增加了不少新特性 1. 异构配置加强不在需要sourceDefs和AssumeTargetDefs文件,在队列文件中已经包含metadata信息,比如tab ...

  6. 详解<a>标签

    相信对于学前端的人而言<a>标签并不陌生,我们先把他所有的属性列出来 一.主要属性 一般来说,a标签有着四种状态,分别是link,hover,active,visited,接下来我会仔细讲 ...

  7. ACE - Reactor源码总结整理

    ACE源码约10万行,是c++中非常大的一个网络编程代码库,包含了网络编程的边边角角. ACE代码可以分三个层次:OS层.OO层和框架层: OS层主要是为了兼容各个平台,将网络底层API统一化,这一层 ...

  8. magento jQuery冲突N种方法

    在做修改模板的时候在page中加入jquery库发现原本自带的js冲突 商品无法加入购物车,很多js都没有效果 这是jQuery和magento自带prototype的冲突解决版本有很多种,说个简单点 ...

  9. 如何快速编写Vim语法高亮文件

    这里有一份中文的入门文档,但是太长了,不想读,所以有本文 最快的办法,就是找一个语法相近的语法高亮文件,稍微改一下 自己从头写的话,首先搞定关键字: syntax case match "是 ...

  10. sphinx.conf 详解

    sphinx的配置文件是在配置的时候最容易出错的了: 我们先要明白几个概念: source:数据源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一 ...