Problem Statement

We have a tree $T$ with vertices numbered $1$ to $N$. The $i$-th edge of $T$ connects vertex $u_i$ and vertex $v_i$.

Let us use $T$ to define the similarity of a permutation $P = (P_1,P_2,\ldots,P_N)$ of $(1,2,\ldots,N)$ as follows.

  • For a simple path $x=(x_1,x_2,\ldots,x_k)$ in $T$, let $y=(P_{x_1}, P_{x_2},\ldots,P_{x_k})$. The similarity is the maximum possible length of a longest common subsequence of $x$ and $y$.

Construct a permutation $P$ with the minimum similarity.

What is a subsequence?

A subsequence of a sequence is a sequence obtained by removing zero or more elements from that sequence and concatenating the remaining elements without changing the relative order.

For instance, \((10,30)\) is a subsequence of \((10,20,30)\), but \((20,10)\) is not.

What is a simple path?

For vertices $X$ and $Y$ in a graph $G$, a walk from $X$ to $Y$ is a sequence of vertices $v_1,v_2, \ldots, v_k$ such that $v_1=X$, $v_k=Y$, and there is an edge connecting $v_i$ and $v_{i+1}$. A simple path (or simply a path) is a walk such that $v_1,v_2, \ldots, v_k$ are all different.

Constraints

  • $2 \leq N \leq 5000$
  • $1\leq u_i,v_i\leq N$
  • The given graph is a tree.
  • All numbers in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$

Output

Print a permutation $P$ with the minimum similarity, separated by spaces. If multiple solutions exist, you may print any of them.


Sample Input 1

3
1 2
2 3

Sample Output 1

3 2 1

This permutation has a similarity of $1$, which can be computed as follows.

  • For $x=(1)$, we have $y=(P_1)=(3)$. The length of a longest common subsequence of $x$ and $y$ is $0$.

  • For $x=(2)$, we have $y=(P_2)=(2)$. The length of a longest common subsequence of $x$ and $y$ is $1$.

  • For $x=(3)$, we have $y=(P_2)=(1)$. The length of a longest common subsequence of $x$ and $y$ is $0$.

  • For $x=(1,2)$, we have $y=(P_1,P_2)=(3,2)$. The length of a longest common subsequence of $x$ and $y$ is $1$. The same goes for $x=(2,1)$, the reversal of $(1,2)$.

  • For $x=(2,3)$, we have $y=(P_2,P_3)=(2,1)$. The length of a longest common subsequence of $x$ and $y$ is $1$. The same goes for $x=(3,2)$, the reversal of $(2,3)$.

  • For $x=(1,2,3)$, we have $y=(P_1,P_2,P_3)=(3,2,1)$. The length of a longest common subsequence of $x$ and $y$ is $1$. The same goes for $x=(3,2,1)$, the reversal of $(1,2,3)$.

We can prove that no permutation has a similarity of $0$ or less, so this permutation is a valid answer.

首先看样例很容易知道,答案至多为1.

那么想要使两个串的 LCS 为1,可以尝试不断剖叶子构造。

维护一个叶子队列,每次取出两个叶子 \(a,b\),令 \(P_a=b,P_b=a\)。这样构造是合法的。

首先假设证明了之前的链的的 LCS 都是 1,此时加入这样两个叶子,那么如果想要让 LCS 更大,需要使选的序列在 \(a,b\) 的后面,那么此时 LCS 仍是1.

#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int fa[N],in[N],l=1,r=0,q[N],n,rt,hd[N],e_num,p[N];
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
int main()
{
scanf("%d",&n);
if(n==2)
{
puts("2 1");
return 0;
}
for(int i=1,u,v;i<n;i++)
scanf("%d%d",&u,&v),in[u]++,in[v]++,add_edge(u,v),add_edge(v,u);
for(int i=1;i<=n;i++)
if(in[i]==1)
rt=i,q[++r]=i;;
while(l<r)
{
p[q[l]]=q[l+1];
p[q[l+1]]=q[l];
for(int i=hd[q[l]];i;i=e[i].nxt)
{
in[e[i].v]--;
if(in[e[i].v]==1)
q[++r]=e[i].v;
}
for(int i=hd[q[l+1]];i;i=e[i].nxt)
{
in[e[i].v]--;
if(in[e[i].v]==1)
q[++r]=e[i].v;
}
l+=2;
}
// printf("%d %d\n",l,r);
if(l==r)
p[q[l]]=q[l];
for(int i=1;i<=n;i++)
printf("%d ",p[i]);
return 0;
}

