AC 了,但是发现了一个事情。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int COLOR=1e6+5;
int n,m,x,y,op,ans,a[N],f[COLOR],to[N],h[COLOR],st[COLOR],siz[COLOR];
void merge(int x,int y){
for(int i=h[x];i;i=to[i]) ans-=(a[i-1]==y)+(a[i+1]==y);
for(int i=h[x];i;i=to[i]) a[i]=y;
to[st[x]]=h[y];
h[y]=h[x];
siz[y]+=siz[x];
/*
to[st[y]]=h[x];
siz[y]+=siz[x]
*/
st[x]=h[x]=siz[x]=0;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<COLOR;i++) f[i]=i;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
ans+=(a[i]!=a[i-1]);
to[i]=h[a[i]];
if(!h[a[i]]) st[a[i]]=i;
h[a[i]]=i;
siz[a[i]]++;
}
while(m--){
scanf("%d",&op);
if(op==1){
scanf("%d%d",&x,&y);
if(x==y) continue;
if(siz[f[x]]>siz[f[y]]) swap(f[x],f[y]);
if(!siz[f[x]]) continue;
merge(f[x],f[y]);
}
else printf("%d\n",ans);
}
return 0;
}
以上代码是满分的,但是如果把接入链表改成注释里的部分,就会全 WA(但对了 subtask 1),而且输出精准比答案大 1。这是为什么?