传送门

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

每次操作相当于把一个兄弟变成儿子(我把你当兄弟你竟然想把我当儿子.jpg)

注意到每次操作最多只能使树的深度增加 $1$

因为链的深度为 $n$ 且形态唯一,那么只要把原树操作成深度为 $n$ 即可

现在得到了一个操作次数的下限,即 $n$ 减树的初始深度

考虑一下如果每次操作都能保证使树的深度增加,那么这样的一系列操作即为最优答案

事实上任何时刻都一定存在可以使长度增加的操作,考虑当前树从根节点出发的最长链,我们找到链上最深的存在分叉的节点 $x$

设 $v$ 为 $x$ 的儿子且在最长链上,$w$ 为 $x$ 的任意一个其他儿子,那么我们只要把 $v$ 变成 $w$ 的儿子即可使树深度增加

显然当树的深度不为 $n$ 时,最长链上一定存在分叉

然后现在问题就是输出方案了,这个东西不太好解释,看代码比较清楚吧...(代码很短)

注意一下代码实现的时候是链变成树而不是树变成链了

代码参考:wxhtxdy

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<vector>
  7. using namespace std;
  8. typedef long long ll;
  9. inline int read()
  10. {
  11. int x=,f=; char ch=getchar();
  12. while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
  13. while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
  14. return x*f;
  15. }
  16. const int N=1e5+;
  17. int n,fa[N],dep[N],son[N];
  18. vector <int> V[N],id,mov;
  19. int cnt;//这个东西表示当前节点上一个兄弟的最后一条链的深度
  20. void dfs(int x)
  21. {
  22. id.push_back(x);
  23. for(int i=;i<=cnt;i++) mov.push_back(x);
  24. cnt=;
  25. for(int v: V[x]) if(v!=son[x]) dfs(v);
  26. if(son[x]) dfs(son[x]);//最长链最后走
  27. cnt++;
  28. }
  29. int main()
  30. {
  31. n=read(); dep[]=;
  32. for(int i=;i<=n;i++)
  33. {
  34. int a=read()+; fa[i]=a;
  35. dep[i]=dep[a]+; V[a].push_back(i);
  36. }
  37. int t=max_element(dep+,dep+n+)-dep;
  38. while(t!=) son[fa[t]]=t,t=fa[t];//找最长链
  39. dfs();
  40. for(int x: id) printf("%d ",x-); puts("");
  41. printf("%d\n",int(mov.size()));
  42. for(int x: mov) printf("%d ",x-); puts("");
  43. return ;
  44. }

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. OpenFOAM 中的边界条件(一)【转载】

    链接:http://xiaopingqiu.github.io/2016/04/02/Boundary-conditions-in-OpenFOAM1/ 本系列解读 OpenFOAM 中边界条件的实现 ...

  2. elasticsearch java索引的增删改查

    1.创建索引并插入数据 Map<String, Object> json = new HashMap<String, Object>(); json.put("use ...

  3. ORM SQLAlchemy 简介

    对象关系映射(Object Relational Mapping,简称ORM使用DB-API访问数据库,需要懂 SQL 语言,能够写 SQL 语句,如果不想懂 SQL,又想使用关系型数据库,可以使用 ...

  4. 微信小程序之--(与唯品会来场粉红色的邂逅 ???)

    Welcome to miaomiaoXiong's segmentfault 微信小程序之--(与唯品会来场粉红色的邂逅 ???) 买买买,虽然双十二刚过,可是唯品会的折扣却是依然火爆.一打开页面, ...

  5. Jmeter Web 性能测试入门 (六):Jmeter 解析 response 并传递 value

    解析response中的内容,并把获取到的value传递到后续的request中,常用的方法就是在想要解析response的request上添加后置处理器 本章介绍两种常用的组件 BeanShell ...

  6. 美国top200药品2

     python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  7. org/springframework/cache/jcache/config/AbstractJCacheConfiguration.class

    在使用Spring-MVC环境时  报错: Failed to parse configuration class [org.springframework.cache.aspectj.AspectJ ...

  8. RAID概念记录

    之前对RAID概念有一些基本的认知,这次同事培训k8s 的持久卷,提到了RAID的一些概念和用法,记录一下. RAID ( Redundant Array of Independent Disks ) ...

  9. React Native登录注册页面实现空白处收起键盘

    其实很简单,直接使用ScrollView作为父视图即可.有木有很神奇啊,以前都还不知道呢.....

  10. 在本地环境(mac)启用https

    前段时间客户一个涉及地理定位功能的页面突然出问题不能正常使用,在修复的过程中发现定位的方法 getCurrentPosition 只能在 https 协议下才能成功调用,这导致我在本地不能调试,每次修 ...