Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)
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)的更多相关文章
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- 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 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
随机推荐
- hdu 1032
题目的意思是把输入的i,j 从i到j的每一个数 做循环,输出循环次数最大的值 易错的地方:做循环是容易直接用i进行计算 i=i/2:或i=i*3+1: 这样i的值改变就不能在做下面数的循环 #incl ...
- Java 泛型,了解这些就够用了。
此文目录: Java泛型是什么? 通常的泛型的写法示例 类型擦除 为什么要使用Java泛型 通过示例了解PECS原则 一.Java泛型是什么? 官方定义 泛型是Java SE 1.5的新特性,泛型的本 ...
- js获取手机联网状态
window.addEventListener("offline", function() { alert('offline') }, false); window.addEven ...
- bat脚本 -- 初步接触
服务器上的Tomcat 经常过一段时间就会死掉,为了防治Tomcat突然当掉影响业务,设置一个定时重启计划. bat脚本如下: @echo restart tomcat6 service, log t ...
- iOS开发UI篇—无限轮播(新闻数据展示)
iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果 二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...
- 【转】ora-00031:session marked for kill处理oracle中杀不掉的锁
一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...
- Qt开发环境中使用报表控件FastReport遇到的一些问题(二)
上一节中谈到的那个问题:传递的变量内容如果是纯英文,报表报错.经笔者反复测试,找到了解决办法:代码中第5行替换为以下 params<<"my_var"<<& ...
- join()、implode()函数
join() 函数 join() 函数把数组元素组合为一个字符串. join() 函数是 implode() 函数的别名. 语法 join(separator,array) 参数 描述 separat ...
- [URAL]刷题记录表
URAL 1001. A + B 1002. 简单题,开方计算! 1003.
- [转]概率基础和R语言
概率基础和R语言 R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在小众领域闪耀着光芒.直到大数据的爆发,R语 ...