当前位置:网站首页>Loj#2324-「清华集训 2017」小 Y 和二叉树
Loj#2324-「清华集训 2017」小 Y 和二叉树
2022-07-17 13:56:00 【QuantAsk】
正题
题目大意
给出 n n n个点的一棵树,每个点的度数不超过 3 3 3。
你要求它的一个二叉树结构(根任意选择)使得其中序遍历的字典序最小。
1 ≤ n ≤ 1 0 6 1\leq n\leq 10^6 1≤n≤106
解题思路
直接找根感觉比较麻烦,我们考虑先确定中序遍历中的第一个点。
显然这个点是最小的一个度数不为 3 3 3的节点,我们设为 x x x。
此时 x x x的左子树肯定没有节点,然后考虑它连接的点安排到右子树或者父节点。
先假设 x x x的度数是 2 2 2,因为下一步是遍历 x x x的右子树,所以我们优先比对两个连接的部分作为子树时字典序最小的第一个数是啥。
因为每个点的度数都不超过 3 3 3,整张图可能出现的子树(不同的根)数量为 2 n − 2 2n-2 2n−2个(每条边的两个方向),我们可以先预处理出每个子树字典序最小时第一个是啥。
这样我们就能快速比较了。
然后考虑 x x x的度数是 1 1 1的时候,记它连接的节点是 y y y,
- 若 y y y的度数为 0 0 0,那随便丢哪都一样。
- 若 y y y的度数为 1 1 1,显然 y y y子节点时可以控制它和它子树的顺序,丢右子树肯定最优。
- 若 y y y的度数为 2 2 2,考虑丢右子树的优势是它一定可以控制左右两棵子树的顺序,而丢父节点的优势是可以优先把 y y y遍历掉,我们比较 y y y和 y y y作为儿子时子树中最小字典序的第一个数,这样就可以确定丢哪了。
至于被丢到子树里面的,我们上面的预处理可以确定子树里面的顺序。
时间复杂度: O ( n ) O(n) O(n)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+10;
int n,tot,rt,k[N],f[N][3],g[N][3],t[N][2];
bool v[N];
void dfs(int x,int fa,int p){
if(f[fa][p]<=n+1)return;
if(k[x]==3)f[fa][p]=n+1;
else f[fa][p]=x;
for(int i=0;i<k[x];i++){
int y=g[x][i];
if(y==fa)continue;dfs(y,x,i);
f[fa][p]=min(f[fa][p],f[x][i]);
}
return;
}
void del(int x,int y){
for(int i=0;i<k[x];i++)
if(g[x][i]==y){
swap(g[x][i],g[x][k[x]-1]);
swap(f[x][i],f[x][k[x]-1]);
k[x]--;
}
return;
}
void solve(int x){
rt=x;v[x]=1;
if(!k[x])return;
if(k[x]==1){
int y=g[x][0];
if(k[y]==3&&y<f[x][0])
del(y,x),t[y][0]=x,solve(y);
else del(y,x),t[x][1]=y;
}
else{
if(f[x][0]>f[x][1])swap(g[x][0],g[x][1]);
int y=g[x][1];t[x][1]=g[x][0];
del(y,x);del(g[x][0],x);
t[y][0]=x;solve(g[x][1]);
}
return;
}
void print(int x){
if(!x)return;
if(v[x]){
print(t[x][0]);
printf("%d ",x);
print(t[x][1]);
}
else if(k[x]==2){
if(f[x][0]>f[x][1])swap(g[x][0],g[x][1]);
del(g[x][0],x);print(g[x][0]);
printf("%d ",x);
del(g[x][1],x);print(g[x][1]);
}
else if(k[x]==1){
if(x<f[x][0])printf("%d ",x);
del(g[x][0],x);print(g[x][0]);
if(x>f[x][0])printf("%d ",x);
}
else printf("%d ",x);
}
int main()
{
memset(f,0x3f,sizeof(f));
scanf("%d",&n);tot=1;
for(int i=1;i<=n;i++){
scanf("%d",&k[i]);
for(int j=0,x;j<k[i];j++)scanf("%d",&g[i][j]);
}
for(int i=1;i<=n;i++)
for(int j=0;j<k[i];j++)dfs(g[i][j],i,j);
int x=0;
for(int i=1;i<=n;i++)
if(k[i]<=2){
x=i;break;}
solve(x);
print(rt);
return 0;
}
边栏推荐
- ROS duplicate name
- Nombre d'entrées nombre d'entrées numériques pures limite de longueur maximale
- Satellite network capacity improvement method based on network coding
- 6G中的卫星通信高效天基计算技术
- Paper notes: mind the gap an empirical evaluation of impaction ofmissing values techniques in timeseries
- 6G smart endogenous: technical challenges, architecture and key features
- UE4 understanding of animation blueprint
- Antd drop-down multiple options to transfer values to the background for query operations
- Environment variable configuration of win10
- LeetCode 2335. Minimum total time required to fill the cup
猜你喜欢

过拟合与欠拟合

ROS 重名

Detailed explanation of Euler angle, axis angle, quaternion and rotation matrix

Game theory (Depu) and investment (40/100)

LeetCode 2331. Calculate the value of Boolean binary tree (tree traversal)

Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)

OpenCV编程:OpenCV3.X训练自己的分类器

Svn learning

How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example

军品研制过程所需文件-进阶版
随机推荐
Pytoch and weight decay (L2 norm)
Introduction to the universal theme system of SAP appgyver
SAP S4 material management inventory module mard database table reading technical details
ENVI_ Idl: use the inverse distance weight method to select the nearest n points for interpolation (bottom implementation) and output them to GeoTIFF format (the effect is equivalent to the inverse di
[LeetCode周赛复盘] 第 302 场周赛20220717
2022/7/16
树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)
MySQL query error
Google Earth Engine APP(GEE)—设定中国区域的一个夜间灯光时序分析app
Environment variable configuration of win10
How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example
mpu9250 ky9250姿态、角度模块和mpu9250 mpl dma对比
Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想
About hping streaming test tool
E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)
Future applications and technical challenges of backscatter communication
[leetcode weekly replay] 302 weekly 20220717
最大半连通子图(tarjan缩点+拓扑排序+dp最长链)
Accident caused by the combination of annotation transaction + distributed lock at the read committed level -- disorder of activity purchase opportunities
[PostgreSQL] PostgreSQL 15 optimizes distinct