Codeforces 1247F. Tree Factory
正难则反,把链操作成树不好想,那么考虑一下如何把树变成链
每次操作相当于把一个兄弟变成儿子(我把你当兄弟你竟然想把我当儿子.jpg)
注意到每次操作最多只能使树的深度增加 $1$
因为链的深度为 $n$ 且形态唯一,那么只要把原树操作成深度为 $n$ 即可
现在得到了一个操作次数的下限,即 $n$ 减树的初始深度
考虑一下如果每次操作都能保证使树的深度增加,那么这样的一系列操作即为最优答案
事实上任何时刻都一定存在可以使长度增加的操作,考虑当前树从根节点出发的最长链,我们找到链上最深的存在分叉的节点 $x$
设 $v$ 为 $x$ 的儿子且在最长链上,$w$ 为 $x$ 的任意一个其他儿子,那么我们只要把 $v$ 变成 $w$ 的儿子即可使树深度增加
显然当树的深度不为 $n$ 时,最长链上一定存在分叉
然后现在问题就是输出方案了,这个东西不太好解释,看代码比较清楚吧...(代码很短)
注意一下代码实现的时候是链变成树而不是树变成链了
代码参考:wxhtxdy
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e5+;
int n,fa[N],dep[N],son[N];
vector <int> V[N],id,mov;
int cnt;//这个东西表示当前节点上一个兄弟的最后一条链的深度
void dfs(int x)
{
id.push_back(x);
for(int i=;i<=cnt;i++) mov.push_back(x);
cnt=;
for(int v: V[x]) if(v!=son[x]) dfs(v);
if(son[x]) dfs(son[x]);//最长链最后走
cnt++;
}
int main()
{
n=read(); dep[]=;
for(int i=;i<=n;i++)
{
int a=read()+; fa[i]=a;
dep[i]=dep[a]+; V[a].push_back(i);
}
int t=max_element(dep+,dep+n+)-dep;
while(t!=) son[fa[t]]=t,t=fa[t];//找最长链
dfs();
for(int x: id) printf("%d ",x-); puts("");
printf("%d\n",int(mov.size()));
for(int x: mov) printf("%d ",x-); puts("");
return ;
}
Codeforces 1247F. Tree Factory的更多相关文章
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题
F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...
- Codeforces 1246D/1225F Tree Factory (构造)
题目链接 https://codeforces.com/contest/1246/problem/D 题解 首先考虑答案的下界是\(n-1-dep\) (\(dep\)为树的深度,即任何点到根的最大边 ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- codeforces 627B B. Factory Repairs(线段树)
B. Factory Repairs time limit per test 4 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 570D - Tree Requests【树形转线性,前缀和】
http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...
- Codeforces 23E Tree
http://codeforces.com/problemset/problem/23/E 题意:给一个树,求砍断某些边,使得所有联通块大小的乘积最大.思路:f[i][j]代表当前把j个贡献给i的父亲 ...
- Codeforces 1092F Tree with Maximum Cost(树形DP)
题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...
- [Educational Round 17][Codeforces 762F. Tree nesting]
题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...
随机推荐
- OpenFOAM 中的边界条件(一)【转载】
链接:http://xiaopingqiu.github.io/2016/04/02/Boundary-conditions-in-OpenFOAM1/ 本系列解读 OpenFOAM 中边界条件的实现 ...
- 解析配置文件 redis.conf
1.units单位 2.INCLUDES包含 3.GENERAL通用 1).daemonize daemonize yes 启用后台守护进程运行模式 2).pidfile pidfile /var/r ...
- navicat连接远程数据库报错'client does not support authentication protocol requested by server consider ...'解决方案
[1.cmd终端连接远程mysql数据库方法] mysql -uhello -pworld -h192.168.1.88 -P3306 -Dmysql_oa mysql -u用户名 -p密码 -h ...
- 20182332 实验一《Linux基础与Java开发环境》实验报告
20182332 实验一<Linux基础与Java开发环境>实验报告 课程:<程序设计与数据结构> 班级: 1823 姓名: 盛国榕 学号:20182332 实验教师:王志强 ...
- powerdesigner导出rtf
报告--reports--新建reports--reports template选择None 从左侧的Available items 拖动 需要导出的item到右侧 右侧的每一个item右键 可以设置 ...
- mysql 远程登陆
1.查询mysql是否启动 netstat -lnp|grep 3306 ps -df |grep mysqld 2.通过TCPIP的方式测试连接 mysql -uqingjiao -padm ...
- 错误Uncaught Error: Bootstrap's JavaScript requires jQuery at bootstrap.min.js:6 错误详解
引入Bootstrap的顺序进行修改 <% //获取以/开始,不以/结尾的部分 pageContext.setAttribute("APP_PATH", request.ge ...
- asp.net网站访问时不能显示页面
web => 属性 => web => servers =>create virtual directory
- 在本地环境(mac)启用https
前段时间客户一个涉及地理定位功能的页面突然出问题不能正常使用,在修复的过程中发现定位的方法 getCurrentPosition 只能在 https 协议下才能成功调用,这导致我在本地不能调试,每次修 ...
- 利用matlab自带函数graycoprops 实现基于共生矩阵的遥感图像纹理特征分析
close all;clear all;clc;I = imread('yaogan2.jpg');HSV = rgb2hsv(I);Hgray = rgb2gray(HSV);% 计算64位灰度共生 ...