传送门

正难则反,把链操作成树不好想,那么考虑一下如何把树变成链

每次操作相当于把一个兄弟变成儿子(我把你当兄弟你竟然想把我当儿子.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的更多相关文章

  1. 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 ...

  2. Codeforces 1246D/1225F Tree Factory (构造)

    题目链接 https://codeforces.com/contest/1246/problem/D 题解 首先考虑答案的下界是\(n-1-dep\) (\(dep\)为树的深度,即任何点到根的最大边 ...

  3. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  4. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  5. codeforces 627B B. Factory Repairs(线段树)

    B. Factory Repairs time limit per test 4 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces 570D - Tree Requests【树形转线性,前缀和】

    http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...

  7. Codeforces 23E Tree

    http://codeforces.com/problemset/problem/23/E 题意:给一个树,求砍断某些边,使得所有联通块大小的乘积最大.思路:f[i][j]代表当前把j个贡献给i的父亲 ...

  8. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

  9. [Educational Round 17][Codeforces 762F. Tree nesting]

    题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...

随机推荐

  1. oracle清除归档

    清除Oracle归档日志命令echo -e 'delete noprompt archivelog ALL COMPLETED BEFORE '\'SYSDATE-${DELETE_ARCHIVELO ...

  2. Golang字符串处理以及文件操作

    一.整数 1.int与uint的初值比较以及其大小. 1 /* 2 #!/usr/bin/env gorun 3 @author :xxxx 4 Blog:http://www.cnblogs.com ...

  3. PyTricks-How to Sort a Python dict

    字典的键值排序 import operator # 1表示按照值排序 xs = {"a": 4, "b": 3, "c": 2, " ...

  4. 关于mysql数据库远程访问

    mysql数据库安装默认为只能本地访问,若需远程连接需根据不同的操作系统做一些操作 Windows: 新装的mysql本地无法登录,显示为1045错误 mysql#1045(1045Access de ...

  5. ArcGIS超级工具SPTOOLS-拓扑错误处理

    1.1  删除线面直线上的点 操作视频: https://weibo.com/tv/v/Hxjgmuv6F?fid=1034:4379388532225679 删除面要素.线要素一条边直线上的点. 1 ...

  6. Django module

    1,模型定义 models.py的例子: class Author(models.Model): name=models.CharField(max_length=20) class Book(mod ...

  7. Android自定义控件之基本图形绘制

    入门示例代码 在Android中Paint类就是画笔,Canvas 就是画布 对于画笔的大小,粗细,颜色,透明度都在paint类中设置,对于画出的成品,比如圆,方形,等在canvas类中的函数生成 入 ...

  8. kotlin泛型基本使用

    class box<T> (t :T){ var vlaue =t } fun main(arg: Array<String>) { val box1:box<Int&g ...

  9. Markdown 图片的简单处理

    0. 前言 最近写 md 文章的时候发现,在 markdown 里插入一些很长的图片的时候,会显得很不好看,于是去查了一下如何实现 markdown 里图片的并排显示,参考了各个博客内的内容和 mar ...

  10. jpa 总结

    转:http://blog.csdn.net/linzhiqiang0316/article/details/52639265 先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 S ...