Problem Statement

     Elly has a graph with N+1 vertices, conveniently numbered from 0 to N. The graph is actually a rooted tree, with the root being the vertex with number zero.

Elly can move between the vertices of her tree by jumping from one vertex to another. Not all jumps are allowed. Elly may jump from vertex A to vertex B if and only if one of A and B is a (direct or indirect) descendant of the other.

Elly is currently standing in the root of the tree: vertex 0. She would like to make a sequence of N jumps that visits each of the other N vertices exactly once. Note that Elly is allowed to jump over previously visited vertices. For example, if A is an ancestor of B and B is an ancestor of C, Elly can jump from A to C or from C to A even if B has been already visited.

You are given the description of the tree: a vector <int> parent with N elements. For each i between 0 and N-1, inclusive, the vertex parent[i] is the parent of the vertex (i+1). If it is possible for Elly to visit each of the vertices 1 through N exactly once, return a vector <int> with N elements: the numbers of the vertices in the order in which she should visit them. If there is more than one possible answer, return the lexicographically smallest one. If there is no way to achieve her goal, return an empty vector <int> instead.

Definition

    
Class: EllysTree
Method: getMoves
Parameters: vector <int>
Returns: vector <int>
Method signature: vector <int> getMoves(vector <int> parent)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256

Notes

- A tree is a connected graph with N+1 vertices and N edges. A rooted tree is a tree in which one vertex is labeled as the root.
- In a rooted tree, the parent of vertex X is the first vertex on the path from X to the root. The root has no parent.
- In a rooted tree, vertex X is a descendant of vertex Y if Y lies on the path from X to the root.
- Given two equally long but different sequences of integers A and B, A is said to be lexicographically smaller than B if A contains a smaller number on the first position where they differ.

Constraints

- parent will contain between 1 and 100 elements, inclusive.
- Each element of parent will be between 0 and |parent|, inclusive, where "|parent|" denotes the number of elements in parent (i.e. N).
- It is guaranteed, that the given graph will be a valid rooted tree.

Examples

0)  
    
{9, 13, 7, 9, 8, 14, 14, 0, 6, 9, 2, 2, 5, 5, 7}
Returns: {1, 5, 2, 11, 13, 12, 8, 3, 7, 15, 14, 4, 6, 9, 10 }
The nodes Elly can jump to from node 6 are: {0, 8, 5, 14, 9, 10, 1, 4}.
1)  
    
{3, 4, 5, 0, 2}
Returns: {1, 2, 3, 4, 5 }
There are no branches in this tree, thus Elly can traverse it in any order.
2)  
    
{0, 0}
Returns: { }
The root has two children. No matter which of them Elly chooses first, she will not be able to get to the other, since the girl has to go back to the root, which is already visited.
3)  
    
{0, 6, 6, 2, 6, 1, 3, 5}
Returns: {2, 4, 1, 3, 7, 6, 5, 8 }
 

题意:给定一棵有根数,标号为0~n,你可以从根(0号点)开始,在这棵树上跳。你只能在子孙与祖先跳跃。要求跳跃n次后访问每个点各一次,并使访问序列字典序最小。

题解:

首先想到的是如何构造出一个可行的方案。我们把跳跃分为向上跳(深度减少)和向下跳 (深度增加)两种。显然,叶节点只可以通过向下跳来访问,访问后也只能向上跳。对于一个非根的非叶节点,其叶节点一定会被访问,我们可以使所有的非根的非叶节点都通过向上跳来访问。

这样,构造一个可行序列的方法就是:先从根跳到一个叶节点上,在逐渐向上跳,直到跳到一个子树没有都被访问的节点。然后再跳到其子树上的一个未被访问的叶节点(一定存在),重复操作。

这样的过程可以看做是用非叶节点去消掉其下方的叶节点,可以用DP处理。

因为答案要求字典序最小,我们可以每次枚举跳到哪个点上,再DP验证之后是否存在可行方案。注意DP时应忽略被访问过的点,对于当前位于的点要特殊考虑。

步数复杂度为n,枚举复杂度为n,DP复杂度为n,总复杂度O(n^3)。

代码:

 int a[],b[],c[],dp[],v[],dp2[],dep[],siz[],now,n;
