微软 2017春招真题

题目

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 v, u's parent should not be on the right of v's parent.



输入

The first line contains three integers N, M and K. N is the number of nodes. M is the depth of the tree. K is the number of leaves.

The second line contains M integers A1, A2, ... AM. Ai represents the number of nodes of depth i.

Then M lines follow. The ith of the M lines contains Ai numbers which are the nodes of depth i from left to right.  

The (M+3)-th line contains K numbers L1, L2, ... LK, indicating the leaves.

Then a K × K matrix D follows. Dij represents the distance between Li and Lj.1 ≤ N ≤ 100

输出

For every node from 1 to N

 output its parent. Output 0 for the root's parent.

样例输入

8 3 5

1 3 4

1

2 3 4

5 6 7 8

3 5 6 7 8

0 3 3 3 3

3 0 2 4 4

3 2 0 4 4

3 4 4 0 2

3 4 4 2 0

样例输出

0 1 1 1 2 2 4 4

分析

首先,给出的测试用例太过简单,先构建一个复杂的测试用例:



从下往上,从左往右遍历:

第六层(最下层):

第一个节点26,其父节点一定是第五层第一个非叶子节点20。同时,在距离矩阵中增加节点20,与其他节点距离为节点26与其他节点距离-1

第二个节点27,若和26的距离为2,则其父节点也是20(×),否则,其父节点是第五层第二个非叶子节点22(√)同时,在距离矩阵中增加节点22,与其他节点距离为节点27与其他节点距离-1

第三个节点28,若和27的距离为2,则其父节点也是22(√),否则,其父节点是第五层第三个非叶子节点24(×)

......

第六层遍历结束,明确了第六层所有节点的父节点,也将第五层所有非叶子节点加入距离矩阵



第五层(倒数第二层):

第一个节点19,其父节点一定是第四层第一个非叶子节点13。同时,在距离矩阵中增加节点13,与其他节点距离为节点19与其他节点距离-1

第二个节点20,若和19的距离为2,则其父节点也是13(√),否则,其父节点是第四层第二个非叶子节点14(×)

......

第五层遍历结束,明确了第五层所有节点的父节点,也将第五层所有非叶子节点加入距离矩阵

......



(一直遍历到第三层)

第二层:

所有节点的父节点都是根节点(第一层的唯一节点)

第一层:

根节点的父节点为0

解答

解法1:结果是Wrong Answer,然而并不知道为什么,谁能帮我看看错在哪了哇~

    import java.util.Scanner;
import java.util.Map;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.ConcurrentHashMap; public class Main_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//节点数8
int m = sc.nextInt();//树深度3
int k = sc.nextInt();//叶子树5
int[] a = new int[m];//深度为i的节点数
int[][] arr = new int[m][];//每行的节点data
Node[][] nodes = new Node[m][];//每行的节点
Map<Integer, Node> nodesMap = new LinkedHashMap<>();//存节点
Map<Node, Map<Node,Integer>> distanceMap = new ConcurrentHashMap<>();//存节点间距离(叶子+非叶子) for (int i = 0; i < m; i++) {
a[i] = sc.nextInt();
arr[i] = new int[a[i]];
nodes[i] = new Node[a[i]];
} for (int i = 0; i < m; i++) {
for (int j = 0; j < a[i]; j++){
arr[i][j] = sc.nextInt();
nodes[i][j] = new Node();
nodes[i][j].data = arr[i][j];
nodes[i][j].level = i + 1;
nodesMap.put(arr[i][j],nodes[i][j]); if(i == 0){
nodes[i][j].parent = null;
}
else if(i == 1){
nodes[i][j].parent = nodes[0][0];
}
}
}
int[] l = new int[n];//叶子 3 5 6 7 8
for(int i = 0; i < k; i++){
l[i] = sc.nextInt();
nodesMap.get(l[i]).isLeaf = true;
} int[][] d = new int[n][n];//叶子节点间距离 for (int i = 0; i < k; i++) {
Map<Node, Integer> tempMap = new ConcurrentHashMap<>();
for (int j = 0; j < k; j++){
d[i][j] = sc.nextInt();
if (l[i] < l[j]){
tempMap.put(nodesMap.get(l[j]),d[i][j]);
distanceMap.put(nodesMap.get(l[i]),tempMap);
}
}
}
//从下往上,从左往右遍历
for(int i = m - 1; i >= 2; i--){
int ii = i - 1;//上一层
int jj = 0;//上一层第0个节点
Node lastNode = null;
for(int j = 0; j < a[i]; j++){
if (lastNode != null){//不是本层第一个节点,计算该节点与本层上一个节点的距离
int distance = (distanceMap.get(lastNode) != null && distanceMap.get(lastNode).get(nodes[i][j]) != null) ? distanceMap.get(lastNode).get(nodes[i][j]) : distanceMap.get(nodes[i][j]).get(lastNode);
if (distance == 2){
nodes[i][j].parent = lastNode.parent;
continue;
}
} while (nodes[ii][jj].isLeaf){
jj++;
} nodes[i][j].parent = nodes[ii][jj];
//在距离矩阵中增加其父节点
Iterator it1 = distanceMap.entrySet().iterator(); while (it1.hasNext()) {
Map.Entry outerEntry = (Map.Entry) it1.next();
Node outerKey = (Node) outerEntry.getKey();
Map<Node,Integer> outerValue = (Map<Node,Integer>) outerEntry.getValue(); if(outerKey == nodes[i][j]){
Map<Node, Integer> tempMap = new ConcurrentHashMap<>();
Iterator it2 = outerValue.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry entry = (Map.Entry) it2.next();
Node innerKey = (Node) entry.getKey();
Integer innerValue = (Integer) entry.getValue();
tempMap.put(innerKey,innerValue - 1);
}
distanceMap.put(nodes[i][j].parent,tempMap);
} else{
Iterator it2 = outerValue.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry entry = (Map.Entry) it2.next();
Node innerKey = (Node) entry.getKey();
Integer innerValue = (Integer) entry.getValue(); if(innerKey == nodes[i][j]){
Map<Node, Integer> tempMap = distanceMap.get(outerKey);
tempMap.put(nodes[i][j].parent,innerValue - 1);
distanceMap.put(outerKey,tempMap);
}
}
} }
lastNode = nodes[i][j];
jj++;
}
}
for (Map.Entry<Integer, Node> entry : nodesMap.entrySet()) {
Node thisNode = entry.getValue();
int result = (thisNode.parent != null) ? thisNode.parent.data : 0;
System.out.print(result+" ");
}
}
}
class Node{
public Node parent;
public int data;
public int level;
public boolean isLeaf;
}

