当前位置:网站首页>LOJ 2324 - "Tsinghua training 2017" small y and binary tree
LOJ 2324 - "Tsinghua training 2017" small y and binary tree
2022-07-19 11:12:00 【QuantAsk】
On the subject
Topic link :https://loj.ac/p/2324
The main idea of the topic
give n n n A tree at a point , The degree of each point does not exceed 3 3 3.
You ask for a binary tree structure ( Root arbitrary choice ) Make the lexicographic order of its traversal smallest .
1 ≤ n ≤ 1 0 6 1\leq n\leq 10^6 1≤n≤106
Their thinking
It's troublesome to find the root directly , Let's first determine the first point in the middle order traversal .
Obviously, this point is the smallest degree, not 3 3 3 The node of , We set it as x x x.
here x x x The left subtree of must have no nodes , Then consider that the points it connects are arranged to the right subtree or parent node .
To assume that x x x What's the degree of this 2 2 2, Because the next step is to traverse x x x The right subtree , So we first compare the two connected parts as the subtree, what is the first number with the smallest dictionary order .
Because the degree of each point does not exceed 3 3 3, Possible subtrees of the whole picture ( Different roots ) The number of 2 n − 2 2n-2 2n−2 individual ( Two directions of each side ), We can preprocess what is the first one with the smallest dictionary order of each subtree .
So we can quickly compare .
Then consider x x x What's the degree of this 1 1 1 When , Remember that the node it connects is y y y,
- if y y y The degree is 0 0 0, It's the same everywhere .
- if y y y The degree is 1 1 1, obviously y y y You can control the order of the child node and its subtree , Losing the right subtree is definitely the best .
- if y y y The degree is 2 2 2, The advantage of considering losing a right subtree is that it You can definitely make it Control the order of the left and right subtrees , The advantage of losing the parent node is that you can give priority to y y y Traverse out , We compare y y y and y y y As the first number of the smallest dictionary order in the son time subtree , In this way, we can determine where we lost it .
As for those thrown into the subtree , The above preprocessing can determine the order in the subtree .
Time complexity : 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;
}
边栏推荐
- Qt 两个重载QListWidget控件对象实现selectitem drag拖拽
- antd 下拉多选传值到后台做查询操作
- Google Earth engine app (GEE) - set up a nighttime lighting timing analysis app in China
- 腾讯云服务器利用镜像部署WordPress个人网站!
- Powercli script performance optimization
- 如何在 RHEL 9 中更改和重置忘记的root密码
- To get to the bottom: Principle Analysis of Objective-C correlation attribute
- Introduction to virtualization troubleshooting
- Some methods of early MCU encryption [get data in the comment area]
- PowerCLI 脚本性能优化
猜你喜欢

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

Win10安装Apache Jena 3.17

vulnhub inclusiveness: 1

mpu9250 ky9250姿态、角度模块和mpu9250 mpl dma对比

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

Four methods of traversing key value in map

【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码

PPDE第二季度迎新 | 欢迎22位AI开发者加入飞桨开发者技术专家计划!

(一)了解MySQL

LeetCode 2319. Judge whether the matrix is an X matrix
随机推荐
2022/7/14
Pytoch and weight decay (L2 norm)
6G全域融合网络展望
AT5147-[AGC036D]Negative Cycle【dp,模型转换】
pjudge#21652-[PR #4]到底有没有九【数位dp】
37. Flex layout
【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
Integrated network architecture and network slicing technology of air, earth and sea
OA系统与MES系统的异同点
Unity高版本退回低版本报错问题
nodeJS中promise对象对结果简便输出办法(建议使用异步终极方案 async+await)
Loj#2324-「清华集训 2017」小 Y 和二叉树
树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)
LeetCode 2315. Statistical asterisk (string)
LeetCode 2319. Judge whether the matrix is an X matrix
Beego framework realizes file upload + seven cattle cloud storage
SSM使用poi将数据导出到excel
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
web安全入门-部署Snort开源IDS/IPS系统
IP SAN has an independent file system. After the application server accesses the IP SAN through the network sharing protocol, it can read and write the files in the file system