Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)

时间:2022-03-31 10:21:06

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;
}