hihocoder1490 Tree Restoration 模拟
Posted 奚政
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hihocoder1490 Tree Restoration 模拟相关的知识,希望对你有一定的参考价值。
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.
题意:有一颗树,给出叶子节点有哪些,叶子节点之间互相的距离,每个节点的深度,并且每层给定左右顺序,树边不交叉,要求复原树。
其实就是一个模拟题,我们可以知道,如果同一层两个叶子节点的距离是2,那么他们肯定是兄弟节点,而由于左右顺序给定,叶节点也给定,那么某一层最左边的两个叶子节点的父节点一定是上一层最左边的一个非叶子节点,而父节点和除子树外各节点的距离,其实是子节点和它们距离 - 1。我就能通过子节点得到父节点的距离信息了。
这个思路就可以做模拟了,我们对于最后一层从最左边的叶子节点开始,找到所有它的兄弟,它的距离顺便更新它的父节点的距离,之后父节点就可以当做叶节点了,然后继续操作同层所有的叶节点,寻找到父亲并更新父亲的距离。这样一层一层向上知道第二层就行。
模拟裸题。
1 #include<stdio.h>
2 #include<string.h>
3 #include<math.h>
4 #include<algorithm>
5 using namespace std;
6
7 const int maxn=105;
8 const int INF=0x3f3f3f3f;
9 const int mod=1e9+7;
10
11 int n,m,k;
12
13 int vis[maxn];
14 int fa[maxn];
15 int mcnt[maxn];
16 int M[maxn][maxn];
17 int Map[maxn][maxn];
18 int a[maxn];
19
20 void update( int l ){
21 int f = fa[l];
22 for(int i = 1 ; i <= n ; ++ i ){
23 if( Map[l][i] != -1){
24 Map[f][i] = Map[i][f] = Map[l][i] - 1;
25 }
26 }
27 }
28
29 int main(){
30 memset(fa,-1,sizeof(fa));
31 scanf("%d%d%d",&n,&m,&k);
32 for(int i = 1 ; i <= m ; ++ i){
33 scanf("%d",&mcnt[i]);
34 }
35 for(int i = 1 ; i <= m ; ++ i){
36 for(int j = 1 ; j <= mcnt[i] ; ++ j){
37 scanf("%d",&M[i][j]);
38 }
39 }
40 memset(vis,0,sizeof(vis));
41 for(int i = 1 ; i <= k ; ++ i){
42 scanf("%d",&a[i]);
43 vis[a[i]] = 1;
44 }
45 memset(Map,-1,sizeof(Map));
46 for(int i = 1 ; i <= k ; ++ i ){
47 for(int j = 1 ; j <= k ; ++ j ){
48 scanf("%d",&Map[a[i]][a[j]]);
49 }
50 }
51
52 for(int l = m ; l > 1 ; -- l ){
53 int S = l,F = l - 1;
54 int pos1 = 1,pos2 = 1;
55 while( pos2 <= mcnt[F] && vis[ M[F][pos2] ] )pos2++;
56 fa[ M[S][pos1] ] = M[F][pos2];
57 while( pos1 <= mcnt[S] ){
58 int l1 = M[S][pos1];
59 if(pos1 == mcnt[S] ){
60 update( l1 );
61 break;
62 }
63 else{
64 int l2 = M[S][pos1+1];
65 if( Map[l1][l2] != 2 ){
66 update( l1 );
67 pos2++;
68 while( pos2 <= mcnt[F] && vis[ M[F][pos2] ] )pos2++;
69 }
70 fa[l2] = M[F][pos2];
71 pos1++;
72 }
73 }
74 }
75 for(int i = 1 ; i <= n ; ++ i ){
76 if(fa[i] == -1 )printf("0");
77 else printf("%d",fa[i]);
78 if(i==n)printf("\n");
79 else printf(" ");
80 }
81 return 0;
82 }
以上是关于hihocoder1490 Tree Restoration 模拟的主要内容,如果未能解决你的问题,请参考以下文章
hihoCoder #1454 : Rikka with Tree II
[hihoCoder#1381]Little Y's Tree