[ARC156C] Tree and LCS
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的更多相关文章
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree
原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...
- 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)
议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...
- CF1208 Red Blue Tree
题目链接 问题分析 这是蒟蒻第一道3500!不过话说luogu上两个题解的程序都是假的可还行(2019.11.1)-- 为了方便叙述,下面我们约定 : \([c]\) 的值为 \(1\) 当且仅当 \ ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
随机推荐
- 《深入理解Java虚拟机》读书笔记:字节码指令简介
字节码指令简介 Java虚拟机的指令由一个字节长度的.代表着某种特定操作含义的数字(称为操作码,Opcode)以及跟随其后的零至多个代表此操作所需参数(称为操作数,Operands)而构成.由于Jav ...
- ChatGPT应用篇:如何快速生成精美PPT提高工作效率-附资料下载
一.ChatGPT生成markdown源代码 问: 我想做一份ChatGPT变现方法的PPT,请生成丰富的教学展示内容,因为生成PPT是需要MarkDown格式的,请您输出Markdown格式的内容 ...
- 重要变更 | Hugging Face Hub 的 Git 操作不再支持使用密码验证
在 Hugging Face,我们一直致力于提升服务安全性,因此,我们将对通过 Git 与 Hugging Face Hub 交互时的认证方式进行更改.从 2023 年 10 月 1 日 开始,我们将 ...
- 发布策略:蓝绿部署、金丝雀发布(灰度发布)、AB测试、滚动发布、红黑部署的概念与区别
蓝绿发布(Blue-Green Deployment) 蓝绿发布提供了一种零宕机的部署方式.不停老版本,部署新版本进行测试,确认OK,将流量切到新版本,然后老版本同时也升级到新版本.始终有两个版本同时 ...
- 《Hadoop大数据技术开发实战》新书上线
当今互联网已进入大数据时代,大数据技术已广泛应用于金融.医疗.教育.电信.政府等领域.各行各业每天都在产生大量的数据,数据计量单位已从B.KB.MB.GB.TB发展到PB.EB.ZB.YB甚至BB.N ...
- freeswitch sofia协议栈调试
概述 freeswitch是一款简单好用的VOIP开源软交换平台. fs内部使用sofia的sip协议栈,本文介绍如何调试跟踪sofia协议栈. 环境 centos:CentOS release 7 ...
- C++的动态分派在HotSpot VM中的重要应用
众所周知,多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定.C++ 和 Java 作为当前最为流行的两种面向对象编程语言,其内部对于多态的支持 ...
- Trie字典
Trie树,又叫字典树,前缀树(Prefix Tree),单词查找树,是一种多叉树的结构. {"a","apple","appeal",&q ...
- FreeSWITCH添加h264编码及pcap视频提取
操作系统 :CentOS 7.6_x64.Windows 10_x64 FreeSWITCH版本 :1.10.9 Python版本:3.9.2 一.启用h264相关模块 这里以 mod_openh26 ...
- 前端三件套系例之JS——JS的BOM操作、JS的DOM操作
文章目录 1 JS的BOM操作 1.介绍 2.window对象 2-1 代码 3.window的子对象 3-1 navigator对象(了解即可) 3-2 screen对象(了解即可) 3-3 his ...