首页 > 其他分享 >CF1918F Caterpillar on a Tree

CF1918F Caterpillar on a Tree

时间:2024-02-12 17:11:24浏览次数:18  
标签:fa dep Tree CF1918F 叶子 int Caterpillar include define

题意简述

你想要遍历一棵大小为 \(n\) 的树,初始在根节点 \(1\),每次你可以花费 \(1\) 从一个点通过一条边到达另一个点,或者花费 \(0\) 传送到根节点。求完成遍历的最小代价。

\(n\le 2\times10^5,k\le 10^9\)。

分析

首先,传送门肯定是在叶子节点使用。

其次,遍历顺序是按照子树的深度升序排序的,因为使用传送门一定会在更深的叶子中使用,而这样排序能在遍历完子树的最后一个叶子时使用传送门到达更浅的节点继续往下遍历,从而减少步数(而对不是最后一个叶子的其他叶子则一定不劣)。

下文为使操作同质化令 \(k\leftarrow k+1\),并强制最终回到根节点。

继续考虑若使用传送门会节省多少步数。设 \(fa_x\) 为按照我们定义的遍历顺序中第 \(x\) 个叶子与第 \(x+1\) 个叶子的 LCA。则在第 \(x\) 个叶子 \(a_x\) 使用传送门能减少 \(dep_x-dep_{fa}-dep_{fa}=dep_x-2dep_{fa}\) 的距离。

而若不使用传送门,我们会分别在进子树和出子树时经过一条边,所以初始答案为 \(2(n-1)\)。

我们需要步数最小,因此直接将所有叶子的 \(dep_x-2dep_{fa}\) 降序排序,贪心的从前往后取直到取满 \(k\) 个或此时减少的答案为负。

由于我们钦定的遍历顺序中深度更深的点在最后,可得最后一个叶子的深度最深。而最后一个叶子的 \(fa\) 为 1,所以最后一个叶子的 \(dep_x-2dep_{fa}\) 最大,故一定能取到,所以令 \(k\leftarrow k+1\) 是正确的。

点击查看代码
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m,k,Q;
vector<int>G[maxn];
int f[maxn],dep[maxn];
bool cmp(int x,int y){return f[x]<f[y];}
void _dfs_(int x){
	f[x]=1;
	for(int u:G[x]){
		dep[u]=dep[x]+1;
		_dfs_(u);
		f[x]=max(f[x],f[u]+1);
	}
}
int fa[maxn];
int a[maxn],cnt;
int c[maxn];
void dfs(int x){
	if(G[x].empty())return a[++cnt]=x,void();
	for(int u:G[x])dfs(u),fa[cnt]=x;
}
void solve_the_problem(){
	n=rd(),k=rd()+1;
	rep(i,2,n){int x=rd();G[x].pb(i);}
	_dfs_(1);
	rep(i,1,n)sort(G[i].begin(),G[i].end(),cmp);
	dfs(1);
	rep(i,1,cnt)c[i]=dep[a[i]]-2*dep[fa[i]];
	sort(c+1,c+cnt+1,greating);
	int ans=2*n-2;
	rep(i,1,cnt){
		if(!k||c[i]<=0)break;
		ans-=c[i],--k;
	}
	write(ans);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;while(_--)solve_the_problem();
}
/*
5 2
4 2 1 2
*/

标签:fa,dep,Tree,CF1918F,叶子,int,Caterpillar,include,define
From: https://www.cnblogs.com/dcytrl/p/18013978

相关文章

  • Link Cut Tree模板(从别人那里拿的)
    可以通过这道题#include<bits/stdc++.h>#defineRregisterint#defineIinlinevoid#defineGif(++ip==ie)if(fread(ip=buf,1,SZ,stdin))#definelcc[x][0]#definercc[x][1]usingnamespacestd;constintSZ=1<<19,N=3e5+9;charbuf[SZ],*ie=buf+SZ,*ip=......
  • P9663 Permutation on Tree 题解
    考虑枚举一个\(x\in[1,n)\),将\(\leqx\)的看作\(0\),\(>x\)的看作\(1\),那么一个排列的贡献实际上就是\(\sum_{x=1}^{n-1}\sum[[p_i\leqx]+[p_{i+1}>x]=1]\)。那么问题转变为一个给定一棵树,每一个点有权值\(0\)或\(1\),求所有排布方案的贡献之和。设\(f_x\)表示......
  • P10013 Tree Topological Order Counting 题解
    首先题目里面写了每一个数都有权值,一般这种题只能去想求出每一个的具体方案数,那么也就是我们得求出\(h_{i,j}\)表示在所有合法拓扑序中\(a_i=j\)的方案数。一颗树的拓扑序数量是\(\dfrac{n!}{\prodsiz_i}\),相信大家都知道。因为我们需要保证这一棵树满足拓扑排序的条件,不......
  • element-plus 2.4.1版本 el-tree踩坑
    element-plus2.4.1版本el-tree设置属性props中的label时,无法指定,例如<el-tree:data="datas.tree_data"show-checkboxnode-key="menu_id":props="{//label:function(data,node){//returndata.menu_name;//},......
  • [LeetCode] 2641. Cousins in Binary Tree II
    Giventherootofabinarytree,replacethevalueofeachnodeinthetreewiththesumofallitscousins'values.Twonodesofabinarytreearecousinsiftheyhavethesamedepthwithdifferentparents.Returntherootofthemodifiedtree.Note......
  • CF1446C Xor Tree 题解
    解题思路与其考虑删除哪些点,不如考虑保留哪些点。考虑到和异或有关,那么我们可以把这些数倒序插入trie树中,然后我们就可以在trie树上跑一个简单的dp:若当前节点为叶子节点,那么保留,返回\(1\);若当前节点在链上,那么直接继承儿子节点;若当前节点有两个儿子,那么更新为较大儿子......
  • Java 中的属性类Properties 以及TreeSet和TreeMap
    属性类Propertiesimportjava.util.Properties;/**目前只需要掌握Properties属性类对象的相关方法即可*Properties是一个Map集合,继承Hashtable,Properties的key和value都是String类型*Properties被称为属性类对象*Properties是线程安全的**/publicclassProperties......
  • Oracle index domain R-tree(B-tree extension)
    *[构建域索引](https://docs.oracle.com/en/database/oracle/oracle-database/19/addci/building-domain-indexes.html#GUID-E370B5E4-BAC0-49C6-B17D-830B3A507FB4)域索引是为专用域(如空间或图像处理)设计的索引。用户可以在设计器创建索引类型后生成给定类型的域索引。域索引的......
  • vue2中el-tree组件实现双击树的节点来修改节点名称
    目标在没双击之前,树的节点是文本样式。在双击之后,节点位置变成输入框形式,原节点的名称显示在输入框中,可以进行修改。修改完毕之后,当输入框失去焦点的时候,输入框消失,又变成原本的文本样式,并且显示的是修改后的节点名称。添加一个树<template><div><el-tree......
  • (13)TreeView1前面带CheckBox显示
     这些节点都是动态生成,再设置。原理还是在前面显示图片实现procedureTForm1.FormCreate(Sender:TObject);varpnode,node:TTreeNode;beginwithTreeView1.Itemsdobeginnode:=Add(nil,'Item1');//动态生成一个节点node.ImageIndex:=......