您的位置:首页 > 其它

CodeForces 731C Socks 并查集

2016-10-28 22:05 330 查看
//http://codeforces.com/problemset/problem/731/C

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;
int N,M,K,Color[MAX],pre[MAX],num[MAX];
vector<int> G[MAX];
int Find(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void mix(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
pre[fy]=fx;
}
int main()
{
ios::sync_with_stdio(false);
cin>>N>>M>>K;
for (int i=1; i<=N; i++)
{
cin>>Color[i];
pre[i]=i;
}
for (int i=1,a,b; i<=M; i++)
{
cin>>a>>b;
mix(a,b);
}
int Ans=0,total=0;
for (int i=1;i<=N;i++)
if (pre[i]==i)
num[i]=++total;
for (int i=1;i<=N;i++)
G[num[Find(i)]].push_back(Color[i]);
for (int i=1;i<=total;i++)
{
int temp=0;
map<int,int> x;
for (int j=0;j<(int)G[i].size();j++)
x[G[i][j]]=x[G[i][j]]+1,temp=max(temp,x[G[i][j]]);
Ans+=G[i].size()-temp;
}
cout<<Ans;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: