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. 热修复框架Tinker的从0到集成之路(转)

    转自:http://blog.csdn.net/lisdye2/article/details/54411727 热修复框架Tinker的从0到集成之路 转载请标明出处: http://blog.cs ...

  2. element 的时间快捷键

    1. <div> <el-date-picker v-model="value4" type="month" :picker-options= ...

  3. 非关系型数据库MongoDB入门

    本文分为以下四块简单介绍非关系型数据库MongoDB:1.MongoDB简介.2.MongoDB和关系数据库对比.3.MongoDB基本概念.4.mongo shell的使用以及对MongoDB的增删 ...

  4. Jedis连接池的使用(转)

    http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135248.html 所需jar:jedis-2.1.0.jar和commons-pool-1 ...

  5. 在linux设置/etc/vimrc 将vim 中后缀.sh的文件 的前几行进行默认输入

    输入vim test.sh 新建后缀sh的文件,效果如下: 具体/etc/vimrc配置为: if expand("%:e") == 'sh'  call setline(1,&q ...

  6. Docker 初识之路

    一. 安装配置 1.安装依赖 sudo yum install -y yum-utils device-mapper-persistent-data lvm2   2.设置阿里云镜像源 sudo yu ...

  7. AcWing 203. 同余方程 (线性同余方程)打卡

    求关于x的同余方程 ax ≡ 1(mod b) 的最小正整数解. 输入格式输入只有一行,包含两个正整数a,b,用一个空格隔开. 输出格式输出只有一行,包含一个正整数x,表示最小正整数解. 输入数据保证 ...

  8. [20190725NOIP模拟测试8]题解

    Orz T1 大水题,考场上看到题目中什么前几位相同末尾加字母莫名慌的一批 后来发现直接无脑哈希就能$O(n)$ KMP同样可切 仔细读题,数组别开小 #include<cstdio> # ...

  9. 十折交叉验证10-fold cross validation, 数据集划分 训练集 验证集 测试集

    机器学习 数据挖掘 数据集划分 训练集 验证集 测试集 Q:如何将数据集划分为测试数据集和训练数据集? A:three ways: 1.像sklearn一样,提供一个将数据集切分成训练集和测试集的函数 ...

  10. Linux 线程Demo

    #include <stdio.h> #include <pthread.h> struct char_print_params { char character; int c ...