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,可是 ...
随机推荐
- 读javascript高级程序设计09-BOM
一.window 1.在全局作用域中定义的变量和函数会被归在window对象. var a=1,b=2; function add(a,b){ return a+b; } console.log(wi ...
- UVA 1349(二分图匹配)
1349 - Optimal Bus Route Design Time limit: 3.000 seconds A big city wants to improve its bus transp ...
- Octopus系列之代码备份
代码 $.extend($.validator.messages, { required: "This field is required.", remote: "Ple ...
- 利用OVER开窗函数分页
在SQL Server中,利用SQL进行分页的方法也有很多,今天要总结的是SQL Server 2005中引入的OVER开窗口函数,然后利用开窗函数进行分页. 示例代码如下: -- 设置数据库上下文 ...
- MySQL与SqlServer中update操作同一个表问题
一 SqlServer中操作如下图 这个是没问题的. 二 MySQL中操作如下图 但是在MySQL中想实现这个功能如下图,但是出错了. 原来是MySQL中不支持子查询的 我们可以这样修改一下就可以实现 ...
- hdoj 5003
题意:给你一个数组a,降序排序后,求sum+=0.95^(i-1)*ai 这题wa了两发,因为我没看清题意,要排序! 精度上面通过a^(i-1)=e^((i-1)*log(a)) 提到精度,就要想到底 ...
- BCG界面库下的Windows8 UI界面样式www.webui8.com
BCG界面库下的Windows8 UI界面样式(Metro风格)控件主要有以下一些功能: 规则的大块磁贴 支持完整键盘导航 Tile组 标题(Caption) 标题按钮(Caption buttons ...
- wine install 32bit netframewok
WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg env WINEARCH=win32 WINEPREFIX=~/.wine32 winetricks dotne ...
- 通过j-interop访问WMI实例代码
代码: import java.net.UnknownHostException; import java.util.logging.Level; import org.jinterop.dcom.c ...
- 【转】精心推荐几款超实用的 CSS 开发工具
原文转自:http://www.html5cn.org/article-5741-1.html 摘要: 当你开发一个网站或 Web 应用程序的时候,有合适的工具,绝对可以帮助您节省大量的时间.在这篇文 ...