There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges are missing so we don't know how the nodes are connected. Instead we know:

1. Which nodes are leaves

2. The distance (number of edges) between any pair of leaves

3. The depth of every node (the root's depth is 1)

4. For the nodes on the same level, their order from left to right

Can you restore the tree's edges with these information? Note if node u is on the left of node vu's parent should not be on the right of v's parent.

题意:有一颗树,给出叶子节点有哪些,叶子节点之间互相的距离,每个节点的深度,并且每层给定左右顺序,树边不交叉,要求复原树。

其实就是一个模拟题,我们可以知道,如果同一层两个叶子节点的距离是2,那么他们肯定是兄弟节点,而由于左右顺序给定,叶节点也给定,那么某一层最左边的两个叶子节点的父节点一定是上一层最左边的一个非叶子节点,而父节点和除子树外各节点的距离,其实是子节点和它们距离 - 1。我就能通过子节点得到父节点的距离信息了。

这个思路就可以做模拟了,我们对于最后一层从最左边的叶子节点开始,找到所有它的兄弟,它的距离顺便更新它的父节点的距离,之后父节点就可以当做叶节点了,然后继续操作同层所有的叶节点,寻找到父亲并更新父亲的距离。这样一层一层向上知道第二层就行。

模拟裸题。

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std; const int maxn=;
const int INF=0x3f3f3f3f;
const int mod=1e9+; int n,m,k; int vis[maxn];
int fa[maxn];
int mcnt[maxn];
int M[maxn][maxn];
int Map[maxn][maxn];
int a[maxn]; void update( int l ){
int f = fa[l];
for(int i = ; i <= n ; ++ i ){
if( Map[l][i] != -){
Map[f][i] = Map[i][f] = Map[l][i] - ;
}
}
} int main(){
memset(fa,-,sizeof(fa));
scanf("%d%d%d",&n,&m,&k);
for(int i = ; i <= m ; ++ i){
scanf("%d",&mcnt[i]);
}
for(int i = ; i <= m ; ++ i){
for(int j = ; j <= mcnt[i] ; ++ j){
scanf("%d",&M[i][j]);
}
}
memset(vis,,sizeof(vis));
for(int i = ; i <= k ; ++ i){
scanf("%d",&a[i]);
vis[a[i]] = ;
}
memset(Map,-,sizeof(Map));
for(int i = ; i <= k ; ++ i ){
for(int j = ; j <= k ; ++ j ){
scanf("%d",&Map[a[i]][a[j]]);
}
} for(int l = m ; l > ; -- l ){
int S = l,F = l - ;
int pos1 = ,pos2 = ;
while( pos2 <= mcnt[F] && vis[ M[F][pos2] ] )pos2++;
fa[ M[S][pos1] ] = M[F][pos2];
while( pos1 <= mcnt[S] ){
int l1 = M[S][pos1];
if(pos1 == mcnt[S] ){
update( l1 );
break;
}
else{
int l2 = M[S][pos1+];
if( Map[l1][l2] != ){
update( l1 );
pos2++;
while( pos2 <= mcnt[F] && vis[ M[F][pos2] ] )pos2++;
}
fa[l2] = M[F][pos2];
pos1++;
}
}
}
for(int i = ; i <= n ; ++ i ){
if(fa[i] == - )printf("");
else printf("%d",fa[i]);
if(i==n)printf("\n");
else printf(" ");
}
return ;
}

hihocoder1490 Tree Restoration 模拟的更多相关文章

  1. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  2. Tree Restoration Gym - 101755F (并查集)

    There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...

  3. #1490 : Tree Restoration

    微软 2017春招真题 题目 There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges a ...

  4. POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28399   Accepted: 9684 De ...

  5. 【微软2017年预科生计划在线编程笔试 B】Tree Restoration

    [题目链接]:https://hihocoder.com/problemset/problem/1490 [题意] 给你一棵树的以下信息: 1.节点个数 2.给出树的每一层从左到右的顺序每个节点的编号 ...

  6. HDU 4925 Apple Tree(模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种 ...

  7. hihocoder 1490 Tree Restoration

    构造. 从最后一层开始往上构造.最后一层肯定都是叶子结点,距离为2的肯定是同一个父亲,确定好了父亲之后,可以确定上一层每个节点之间的距离,以及上一层每个节点到还未确定的叶子节点之间的距离. #incl ...

  8. Codeforces gym101755F Tree Restoration(拓扑排序)

    题意: 一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树 n<=1000 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...

  9. 【EasyUI学习-2】Easyui Tree的异步加载

    作者:ssslinppp       1. 摘要 2. tree的相关介绍 3. 异步加载tree数据,并实现tree的折叠展开 3.1 功能说明: 3.2 前台代码 3.3 后台代码 4. 其他 1 ...

随机推荐

  1. 软件设计基础-C/S系统

    在软件设计开发过程中,逐渐形成了一些针对特定应用领域的软件系统组织方式的惯用模式 如经典的C/S(client/server,客户/服务器)模式和B/S(browser/server,浏览器/服务器) ...

  2. php随手记

    引用(&)是变量的别名,而不是指针,可用unset(变量名)把此变量的别名注销掉,等于没有声明此变量. @为错误抑制符,可以用在任何表达式前面. ``为命令操作符,可以执行系统命令. inst ...

  3. pytorch加载和保存模型

    在模型完成训练后,我们需要将训练好的模型保存为一个文件供测试使用,或者因为一些原因我们需要继续之前的状态训练之前保存的模型,那么如何在PyTorch中保存和恢复模型呢? 方法一(推荐): 第一种方法也 ...

  4. CentOS7安装配置Bacular

    参考: http://blog.51cto.com/molewan/2045602 https://blog.csdn.net/heshangkung/article/details/47955023 ...

  5. DevExpress WinForms v18.2新版亮点(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...

  6. Android开发 --代码布局

    Android开发 --代码布局 在线性布局LinearLayout里加入view比较简单,因为属性比较少,布局简单 示例,加入一个TextView LinearLayout layout = (Li ...

  7. 四川省赛 SCU - 4438

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

  8. HTML5中input[type='date']自定义样式

    HTML5提供了日历控件功能,缩减了开发时间,但有时它的样式确实不如人意,我们可以根据下面的代码自行修改. 建议:复制下面的代码段,单独建立一个css文件,方便我们修改. /* 修改日历控件类型 */ ...

  9. 20165326 java第九周学习笔记

    第九周学习笔记 URL类 属于java.net包 最基本三部分:协议(对象所在的Java虚拟机支持).地址(能连接的有效IP地址或域名).资源(主机上的任何一个文件) 常用构造方法 public UR ...

  10. day 55 前端

    前端JQuery 语法 1 关于表格基数偶数背景颜色变换的 2 关于has后代   和not非 3 jQuery 和dom的转换  dom 转换成jQuery 用$(包起来) 不加引号 4  关于 n ...