传送门

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

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

    Hdu5762 题意: 你n个点,让你找两个数对,A,B和C,D,使得A和B的曼哈顿距离等于C和D的曼哈顿距离,问是否存在这样的对,A!=C且B!=D. 解法: 直接暴力判断,时间复杂度是 $ O(n ...

  2. MD5介绍

    md5介绍 1. md5简介 md5的全称是md5信息摘要算法(英文:MD5 Message-Digest Algorithm ),一种被广泛使用的密码散列函数,可以产生一个128位(16字节,1字节 ...

  3. Nginx 之 Location 的整理

    1. Location 的整理 在将配置解析完后,所有的 location 此时都以 tree 的形式组织起来,具体可参考 Nginx之 Location 的生成. 此时需要对所有 server 下的 ...

  4. PHP多进程开发与Redis结合实践

    原文:https://blog.51cto.com/laok8/2107892?source=drh 业务逻辑介绍: 用户在 APP 上发帖子,然后存储到 Redis 的 List 列表中 利用 Li ...

  5. 如何捕捉Desried Capabilities中的appPackafe和appActive

    捕捉这两个参数需要借助adb工具的日志进行分析.ADB是一种命令行工具,用于PC和Android模拟器之前连接通信,集成在Android ADK中,默认在platfrom-tools目录下.在cmd运 ...

  6. [go]ini配置文件解析

    // config.ini [app] server.port = 8080 name = resk enabled = false time = 10s ;我是一个注释 #mysql数据库配置 [m ...

  7. kotlin 泛型函数

    fun<T> singletonList(item:T):List<T>{ ..... } fun<T>T.basicToString():String{ .... ...

  8. [服务器时区问题]PHP Warning: strftime(): It is not safe to rely on the system's timezone set

    PHP Warning: strftime(): It is not safe to rely on the system's timezone set 当运行一些程序时,在httpd日志中会有如下警 ...

  9. Sql题目精选练习

    1.每日经典sql 1.1.1 根据三张关系表查询雇员中工资最高的雇员的员工姓名.工资和部门号. salary(工资表) employee(员工表) department(部门表) Sql语句: SE ...

  10. TensorFlow Object Detection API —— 制作自己的模型

    https://blog.csdn.net/qq_24474463/article/details/81530900 (t20190518) luo@luo-All-Series:~/MyFile/T ...