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\) 题解 ...
随机推荐
- JAVA - IO - IO的类型(AIO, BIO, NIO)
IO的方式通常分为几种,同步阻塞的BIO.同步非阻塞的NIO.异步非阻塞的AIO. 一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSock ...
- IdentityServer4入门一
这几天学习IdentityServer4,感觉内容有点乱,也可能自己水平有限吧.但为了巩固学习的内容,也打算自己理一下思路. 首先IdentityServer解决什么问题? 下图是我们的一个程序的组织 ...
- Oracle中shrink space命令
shrink_clause: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_3001.htm#i2192484 ...
- django 2.2和mysql使用的常见问题
可能是由于Django使用的MySQLdb库对Python3不支持,我们用采用了PyMySQL库来代替,导致出现各种坑,特别是执行以下2条命令的是时候: python manage.py makemi ...
- Build Telemetry for Distributed Services之OpenTracing实践
官网:https://opentracing.io/docs/best-practices/ Best Practices This page aims to illustrate common us ...
- .net framework 4.0 安装失败解决办法
方法一 1.打开cmd命令窗口 运行net stop WuAuServ 停止更新服务 2.开始----运行------输入%windir% 3.找到SoftwareDistribution的 ...
- 怎样获取java新IO的Path文件大小
import org.junit.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file. ...
- hdfs操作命令
文件操作命令:hdfs dfs -ls /hdfs dfs -mkdir /hdfs dfs -rm -rf /hdfshdfs dfs -duhdfs dfs -get /hdfs /localhd ...
- 解决报错Fatal error in launcher
换电脑重装python,打算安装第三方库的时候出现错误: Fatal error in launcher 然而在网上搜到的大多数是解决 —— Fatal error in launcher: Unab ...
- 【ABAP系列】SAP ABAP 字符编码与解码、Unicode
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 字符编码与解码 ...