并查集的链表优化

来源:互联网 发布:分区数据恢复 编辑:程序博客网 时间:2024/05/18 02:48

Codeforces Round #376 (Div. 2)

C. Socks

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
input
3 2 3
1 2 3
1 2
2 3
output
2
input
3 2 2
1 1 2
1 2
2 1
output
0
Note
In the first sample, Arseniy can repaint the first and the third socks to the second color.

In the second sample, there is no need to change any colors.

这道题很显然并查集,但是当时只写了50分的算法,就是在查找同一集合的元素的时候超时了。

这里是当时写的代码:

#include<iostream>#include<cstdio>using namespace std;const int maxn=200005;int root[maxn],ans,father[maxn],cnt[maxn],n,m,k;bool mark[maxn],flag=1;int color[maxn];int getfather(int x){    if(father[x]!=x) return getfather(father[x]);    return x;}int main(){    int i,j,a,b;    scanf("%d%d%d",&n,&m,&k);    for(i=1;i<=n;i++) scanf("%d",&color[i]);    for(i=1;i<=n;i++) father[i]=i;    for(i=1;i<=m;i++)    {        scanf("%d%d",&a,&b);        mark[a]=1;mark[b]=1;        if(getfather(a)!=getfather(b)) father[b]=a;    }    int start=0;    while(flag==1)    {        int maxsum=0;        int stdfather;        for(i=1;i<=n;i++) cnt[i]=0;        flag=0;        int ccnt=0;        for(i=start+1;i<=n;i++)        if(mark[i]==1)        {            start=i;            flag=1;            mark[i]=0;            ccnt++;            stdfather=getfather(i);            cnt[color[i]]++;            break;        }        for(i;i<=n;i++)            if(getfather(i)==stdfather&&mark[i]==1)            {                ccnt++;                mark[i]=0;                cnt[color[i]]++;                maxsum=max(maxsum,cnt[color[i]]);            }        ans+=ccnt-maxsum;    }    printf("%d",ans);}

这里是使用链表优化的并查集的代码:

#include<cstdio>   #include<iostream>    using namespace std;   const int maxn=200000+5,inf=0x3f3f3f3f;   inline void _read(T &x){       char ch=getchar(); bool mark=false;       for(;!isdigit(ch);ch=getchar())if(ch=='-')mark=true;       for(x=0;isdigit(ch);ch=getchar())x=x*10+ch-'0';       if(mark)x=-x;   }   int n,m,k,father[maxn],s[maxn],Last[maxn],Next[maxn],cnt[maxn]; int getfather(int x) {return father[x]==x? x:father[x]=getfather(father[x]);} void Union(int x,int y){     int fx=getfather(x),fy=getfather(y);     if(fx==fy) return ;     father[fx]=fy;     Next[Last[fy]]=fx;     Last[fy]=Last[fx]; }int Count(int x){     int i,size=0,_max=0;     for(i=x;i;i=Next[i])    {         size++;         cnt[s[i]]++;         _max=max(_max,cnt[s[i]]);     }     for(i=x;i;i=Next[i])cnt[s[i]]--;     return size-_max; } int main(){        int i,j,x,y,ans=0;     _read(n);_read(m);_read(k);     for(i=1;i<=n;i++) father[i]=Last[i]=i;     for(i=1;i<=n;i++)_read(s[i]);     for(i=1;i<=m;i++)    {         _read(x);_read(y);         Union(x,y);     }     for(i=1;i<=n;i++)         if(getfather(i)==i) ans+=Count(i);     cout<<ans<<endl;     return 0;   } 

之前的代码的缺点是每次都要从1扫到n,这样的话一旦堆数多了就会扫描很多遍,而经过链表优化的并查集每次从根开始查找,既保证了一定会扫到所有点,又保证了对于每个点只会查找一次,因此时间复杂度要少很多。

精华:

void Union(int x,int y){     int fx=getfather(x),fy=getfather(y);     if(fx==fy) return ;     father[fx]=fy;     Next[Last[fy]]=fx;     Last[fy]=Last[fx]; }

注意初始化:

for(i=1;i<=n;i++) father[i]=Last[i]=i; 
0 0
原创粉丝点击