附两个测试用例:

9 4 6

1 2 3 4

9

1 10

2 3 4

5 6 7 8

3 5 6 7 8 10

0 3 3 3 3 3

3 0 2 4 4 4

3 2 0 4 4 4

3 4 4 0 2 4

3 4 4 2 0 4

3 4 4 4 4 0


31 6 13
1 4 6 7 7 6
1
2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
3 8 12 15 19 21 23 26 27 28 29 30 31
0 3 4 4 5 5 5 6 6 6 6 6 6
3 0 3 5 4 4 6 5 7 7 7 7 7
4 3 0 6 5 5 7 6 8 8 8 8 8
4 5 6 0 7 7 7 8 4 4 8 8 8
5 4 5 7 0 4 8 3 9 9 9 9 9
5 4 5 7 4 0 8 5 9 9 9 9 9
5 6 7 7 8 8 0 9 9 9 7 7 7
6 5 6 8 3 5 9 0 10 10 10 10 10
6 7 8 4 9 9 9 10 0 2 10 10 10
6 7 8 4 9 9 9 10 2 0 10 10 10
6 7 8 8 9 9 7 10 10 10 0 2 4
6 7 8 8 9 9 7 10 10 10 2 0 4
6 7 8 8 9 9 7 10 10 10 4 4 0

#1490 : Tree Restoration的更多相关文章

  1. hihocoder 1490 Tree Restoration

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

  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. hihocoder1490 Tree Restoration 模拟

    There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges are missing so w ...

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

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

  5. #1490 : Tree Restoration-(微软2017在线笔试)

    输入n m km个数,表示每层的节点个数接下来m行是每层的节点,节点顺序是从左往右的k个叶子节点k*k个矩阵,表示叶子节点之间的距离 输出:每个节点的父亲节点编号,root节点是0 题解:1.很明显, ...

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

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

  7. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  8. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

随机推荐

  1. adb devices报错解决

    1. 执行adb device报错如下 2. 报错原因及解决办法 报错时开启了Androidkiller,关闭即解决问题 可能原因:adb命令被占用冲突了

  2. go lang中局部变量的内存分配

    晚上在阅读go lang的资料时突然想到一个问题,go是如何分配变量的内存结构的呢?好在网上的一篇文章做了透彻的分析见[go语言局部变量分配在栈还是堆]. 其结论是go语言局部变量的分配是由编译器决定 ...

  3. R语言文本挖掘+词云显示(jiebaR包+wordcloud2包)

    利用2018年政府工作报告的例子向大家展示一下R语言如何进行文本挖掘的~用到的包有jiebaR和wordcloud2. 1.安装并加载jiebaR install.packages("jie ...

  4. SAP函数 CS_WHERE_USED_MAT 反查上层BOM

    遇到用户要根据下层物料反查最上层BOM物料是什么. 试了一下,通过函数 CS_WHERE_USED_MAT 来查询,但是只能往上查询一层,类似事务码CS15的效果.如果要找最上层物料,需要自己写迭代进 ...

  5. 【转载】COM 组件设计与应用(十三)——事件和通知(VC6.0)

    原文:http://vckbase.com/index.php/wv/1243.html 一.前言 我的 COM 组件运行时产生一个窗口,当用户双击该窗口的时候,我需要通知调用者: 我的 COM 组件 ...

  6. AGC 030 B - Tree Burning

    B - Tree Burning 链接 题意: 一个长度为L的环,有n个位置上有树,从0出发,每次选择一个方向(顺时针或者逆时针),一直走,直到走到一棵树的位置,烧掉这棵树,重复这个过程,直到没有树. ...

  7. linux 之 jq

    1.安装 mac 安装: brew install jq centos 安装: yum install jq ubuntu: 安装: apt-get install jq 2.使用 cat test. ...

  8. 【PaPaPa】集成B/S主流技术的MVC5项目 - 实干派:说做就做,我们已经起航,你还在观望吗

    我们是谁 我们是C#爱好者,互相分享技术,一起学习一起成长一起做一个项目. 我们是开源爱好者,从我们手上出来的代码都会托管在源代码管理平台(oschina),到目前为止不收费,将来也不会出现任何收费情 ...

  9. vs2019编译redis

    版本信息 使用Redis源码版本,解压工程右键生成hiredis项目正常,编译Win32_Interop项目报下图错误(error C2039:system_error:不是std成员;error C ...

  10. OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor

    在python用cv2.imread()读取图片的时候一直报错,检查了图片的绝对路径是存在的. 报错的图片用skimage.io.imread()是可以读的. 查了一下,有可能是原图片文件有什么嵌入错 ...