[ARC156C] Tree and LCS的更多相关文章

  1. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  2. 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree

    原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...

  3. 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)

    议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...

  4. CF1208 Red Blue Tree

    题目链接 问题分析 这是蒟蒻第一道3500!不过话说luogu上两个题解的程序都是假的可还行(2019.11.1)-- 为了方便叙述,下面我们约定 : \([c]\) 的值为 \(1\) 当且仅当 \ ...

  5. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  6. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  7. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  8. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  10. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

随机推荐

  1. 小白python和pycharm安装大佬勿扰

    编程语言发展和Python安装 计算机语言的发展 机器语言 1946年2月14日,世界上第一台计算机ENIAC诞生,使用的是最原始的穿孔卡片.这种卡片上使用的语言是只有专家才能理解的语言,与人类语言差 ...

  2. Row Major

    Smiling & Weeping ----昨天, 别人在我身旁大声说出你的名字, 这对于我, 像从敞开的窗口扔进了一朵玫瑰花. 思路:不客气地说,这是一道令人费解的题目,要求构造一个字符串, ...

  3. Mysql进击篇-存储引擎、索引、sql优化、视图、锁、innoDb、管理

    1.存储引擎 (1)连接层 最上层是一些客户端和连接服务,主要完成一些类似于连接处理,授权认证.以及相关的安全方案,服务器也会为安全接入的每个客户端验证它所具有的操作权限 (2)服务层 第二层架构主要 ...

  4. redis基本数据类型 set类型

    127.0.0.1:6379> SADD s1 a b c (integer) 3 127.0.0.1:6379> SMEMBERS s1 1) "b" 2) &quo ...

  5. 文心一言 VS 讯飞星火 VS chatgpt (105)-- 算法导论10.1 3题

    三.用go语言,仿照图 10-2,画图表示依次执行操作 ENQUEUE(Q,4).ENQUEUE(Q,1).ENQUEUE(Q,3).DEQUEUE(Q).ENQUEUE(Q,8)和 DEQUEUE( ...

  6. 其它-Supervisor的使用

    文章目录 Supervisor 的使用 一 Supervisor介绍 二 安装 2.1 安装方式 2.2 验证 2.3 配置 2.4 配置详情(了解) 2.5 启动.停止.重启 三 program 配 ...

  7. Markdown · Typora | 基本画图技巧

    如果想画一些简单的状态图,可以使用 typora 自带的 mermaid 工具. (mermaid 不止能画简单的状态图,还能画流程图等,详见参考资料) 定义节点 可以定义不同形状的节点,并为节点添加 ...

  8. 基于Spring事件驱动模式实现业务解耦

    事件驱动模式 举个例子 大部分软件或者APP都有会有会员系统,当我们注册为会员时,商家一般会把我们拉入会员群.给我们发优惠券.推送欢迎语什么的. 值得注意的是: 注册成功后才会产生后面的这些动作: 注 ...

  9. chatgpt与搜索结合,百度会在这股浪潮下掘金吗?

    年末,在百度内部沟通会上,百度创始人李彦宏提到最近他的朋友们问他如何看待ChatGPT,包括他周围同行业中的同一领域和跨境人士. ​事实上,早在ChatGPT将他的对话能力和直观的互动方式展现给全世界 ...

  10. 解决因对EFCore执行SQL方法不熟练而引起的问题

    前言 本文测试环境:VS2022+.Net7+MySQL 因为我想要实现使用EFCore去执行sql文件,所以就用到了方法ExecuteSqlAsync,然后就产生了下面的问题,首先因为方法接收的参数 ...