void qq(int x,int fa)
{
dep[x]=dep[fa]+;
for(int i=c[x];i;i=b[i])qq(i,x);
}
bool gcd(int x,int y)
{
if(dep[x]>dep[y])swap(x,y);
while(dep[x]<dep[y])y=a[y];
return x!=y;
}
void ss(int x)
{
dp[x]=; dp2[x]=; siz[x]=;
for(int i=c[x];i;i=b[i])
{
ss(i); dp[x]=dp[x]+dp[i]; dp2[x]=max(dp2[x],dp2[i]); siz[x]=siz[x]+siz[i];
}
if((v[x]==)and(siz[x]==))dp[x]++; if(now==x)dp2[x]=;
if((dp[x]>)and(siz[x]!=)and((v[x]==)or(now==x)))
{
if(dp2[x]==)dp[x]=max(dp[x]-,);else dp[x]=max(dp[x]-,);
}
if((v[x]==)or(x==now))siz[x]++;
}
class EllysTree
{
public:
vector <int> getMoves(vector <int> parent)
{
//$CARETPOSITION$
vector <int> ans; n=parent.size();
for(int i=;i<n;i++)a[i+]=parent[i];
for(int i=;i<=n;i++){ b[i]=c[a[i]]; c[a[i]]=i; }
qq(,);
now=; v[]=; ss(); if(dp[]>)return ans;
for(int ii=;ii<=n;ii++)
{
for(int i=;i<=n;i++)
if(v[i]==)
{
if(gcd(now,i))continue;
int tnow=now; now=i; v[i]=; ss(); if(dp[]==){ ans.push_back(i); break; }
now=tnow; v[i]=;
}
}
return ans;
}
};

TopCoder[TCO2016 Round 1A]:EllysTree(1000)的更多相关文章

  1. [Google Codejam] Round 1A 2016 - The Last Word

    [Problem Description] Problem On the game show The Last Word, the host begins a round by showing the ...

  2. [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product

    Problem A. Minimum Scalar Product   This contest is open for practice. You can try every problem as ...

  3. Google Code Jam Round 1A 2015 Problem B. Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  4. 2008 Round 1A C Numbers (矩阵快速幂)

    题目描述: 请输出(3+√5)^n整数部分最后3位.如果结果不超过2位,请补足前导0. 分析: 我们最容易想到的方法肯定是直接计算这个表达式的值,但是这样的精度是不够的.朴素的算法没有办法得到答案.但 ...

  5. Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)

    Problem C. Numbers This contest is open for practice. You can try every problem as many times as you ...

  6. TopCoder[SRM513 DIV 1]:Reflections(1000)

    Problem Statement      Manao is playing a new game called Reflections. The goal of the game is trans ...

  7. Round 1A 2020 - Code Jam 2020

    Problem A. Pattern Matching 把每个字符串分成第一个之前,最后一个之后,中间的部分 三个部分 每个字符串的中间的部分可以直接拼接 前后两个部分需要判断下是否合法 #inclu ...

  8. Google Code Jam Round 1A 2015 解题报告

    题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...

  9. TopCoder SRM 642 Div.2 1000 --二分+BFS

    题意: 给你一张图,N个点(0~N-1),m条边,国王要从0到N-1,国王携带一个值,当走到一条边权大于此值的边时,要么不走,要么提升该边的边权,提升k个单位花费k^2块钱,国王就带了B块钱,问能携带 ...

随机推荐

  1. Ubuntu更换阿里云数据源

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo vim /etc/apt/sources.list 将里面的内容全部删除修改成 ...

  2. 多线程用this指针来传递参数(整理)

    整理自CSDN的论坛中,地址:https://bbs.csdn.net/topics/390703249 1.不同的线程不是两个独立的程序:线程不是进程(process是你说的程序) 2.线程函数必须 ...

  3. Android开发笔记之ArrayAdapter

    1,ArrayAdapter的item中的条目的布局文件的正确写法: item.xml <?xml version="1.0" encoding="utf-8&qu ...

  4. Vivado利用IP自带的示例工程和仿真

    有时候想查看IP的特性和功能,又不想自己写testbench,Vivado自带的IP示例工程就能派上用场,原来一直不知道怎么打开IP的示例工程 第一步:在原有的工程中新建IP,按照你想要的IP属性,例 ...

  5. 【原理】RabbitMQ架构图

    Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线,保证数据能按照指定的方式进行传输, Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列. Queue:消息 ...

  6. cf期望概率专题

    cf1009E:求到第i段期望和的比较困难,但是单独求每段的期望是比较容易的,所以单独对每段求和,然后累计总和 E[i]=1/2*a1+1/4*a2+...+1/2^(i-1)*ai-1+1/2^(i ...

  7. mysql8.*忘记密码

    1.关闭mysql服务 2.打开cmd窗口,找到安装目录下的bin然后复制命令 mysqld --console --skip-grant-tables --shared-memory 3.再打开一个 ...

  8. AcWing 138. 兔子与兔子 hash打卡

    很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然后我们每 ...

  9. 关于BUG管理工具的操作总结。(禅道)

    禅道是第一款国产的优秀开源项目管理软件.先进的管理思想,合理的软件架构,简洁实效的操作,优雅的代码实现,灵活的扩展机制,强大而易用的api 调用机制,多语言支持,多风格支持,搜索功能,统计功能——这一 ...

  10. java 轻量级 RestClient

    package org.rx.socks.http; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; ...