当前位置:网站首页>CF1527D MEX Tree (mex & tree & inclusive)
CF1527D MEX Tree (mex & tree & inclusive)
2022-08-04 14:15:00 【Harris-H】
CF1527D MEX Tree(mex&树&容斥)
Consider simple tolerance, m e x i mex_i mexi 等于包含 [ 0 , i − 1 ] [0,i-1] [0,i−1]All paths are subtracted inclusive [ 0 , i ] [0,i] [0,i]的路径.
记录: a n s i ans_i ansi为包含 [ 0 , i ] [0,i] [0,i]的所有路径.
m e x i = a n s i − 1 − a n s i mex_i=ans_{i-1}-ans_i mexi=ansi−1−ansi
m e x 0 = n ( n − 1 ) 2 − a n s 0 mex_0=\dfrac{n(n-1)}{2}-ans_0 mex0=2n(n−1)−ans0
转化为求 a n s i ans_i ansi
Consider recursion from small to large.
当前包含 [ 0 , i − 1 ] [0,i-1] [0,i−1]The shortest path endpoints are l , r l,r l,r.
情况1 i i i在 l , r l,r l,r路径上,不用操作.
情况2 i i i为 l l l或 r r rsubtree node of,将 l l l或 r r r移动到 i i i上,The answer is the product of the sizes of the two subtrees.
情况3 i i i不是 l , r l,r l,r的两个 l c a lca lca,说明无解,直接break,后面的 i i i都无解.
注意当 l = 1 l=1 l=1或 r = 1 r=1 r=1 To subtract the size of each other's subtree,再乘积.
时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)
#include<bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
const int maxn=2e5+10;
struct node{
int to,nxt;
}e[maxn<<1];
int hd[maxn],len;
inline void addedge(int fr,int to){
e[++len]={
to,hd[fr]};
hd[fr]=len;
}
int dep[maxn],fa[maxn],n,son[maxn],t,top[maxn];
ll siz[maxn];
void dfs1(int p,int f){
dep[p]=dep[f]+1;
fa[p]=f;
siz[p]=1;
for(ri i=hd[p];i;i=e[i].nxt)
if(e[i].to!=f){
dfs1(e[i].to,p);
siz[p]+=siz[e[i].to];
if(siz[e[i].to]>siz[son[p]])son[p]=e[i].to;
}
}
void dfs2(int p,int k){
top[p]=k;
if(son[p]){
dfs2(son[p],k);
for(ri i=hd[p];i;i=e[i].nxt)
if(!top[e[i].to])
dfs2(e[i].to,e[i].to);
}
}
inline int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
ll ans[maxn];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
len=0;
memset(hd,0,sizeof hd);
memset(son,0,sizeof son);
memset(top,0,sizeof top);
for(ri i=1,x,y;i<n;++i){
scanf("%d%d",&x,&y),++x,++y;
addedge(x,y),addedge(y,x);
}
dfs1(1,0);
dfs2(1,1);
memset(ans,0,sizeof ans);
ll sum=1;
for(ri i=hd[1];i;i=e[i].nxt)ans[1]+=siz[e[i].to]*sum,sum+=siz[e[i].to];
printf("%lld",1ll*n*(n-1)/2-ans[1]);
ri l=1,r=1,sl,sr;
for(ri i=2;i<=n;++i){
ri a=lca(i,l),b=lca(i,r);
if(a==l&&b==1){
if(l==1){
sl=siz[i];
for(ri j=i;fa[j]>1;j=fa[j],sl=siz[j]);
}
l=i;
}
else if(b==r&&a==1){
if(r==1){
sr=siz[i];
for(ri j=i;fa[j]>1;j=fa[j],sr=siz[j]);
}
r=i;
}
else if(a!=i&&b!=i)break;
if(l==1)ans[i]=siz[r]*(n-sr);
else if(r==1)ans[i]=siz[l]*(n-sl);
else ans[i]=siz[l]*siz[r];
}
for(ri i=2;i<=n+1;++i)printf(" %lld",ans[i-1]-ans[i]);
printf("\n");
}
return 0;
}
边栏推荐
- PAT甲级:1038 Recover the Smallest Number
- Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
- 手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
- 干掉visio,这个画图神器真的绝了
- G.登山小分队(暴力&dfs)
- 浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况
- Fuse bit of AVR study notes
- CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
- 爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
- AutoCAD DWG,DXF文件导出高清图片、PDF
猜你喜欢

相似文本聚类与调参

Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing

Win11勒索软件防护怎么打开?Win11安全中心勒索软件防护如何设置

按键控制开关4017芯片数字电路

Phasecraft连下两城,助力英国量子技术商业化加速!

如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?

c#之winform(软件开发)

世间几乎所有已知蛋白质结构,都被DeepMind开源了

State security organs conduct criminal arrest and summons review on Yang Zhiyuan, a suspect suspected of endangering national security

How to play the Tower of Hanoi
随机推荐
State security organs conduct criminal arrest and summons review on Yang Zhiyuan, a suspect suspected of endangering national security
php中的ceil和floo以及round函数「建议收藏」
如何才能有效、高效阅读?猿辅导建议“因材因时施教”
LM2596有没有可以替代的?LM2576可以
How to play the Tower of Hanoi
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
博途200/1500PLC多段曲线控温FB(支持40段控温曲线、段曲线搜索、暂停、跳段等功能)
MySQL【触发器】
世间几乎所有已知蛋白质结构,都被DeepMind开源了
"C pitfalls and pitfalls" reading summary
【无标题】
谁说 Mysql 单表最大 2000 W ?我硬要塞它 1 个亿
MySQL【窗口函数】【共用表表达式】
【LeetCode】38、外观数列
如何在ubuntu环境下安装postgresql并配置远程访问
关于redis的几件小事(五)redis保证高并发以及高可用
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
G.登山小分队(暴力&dfs)
Keycloak 6.0.0 正式发布,身份和访问管理系统
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常