zoukankan      html  css  js  c++  java
  • ZOJ --- 3516 Tree of Three

    Tree of Three

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Now we have a tree and some queries to deal with. Every node in the tree has a value on it. For one node A, we want to know the largest three values in all the nodes of the subtree whose root is node A. Node 0 is root of the tree, except it, all other nodes have a parent node.

    Input

    There are several test cases. Each test case begins with a line contains one integer n(1 ≤ n ≤ 10000), which indicates the number of the node in the tree. The second line contains one integer v[0], the value on the root. Then for the following n - 1 lines(from the 3rd line to the (n + 1)th line), let i + 2 be the line number, then line i + 2contains two integers parent and v[i], here parent is node i's parent node, v[i] is the value on node i. Here 0 ≤ v[i] ≤ 1000000. Then the next line contains an integer m(1 ≤m ≤ 10000), which indicates the number of queries. Following m lines, each line contains one integer q, 0 ≤ q < n, it meas a query on node q.

    Output

    For each test case, output m lines, each line contains one or three integers. If the query asked for a node that has less than three nodes in the subtree, output a "-1"; otherwise, output the largest three values in the subtree, from larger to smaller.

    Sample Input

    5
    1
    0 10
    0 5
    2 7
    2 8
    5
    0
    1
    2
    3
    4
    

    Sample Output

    10 8 7
    -1
    8 7 5
    -1
    -1
    

     思路:深度优先搜索,一层一层由子节点向跟节点回溯。

    1.

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #define MAX 11111
     6 using namespace std;
     7 int Three_Max[MAX][15], val[MAX], cnt[MAX];
     8 typedef struct{
     9     int to, next;
    10 }Node;
    11 Node edge[MAX];
    12 int head[MAX];
    13 void AddEdge(int u, int v, int i){
    14     edge[i].to = v;
    15     edge[i].next = head[u];
    16     head[u] = i;
    17 }
    18 bool cmp(int a, int b){
    19     return a > b;
    20 }
    21 void dfs(int id){
    22     cnt[id] = 1;
    23     Three_Max[id][1] = val[id];
    24     for(int i = head[id];i != -1;i = edge[i].next){
    25         int u = edge[i].to;
    26         dfs(u);
    27         for(int j = 4;j <= 6;j ++) Three_Max[id][j] = Three_Max[u][j-3];
    28         sort(Three_Max[id] + 1, Three_Max[id]+10, cmp);
    29         cnt[id] += cnt[u];
    30     }
    31 }
    32 int main(){
    33     int n, m, cc, u, v, k;
    34     while(~scanf("%d", &n)){
    35         memset(head, -1, sizeof(head));
    36         memset(Three_Max, 0, sizeof(Three_Max));
    37         memset(cnt, 0, sizeof(cnt));
    38         k = 1;
    39         for(int i = 0;i < n;i ++){
    40             if(0 == i){
    41                 scanf("%d", &cc);
    42                 val[0] = cc;
    43             }else{
    44                 scanf("%d%d", &u, &cc);
    45                 val[i] = cc;
    46                 AddEdge(u, i, k);
    47                 k ++;
    48             }
    49         }
    50         dfs(0);
    51         scanf("%d", &m);
    52         for(int i = 0;i < m;i ++){
    53             scanf("%d", &u);
    54             if(cnt[u] < 3) printf("-1
    ");
    55             else{
    56                 for(int j = 1;j < 3;j ++) printf("%d ", Three_Max[u][j]);
    57                 printf("%d
    ", Three_Max[u][3]);
    58             }
    59         }
    60     }
    61     return 0;
    62 }

     2.

     1 #include<iostream>
     2 #include<climits>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #define MAX 11111
     7 using namespace std;
     8 int Three_Max[MAX][15], val[MAX], cnt[MAX];
     9 typedef struct{
    10     int to, next;
    11 }Node;
    12 Node edge[MAX];
    13 int head[MAX];
    14 void AddEdge(int u, int v, int i){
    15     edge[i].to = v;
    16     edge[i].next = head[u];
    17     head[u] = i;
    18 }
    19 bool cmp(int a, int b){
    20     return a > b;
    21 }
    22 int *dfs(int id){
    23     cnt[id] = 1;
    24     Three_Max[id][1] = val[id];
    25     for(int i = head[id];i != -1;i = edge[i].next){
    26         int u = edge[i].to;
    27         int *temp = dfs(u);
    28         for(int j = 4;j <= 6;j ++) Three_Max[id][j] = temp[j-3];
    29         sort(Three_Max[id] + 1, Three_Max[id]+10, cmp);
    30         cnt[id] += cnt[u];
    31     }
    32     return Three_Max[id];
    33 }
    34 int main(){
    35     int n, m, cc, u, v, k;
    37     while(~scanf("%d", &n)){
    38         memset(head, -1, sizeof(head));
    39         memset(Three_Max, 0, sizeof(Three_Max));
    40         memset(cnt, 0, sizeof(cnt));
    41         k = 1;
    42         for(int i = 0;i < n;i ++){
    43             if(0 == i){
    44                 scanf("%d", &cc);
    45                 val[0] = cc;
    46             }else{
    47                 scanf("%d%d", &u, &cc);
    48                 val[i] = cc;
    49                 AddEdge(u, i, k);
    50                 k ++;
    51             }
    52         }
    53         dfs(0);
    54         scanf("%d", &m);
    55         for(int i = 0;i < m;i ++){
    56             scanf("%d", &u);
    57             if(cnt[u] < 3) printf("-1
    ");
    58             else{
    59                 for(int j = 1;j < 3;j ++) printf("%d ", Three_Max[u][j]);
    60                 printf("%d
    ", Three_Max[u][3]);
    61             }
    62         }
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    系统集成项目管理工程师高频考点(第一章)
    2、无重复字符的最长子串
    1、爬楼梯
    webpack起步
    Centos7安装nginx
    Centos7安装nacos
    Centos7安装java和maven
    centos7安装fastDFS
    aop中获取请求消息和属性
    数据库面对高并发的思路
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3639159.html
Copyright © 2011-2022 走看看