【CF1247F】Tree Factory(构造)
题意:给定一棵n个点的树,要求将一条可以随意标号的链通过若干次操作变成这棵树
一次操作是指若v不为根且v的父亲不为根,则将v以及v的子树移到v的父亲的父亲上
要求给出标号方案,操作次数以及方案
n<=1e5
思路:考虑最小的操作次数,每一次操作可能使树的最大深度+1,事实上也存在这样的构造方案:
找到从根下来的最长链,找到深度最大的分叉点u,设最长链的后继为v,u的另一个儿子为w,则将v变成w的儿子
具体实现的时候可以用cnt记录当前节点上一个兄弟的最后一条链的深度
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,ll>P;
#define N 200010
#define M 200010
#define INF 1e9
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=1e9+,inv2=(MOD+)/;
double eps=1e-;
int dx[]={-,,,};
int dy[]={,,-,}; int head[N],vet[N],nxt[N],f[N],d[N],c[N],id[N],son[N],tot,cnt,ans,s; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void add(int a,int b)
{
nxt[++tot]=head[a];
vet[tot]=b;
head[a]=tot;
} void dfs(int u)
{
id[++s]=u;
rep(i,,cnt) c[++ans]=u;
cnt=;
int e=head[u];
while(e)
{
int v=vet[e];
if(v!=son[u]) dfs(v);
e=nxt[e];
}
if(son[u]) dfs(son[u]);
cnt++;
} int main()
{
int n=read(); d[]=;
rep(i,,n) head[i]=;
tot=;
rep(i,,n)
{
int x=read()+;
f[i]=x;
d[i]=d[x]+;
add(x,i);
}
int k=;
rep(i,,n)
if(d[i]>d[k]) k=i;
while(k>)
{
son[f[k]]=k;
k=f[k];
}
s=ans=;
dfs();
rep(i,,n) printf("%d ",id[i]-);
printf("\n");
printf("%d\n",ans);
rep(i,,ans) printf("%d ",c[i]-);
return ;
}
【CF1247F】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\)为树的深度,即任何点到根的最大边 ...
- C#结合Jquery LigerUI Tree插件构造树
Jquery LigerUI Tree是Jquery LigerUI()的插件之一,使用它可以快速的构建树形菜单.呵呵 废话不说了,直入正题,下面介绍C#结合ligerui 构造树形菜单的两种方法 1 ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造
C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- Huffman Tree 简单构造
//函数:构造Huffman树HT[2*n-1] #define MAXVALUE 9999//假设权值不超过9999 #define MAXLEAF 30 #define MAXNODE MAXLE ...
- Codeforces Round #453 (Div. 1) D. Weighting a Tree(构造)
题意 一个 \(n\) 个点 \(m\) 条边的无向连通图中每个点都有一个权值,现在要求给每条边定一个权值,满足每个点的权值等于所有相连的边权之和,权值可负. 题解 如果图是一棵树,那么方案就是唯一的 ...
- CF1003E Tree Constructing 构造+树论
正解:构造 解题报告: 传送门! 这题麻油翻译鸭,,,那就先大概港下题意趴QAQ 构造一棵n个点,直径为d,每个点点度不超过k的树 这题其实我jio得还是比较简单的趴,,, 首先构造出一条直径,就是一 ...
- 2018.09.22 atcoder Integers on a Tree(构造)
传送门 先考虑什么时候不合法. 第一是考虑任意两个特殊点的权值的奇偶性是否满足条件. 第二是考虑每个点的取值范围是否合法. 如果上述条件都满足的话就可以随便构造出一组解. 代码: #include&l ...
随机推荐
- linux/linux学习笔记-Shell基础(mooc)
一.shell概述 shell根据ascII表,将命令翻译为0101...传给内核执行. 内核->shell翻译为命令->用户(操作的界面就是shell,shell=翻译官) linux标 ...
- 基于 @Scheduled 注解的 ----定时任务
最常用的方法@Scheduled 注解表示起开定时任务 依赖 <dependencies> <dependency> <groupId>org.springfram ...
- Ubuntu 安装nodejs最新版本
sudo apt update -y sudo apt install -y npm sudo npm config set registry https://registry.npm.tao ...
- 记:第一次更新服务器CUDA和GPU驱动
因有需求需要改动centos7中的CUDA(更新到10)和GUP 的driver(更新到410)的版本. 事先需要查看原版本的信息,使用nvidia-smi可以查看driver的版本信息(最新的也显示 ...
- Dos - 学习总结(1)
1.控制台复制 1>鼠标右键,标记. 2>选定复制内容后,鼠标右键,完成复制. 2.
- JavaDoc注释
标签 说明 JDK 1.1 doclet 标准doclet 标签类型 @author 作者 作者标识 √ √ 包. 类.接口 @version 版本号 版本号 √ √ 包. 类.接口 @param 参 ...
- C++中的深拷贝和浅拷贝构造函数
1,对象的构造在实际工程开发当中是相当重要的,C++ 中使用类就要创建对象,这 就涉及了对象的构造,本节课讲解对象的构造和内存操作方面的问题: 2,实际工程开发中,bug 产生的根源,必然的会有内存操 ...
- 求大组合数mod p,(p不一定为质数)
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define N 2000005 ll p; ll ...
- time和datetime的区别
time在 Python 文档里,time是归类在Generic Operating System Services中,换句话说, 它提供的功能是更加接近于操作系统层面的.通读文档可知,time 模块 ...
- 使用axios发送ajax请求
1.安装 npm install axios 2.在Home.vue中引入 import axios from 'axios' export default { name: 'Home', c ...