传送门

无脑暴力+O2=AC

题目要统计距离两两相等的三个点的组数,这三个点之间显然有一个点,并且这三个点到这个点的距离都相同.所以枚举中间这个点作为根,然后bfs整棵树,对于每一层,把以根的某个儿子的子树中在这一层点的数量统计出来,那么这样三元组的数量就是在这些点里面选3个点,并且分别来自不同子树的方案,\(f_{i,0/1/2/3}\)即可

详见代码

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register using namespace std;
const int N=5000+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
LL ans,f[N][4];
int to[N<<1],nt[N<<1],hd[N],dg[N],tot=1,a[N];
bool v[N];
il void add(int x,int y)
{
++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot,++dg[x];
++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot,++dg[y];
}
int n,m; int main()
{
n=rd();
for(int i=1;i<n;++i) add(rd(),rd());
for(int i=0;i<=n;++i) f[i][0]=1;
queue<int> id[2],q[2];
for(int i=1;i<=n;++i)
{
memset(v,0,sizeof(v));
v[i]=1;
while(!id[0].empty()) id[0].pop();
while(!id[1].empty()) id[1].pop();
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
m=dg[i];
int nw=1,la=0;
for(int j=hd[i],k=1;j;j=nt[j],++k) id[0].push(k),q[0].push(to[j]);
while(!q[la].empty())
{
memset(a,0,4*(m+1));
while(!q[la].empty())
{
int k=id[la].front(),x=q[la].front();
id[la].pop(),q[la].pop();
++a[k],v[x]=true;
for(int j=hd[x];j;j=nt[j])
{
int y=to[j];
if(!v[y]) id[nw].push(k),q[nw].push(y);
}
}
for(int j=1;j<=m;++j)
{
for(int k=1;k<=3;++k) f[j][k]=f[j-1][k]+f[j-1][k-1]*a[j];
}
ans+=f[m][3];
swap(nw,la);
}
}
printf("%lld\n",ans);
return 0;
}

正解是长链剖分

咕咕咕

传送门

其实上面那个dp比较沙雕,可以直接设\(f_{i,j}\)为点\(i\)子树内到\(i\)距离为\(j\)的点个数,\(g_{i,j}\)为点\(i\)子树内,到lca距离为\(d\),且这个lca到\(i\)距离为\(d-j\)的点对个数,然后转移就是

\[ans=\sum_{x}g_{x,0}+\sum_{y=son_x}\sum_{j}f_{x,j-1}*g_{y,j}$$$$f_{x,j}=\sum_{y=son_x} f_{y,j-1}$$$$g_{x,j}=\sum_{y=son_x,z=son_x,y<z} f_{y,j-1}*f_{z,j-1}+\sum_{y=son_x} g_{y,j+1}
\]

对整棵树长链剖分之后,那么转移时就直接可以继承重儿子的信息,轻儿子直接暴力合并,因为每个点只会在链顶被暴力合并上去,所以复杂度是\(O(n)\)的

#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register using namespace std;
const int N=100000+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int to[N<<1],nt[N<<1],hd[N],tot=1;
il void add(int x,int y)
{
++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot;
++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot;
}
int n,m;
int ff[N],de[N],dpt[N],son[N];
LL *f[N],*g[N],rbq[N<<3],*uc=rbq,ans;
void dfs1(int x)
{
dpt[x]=de[x];
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==ff[x]) continue;
ff[y]=x,de[y]=de[x]+1,dfs1(y),dpt[x]=max(dpt[x],dpt[y]);
if(dpt[son[x]]<dpt[y]) son[x]=y;
}
}
void dd(int x)
{
if(son[x]) f[son[x]]=f[x]+1,g[son[x]]=g[x]-1,dd(son[x]);
f[x][0]=1,ans+=g[x][0];
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==ff[x]||y==son[x]) continue;
f[y]=uc,uc+=(dpt[y]-de[x]+1)<<1,g[y]=uc,uc+=(dpt[y]-de[x]+1)<<1;
dd(y);
for(int j=0;j<dpt[y]-de[x];++j)
{
if(j) ans+=f[x][j-1]*g[y][j];
ans+=g[x][j+1]*f[y][j];
}
for(int j=0;j<dpt[y]-de[x];++j)
{
g[x][j+1]+=f[x][j+1]*f[y][j];
if(j) g[x][j-1]+=g[y][j];
f[x][j+1]+=f[y][j];
}
}
} int main()
{
n=rd();
for(int i=1;i<n;++i) add(rd(),rd());
de[1]=1,dfs1(1);
f[1]=uc,uc+=(dpt[1]+1)<<1,g[1]=uc,uc+=(dpt[1]+1)<<1;
dd(1);
printf("%lld\n",ans);
return 0;
}

