BZOJ.5407.girls/CF985G. Team Players(三元环计数+容斥)

时间:2021-11-22 23:11:38

题面

传送门(bzoj)

传送门(CF)

\(llx\)身边妹子成群,这天他需要从\(n\)个妹子中挑出\(3\)个出去浪,但是妹子之间会有冲突,表现为\(i,j\)之间连有一条边\((i,j)\),定义一种选择方案的权值为\(Ai+Bj+Ck,i<j<k\),求所有选择方案的权值之和

题解

容斥,至少\(0\)条边相连的方案\(-\)至少\(1\)条边相连的方案\(+\)至少\(2\)条边相连的方案\(-\)至少\(3\)条边相连的方案

至少\(3\)条边相连的方案最难数,是个三元环计数,和这题方法一样

虽然我感觉自己去数啥都数不出来

//minamoto
#include<bits/stdc++.h>
#define R register
#define ull unsigned long long
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=2e5+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
struct EG{int u,v;}E[N];
vector<int>to[N],Pre[N],sum[N];int deg[N],sz[N],ss[N],vis[N];
int n,m,tim;ull res,A,B,C,ps[N];
inline ull bin(R int n){return 1ll*n*(n-1)>>1;}
inline ull calc(R int l,R int r){return 1ll*(l+r)*(r-l+1)>>1;}
int main(){
// freopen("testdata.in","r",stdin);
n=read(),m=read(),A=read(),B=read(),C=read();
for(R int i=1,u,v;i<=m;++i){
u=read(),v=read(),u>v?swap(u,v),0:0;
++sz[u],++ss[v],ps[v]+=u,++deg[u],++deg[v],to[u].push_back(v),Pre[v].push_back(u);
E[i].u=u,E[i].v=v;
}
fp(i,0,n-1)sort(to[i].begin(),to[i].end()),sort(Pre[i].begin(),Pre[i].end());
fp(i,1,m)deg[E[i].u]>deg[E[i].v]||(deg[E[i].u]==deg[E[i].v]&&E[i].u>E[i].v)?add(E[i].u,E[i].v):add(E[i].v,E[i].u);
fp(i,0,n-3)res+=A*i*bin(n-1-i);
fp(i,1,n-2)res+=B*i*i*(n-1-i);
fp(i,2,n-1)res+=C*i*bin(i);
fp(i,0,n-1){
sum[i].resize(sz[i]);sz[i]?sum[i][0]=to[i][0]:0;
fp(j,1,sz[i]-1)sum[i][j]=sum[i][j-1]+to[i][j];
}
for(R int i=1,u,v;i<=m;++i){
u=E[i].u,v=E[i].v;
res-=(A*u+B*v)*(n-1-v)+C*calc(v+1,n-1),
res-=(B*u+C*v)*u+A*calc(0,u-1),
res-=(A*u+C*v)*(v-u-1)+B*calc(u+1,v-1);
}
fp(u,0,n-1)fp(i,0,sz[u]-1){
R int v=to[u][i];
res+=(A*u+B*v)*(sz[u]-i-1)+C*(sum[u][sz[u]-1]-sum[u][i]);
if(sz[v])res+=(A*u+B*v)*sz[v]+C*sum[v][sz[v]-1];
}
for(R ull i=0,sum=0;i<n;++i,sum=0)
fp(j,0,ss[i]-1){
R int u=Pre[i][j];
res+=(B*u+C*i)*j+A*sum,
sum+=Pre[i][j];
}
fp(u,0,n-1){
++tim;
go(u)vis[v]=tim;
for(R int k=head[u];k;k=e[k].nx)go(e[k].v)if(vis[v]==tim){
R int x=u,y=e[k].v,z=v;
if(x>y)swap(x,y);if(x>z)swap(x,z);if(y>z)swap(y,z);
res-=A*x+B*y+C*z;
}
}
printf("%llu\n",res);
//printf("%I64u\n",res); 要交到CF上的话得写这个格式
return 0;
}