当前位置:网站首页>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;
}
边栏推荐
- Cmake常用命令(五)
- Antd drop-down multiple options to transfer values to the background for query operations
- 如何在 RHEL 9 中更改和重置忘记的root密码
- SSM uses POI to export data to excel
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- Connected graph (union search set)
- How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example
- Win10的环境变量配置
- Data Guard Broker的概念和Data Guard Broker的配置过程
- Unity3d 模型中心点的转换(源代码)
猜你喜欢

Second classification learning is extended to multi classification learning

论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries

Win10 install Apache Jena 3.17

Evaluation method of machine learning model

剑指 Offer II 041. 滑动窗口的平均值

Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)

How does unity3d use the asset store to download some useful resource packages

英伟达用AI设计GPU:最新H100已经用上,比传统EDA减少25%芯片面积

SVN学习

每日刷题记录 (二十六)
随机推荐
How does unity3d use the asset store to download some useful resource packages
Connected graph (union search set)
UE4 understanding of animation blueprint
Efficient space-based computing technology for satellite communication in 6g
Introduction to virtualization troubleshooting
Huawei machine test: number of continuous licensing
LeetCode 2315. Statistical asterisk (string)
After summarizing the surface based knowledge of the database
Unity3d 读取mpu9250 例子原代码
【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
Rotation in unity3d
Vérification logique complexe personnalisée lors de l'ajout et de la modification - 2022 nouvel élément
Google Earth Engine APP(GEE)—设定中国区域的一个夜间灯光时序分析app
Definable 6G security architecture
如何在 RHEL 9 中更改和重置忘记的root密码
Win10的环境变量配置
Pytoch framework learning record 1 cifar-10 classification
Win10安装Apache Jena 3.17
Unity3d 模型中心点的转换(源代码)
Introduction to the universal theme system of SAP appgyver