luogu P3565 [POI2014]HOT-Hotels的更多相关文章

  1. Luogu P3577 [POI2014]TUR-Tourism

    Luogu P3577 [POI2014]TUR-Tourism 题目链接 题目大意:给出一张\(n\)个点,\(m\)条边的无向图,保证任意两点之间没有点数超过\(10\)的简单路径.选择第\(i\ ...

  2. luogu P3567 [POI2014]KUR-Couriers

    二次联通门 : luogu P3567 [POI2014]KUR-Couriers MMP 指针 RE + MLE + WA..... 不得已...向黑恶的数组实力低头 /* 指针 */ #inclu ...

  3. 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers

    题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...

  4. P3565 [POI2014]HOT-Hotels

    题目描述 There are nn towns in Byteotia, connected with only n-1n−1 roads. Each road directly links two ...

  5. [luogu]P3572 [POI2014]PTA-Little Bird(单调队列)

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  6. luogu P3576 [POI2014]MRO-Ant colony

    传送门 一群蚂蚁能被吃,也就是走到指定边的两端点之一要走到另一端点时有\(k\)只,我们可以从这两端点逆推,记两个值为走到某个点时最后会被吃掉\(k\)只蚂蚁的蚂蚁数量范围,式子下面有,很好理解(雾) ...

  7. Luogu P3579 [POI2014]PAN-Solar Panels

    题目大意: 给出T组询问,每组询问给出四个数a,b,c,d,每次询问满足a<=x<=b,c<=y<=d的gcd(x,y)的最大值 首先可以想到如果存在gcd(x,y)=k,那么 ...

  8. luogu P3572 [POI2014]PTA-Little Bird |单调队列

    从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力 #include<cstdio> #include<cstring> #i ...

  9. luogu P3572 [POI2014]PTA-Little Bird

    题目描述 从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力 单调队列优化动态规划 #include<cstdio> #include&l ...

随机推荐

  1. python之hasattr、getattr和setattr函数

    hasattr函数使用方法 # hasattr函数使用方法 # hasattr(object,attr) # 判断一个对象里是否有某个属性或方法,返回布尔值,有为True,否则False class ...

  2. 如何在Vue项目中引入jQuery?

    假设你的项目由vue-cli初始化 (e.g. vue init webpack my-project). 在你的vue项目目录下执行: npm install jquery --save-dev 打 ...

  3. codeforces437C

    The Child and Toy CodeForces - 437C On Children's Day, the child got a toy from Delayyy as a present ...

  4. Ubuntu 16.04安装idea

    此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 前提:必须正确安装JDK和Tomcat. 下载: https://www.jetbrain ...

  5. [代码]--c#实现屏幕取词源码下载

    最近公司有一个 项目需要实现类似于金山词霸,有道词典等的屏幕取词功能,准确来说是划词功能,网上搜了各种屏幕取词无外乎就两种: A.金山词霸组件法 B.Nhw32.dll法 百度搜到的重复内容真的太多了 ...

  6. BZOJ3110[Zjoi2013]K大数查询——权值线段树套线段树

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是 ...

  7. BZOJ1124 POI2008枪战Maf(环套树+贪心)

    每个点出度都为1,可以发现这张图其实是个环套树森林,树中儿子指向父亲,环上边同向. 首先自环肯定是没救的,先抬出去. 要使死亡人数最多的话,显然若一个点入度为0其不会死亡,而一个孤立的环至少会留下一个 ...

  8. 详解HTTP协议

    一 什么是HTTP协议 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网服务器传输超文本到本地浏览器的传送协议.HTTP 是基于 TCP/ ...

  9. C# 哈希表&列队&栈

    哈希表://不规定类型,不规定长度,不规定键值 Hashtable ht = new Hashtable();  //定义 ht[1] = 1; ht['a'] = "abc"; ...

  10. JSP总结(一)——基础(汇总)

    前言:原本呢,是打算只写个JSP的内置对象总结,但是没想到这个家伙的JSP总结非常不错,我就拿来用了. 注:后缀为汇总的基本上是整理一些网上的. 借鉴地址:http://www.cnblogs.com ...