首页 > 其他分享 >[ARC156C] Tree and LCS

[ARC156C] Tree and LCS

时间:2023-02-20 10:14:49浏览次数:67  
标签:LCS similarity Tree length subsequence common ARC156C longest

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;
}

标签:LCS,similarity,Tree,length,subsequence,common,ARC156C,longest
From: https://www.cnblogs.com/mekoszc/p/17136375.html

相关文章

  • D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths (cf741D)
    D.Arpa’sletter-markedtreeandMehrdad’sDokhtar-koshpaths(cf741D)tag:dsuontree,dp题目链接题意:给你一棵树,每一条边的权值是'a'到'v'的字母,求在每一个点......
  • LC 572. Subtree of Another Tree
    572.SubtreeofAnotherTree思路与题解这是一道挺有意思的问题,有三种解法,其中第二种是KMP算法的应用。筛法的原理参见204.CountPrimes。解法1:暴力dfs法。直接暴力......
  • CF1060F Shrinking Tree
    题面传送门考虑枚举最后剩下的点,然后令它为根。对于每个不是根的点,我们记\(ti_i\)表示\(i\)是什么时候和它的父亲合并的,\(op_i\)表示\(i\)在和父亲合并的时候是不......
  • 3、TreeMap源码解析
    目录1TreeMap基本介绍2红黑树数据结构回顾3成员变量4内部类Entry5构造函数6重要方法分析6.1get方法分析6.2put方法分析6.3插入调整函数fixAfterInsertion()解析6.......
  • element-ui的tree结构自定义节点点击是否可触发展开缩放
    近期公司项目,用element-ui的tree结构渲染一套数据,层级以两级或三级居多其中一级节点无实际意义,因此希望一级节点点击后正常展开缩放二级节点有实际意义,点击后,若下方有三......
  • WPF中使用LibVLCSharp.WPF 播放rtsp
    目录LibVLCSharp.WPF简介vlc:VideoView基本使用安装LibVLC播放rtsp引入命名空间xaml代码cs代码截图概述代码示例vlc:VideoView进阶使用空域问题......
  • 【CF207C3】Game with Two Trees
    【CF207C3】GamewithTwoTreesDescription有两颗动态加入点的树,每条边有一个字符每次加入完点后T1中到根的链在T2中的匹配次数之和Input一行一个数\(q\)Output输......
  • 208. Implement Trie (Prefix Tree)[Medium]
    208.ImplementTrie(PrefixTree)Atrie(pronouncedas"try")orprefixtreeisatreedatastructureusedtoefficientlystoreandretrievekeysinadataset......
  • TreeMap实现排序
    TreeMapTreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的比较器。当用Iterator遍历TreeMap时,得到的记录是排过序的。TreeMap取......
  • TreeMap是按照key的字典顺序来排序
    一、TreeMapTreeMap默认排序规则:按照key的字典顺序来排序(升序)字典排序(lexicographicalorder)是一种对于随机变量形成序列的排序方法。即按照字母顺序,或者数字小大顺序,由小......