版本比较
连点游戏题解
正文
增加 16 行删除 16 行
30 行未改动31323334353536373839404142434445454647484916 行未改动6667686970707172737475767677787980818283838485868788898990919293949596979798991001013 行未改动1051061071081091091101111121131141151151161171181192 行未改动1221231241251261261271281291301311321291301311321331341341351361361371381391405 行未改动30 行未改动```cpp#include<bits/stdc++.h>using namespace std;const int N=1e6+5;namespace io {//快读 namespace io {//快读 const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; }
// putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush ();16 行未改动using io :: putc;using io :: print;using io :: flush;int n,las[N*2],nex[N*2],h[N],tot=0,a[N],p[N],q[N],cnt3;void add(int x,int y){//链式前向星存图 void add(int x,int y){//链式前向星存图 las[++tot]=y; nex[tot]=h[x]; h[x]=tot;}int b[N],c[N],m[N];//b[i]记录祖先中第一个颜色为i的位置//c[i]记录现在在b[i]的哪一个儿子中 //c[i]记录现在在b[i]的哪一个儿子中int cnt[N],cnt2[N];//cnt记录p[z]=i的z的个数,cnt2记录q[z]=i的z的个数,void dfs(int x,int fa){ int lastb=b[a[x]]; p[x]=lastb; int lastc=c[a[x]]; q[x]=lastc; if(p[x]==0){//注意p[i]=0不能全部加到0上,所以要单独开一个数组存 if(p[x]==0){//注意p[i]=0不能全部加到0上,所以要单独开一个数组存 m[a[x]]++; } else{ cnt[p[x]]++,cnt2[q[x]]++; } b[a[x]]=x;//更新当前颜色 b[a[x]]=x;//更新当前颜色 for(int i=h[x];i!=0;i=nex[i]){ if(las[i]==fa){ continue; } c[a[x]]=las[i]; dfs(las[i],x); } b[a[x]]=lastb,c[a[x]]=lastc;//复原 b[a[x]]=lastb,c[a[x]]=lastc;//复原 return;}struct node{ int v,id;3 行未改动}int main(){// freopen("click.in","r",stdin);// freopen("click.out","w",stdout); gi(n); gi(n); for(int i=1;i<=n;i++){ gi(a[i]); f[i].id=i,f[i].v=a[i]; } sort(f+1,f+1+n,cmp); for(int i=1;i<=n;i++){//将颜色离散化 for(int i=1;i<=n;i++){//将颜色离散化 if(f[i].v!=f[i-1].v||i==1){ cnt3++; } a[f[i].id]=cnt3;2 行未改动 int x,y; gi(x),gi(y); add(x,y),add(y,x); } dfs(1,0);//预处理 dfs(1,0);//预处理 long long ans=0; for(int i=1;i<=n;i++){ if(p[i]!=0){//y不能为0 ans=ans+cnt[p[i]]-cnt2[q[i]];//情况3 if(p[p[i]]!=0){ ans=ans+(cnt2[q[p[i]]]-1+1)*2;//情况1+2 if(p[i]!=0){//y不能为0 ans=ans+cnt[p[i]]-cnt2[q[i]];//情况3 if(p[p[i]]!=0){ ans=ans+(cnt2[q[p[i]]]-1+1)*2;//情况1+2 } else{//p[p[i]]=0要特殊处理 else{//p[p[i]]=0要特殊处理 if(p[i]!=1){ ans=ans+(m[a[i]]-1)*2;//无情况1 ans=ans+(m[a[i]]-1)*2;//无情况1 } } } }5 行未改动