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,可是 ...
随机推荐
- iOS开发拓展篇—UIDynamic(捕捉行为)
iOS开发拓展篇—UIDynamic(捕捉行为) 一.简介 可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动 UISnapBehavior的初始化 - (instancetype ...
- 【仿真】【modelsim】:verilog功能仿真流程
一.编写verilog源文件,在diamond中编译.编写testbench文件.在diamond设置中将仿真工具设置为modelsim,运行仿真向导 二.自动进入modelsim, 编译全部 运行仿 ...
- css盒子模型层级3D图
作为前端开发工程师,大家都应该知道盒子模型.下面用一张图来表达3D盒子模型的层级关系 大家可以看到background-color 在background-image的下一层.这个希望对大家有帮助
- css布局之三列布局
网站上使用三列布局的还是比较多的,不过三列和两列有些相似: 1.自适应三列 <!DOCTYPE html> <html lang="en"> <hea ...
- PATH变量
PATH变量由系统管理员配置,通常包含如下一些存储系统程序的标准路径: /bin 二进制文件目录,用于存放启动系统时用到的路径 /usr/bin 用户二进制文件目录,用于存放用户使用的标准程序 / ...
- IOS网络开发(三)
1 飞机航班查询软件 1.1 问题 NSURLConnection是IOS提供的用于处理Http协议的网络请求的类,可以实现同步请求也可以实现异步请求,本案例使用NSURLConnection类实现一 ...
- 利用pip8.1.2 安装django1.9.7
把python2升级到python3之后,利用pip安装django1.9.7时报错: DistributionNotFound: The 'pip==7.1.0' distribution was ...
- 关于 edittext 软键盘退出监听解决办法
edittext 有个onCreateInputConnection 的方法,通过它可以自定义一个输入法连接器,那里连接器里面有个方法(finishComposingText)能监听输入完成的动作. ...
- UIControlEventTouch
在控件事件中,简单解释下下面几个事件. 说明:由于是在“iOS 模拟器”中测试的,所以不能用手指,只能用鼠标. 1)UIControlEventTouchDown 指鼠标左键按下(注:只是“按下”)的 ...
- UVa 10129单词(欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...