• ZOJ3516 (图的遍历)


    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 + 2 contains 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

    建图方法及遍历:链式齐向前星。
    http://blog.csdn.net/acdreamers/article/details/16902023(链式向前星)
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 using namespace std;
     7 int num,k,pa;
     8 #define maxn 32010
     9 int val[maxn];
    10 int head[maxn];
    11 int cnt[maxn];
    12 struct Edge
    13 {
    14     int u;
    15     int v;
    16     int next;
    17 };
    18 Edge edge[maxn];
    19 void addedge(int u,int v)//链式前向星模板
    20 {
    21     edge[num].u=u;
    22     edge[num].v=v;
    23     edge[num].next=head[u];
    24     head[u]=num++;
    25 }
    26 void dfs(int p)
    27 {
    28   for(int i=head[p];i!=-1;i=edge[i].next)
    29   {
    30       int v=edge[i].v;
    31       cnt[k++]=val[v];
    32       dfs(v);
    33   }
    34 }
    35 int main()
    36 {
    37     int n,root;
    38     while(~scanf("%d",&n))
    39     {
    40         memset(head,-1,sizeof(head));
    41         num=0;
    42         scanf("%d",&val[0]);
    43         for(int i=1;i<n;i++)
    44         {
    45             scanf("%d%d",&pa,&val[i]);
    46             addedge(pa,i);
    47         }
    48         int m,pos;
    49         scanf("%d",&m);
    50         for(int i=0;i<m;i++)
    51         {
    52             k=0;
    53             memset(cnt,0,sizeof(cnt));
    54             scanf("%d",&pos);
    55             cnt[k++]=val[pos];
    56             dfs(pos);
    57             if(k<3)
    58             printf("-1
    ");
    59             else
    60             {
    61                 sort(cnt,cnt+k);
    62                 reverse(cnt,cnt+k);
    63                 printf("%d %d %d
    ",cnt[0],cnt[1],cnt[2]);
    64             }
    65         }
    66     }
    67 
    68     return 0;
    69 }
  • 相关阅读:
    Rust v1.39发布
    Theory of Storage
    What is Market Intelligence and how is it Used?
    敏感性分析与风险分析
    四大交易所重磅齐发布!涉及股指期权上市、市场体系创新、优化期权做市商机制等等
    库存+基差+跨期套利的交易策略
    大宗商品贸易模式及风险识别——基于贸易融资方式、真实贸易资金需求等视角
    CME Futures & Options Order Book
    什么是期货跟单?什么是期货跟单系统?
    ScrollReveal-元素随页面滚动产生动画的js插件
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4685079.html
Copyright © 2020-2023  润新知