树(2):一般树的遍历

一般树的遍历

1090 Highest Price in Supply Chain (25 分)
 

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (≤), the total number of the members in the supply chain (and hence they are numbered from 0 to N1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si​​ is the index of the supplier for the i-th member. Sroot​​ for the root supplier is defined to be −. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1.

Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:

1.85 2

#include <cstdio>
#include <vector>
using namespace std;
const int MAXN =100010;
int n,sum=0;
double p,r,maxp=0;
struct node
{
    double price;
    vector<int> child;
}nodes[MAXN];
void conter(int root,double pr)
{
    nodes[root].price=pr;
    if(nodes[root].child.size()==0)
    {
        if(pr>maxp) maxp=pr;
    }else
    {
        for(int i=0;i<nodes[root].child.size();i++) conter(nodes[root].child[i],pr*(1+r/100));    
    }
}
int main()
{
    scanf("%d %lf %lf",&n,&p,&r);
    int root,m;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&m);
        if(m==-1) root=i;
        else nodes[m].child.push_back(i);
    }
    
    conter(root,p);
    for(int i=0;i<n;i++)
    {
        if(nodes[i].price==maxp) sum++;
    }
    printf("%.2f %d",maxp,sum); 
    return 0;
}

采用静态写法,按套路来即可。然后按照具体要求来遍历,一般就能搞定。

1106 Lowest Price in Supply Chain (25 分)
 

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the lowest price a customer can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (≤), the total number of the members in the supply chain (and hence their ID's are numbered from 0 to N1, and the root supplier's ID is 0); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

Ki​​ ID[1] ID[2] ... ID[Ki​​]

where in the i-th line, Ki​​ is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. Kj​​ being 0 means that the j-th member is a retailer. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the lowest price we can expect from some retailers, accurate up to 4 decimal places, and the number of retailers that sell at the lowest price. There must be one space between the two numbers. It is guaranteed that the all the prices will not exceed 1.

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0
2 6 1
1 8
0
0
0

Sample Output:

1.8362 2
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN =100010;
int n,sum=0;
double p,r,minp=10000000000;
struct node
{
    double price;
    vector<int> child;
}nodes[MAXN];
void conter(int root,double pr)
{
    nodes[root].price=pr;
    if(nodes[root].child.size()==0)
    {
        if(pr<minp) minp=pr;
    }else
    {
        for(int i=0;i<nodes[root].child.size();i++) conter(nodes[root].child[i],pr*(1+r/100));    
    }
}
int main()
{
    scanf("%d %lf %lf",&n,&p,&r);
    int m,child;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&m);
        if(m!=0)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&child);
                nodes[i].child.push_back(child);
            }
        }
        
    }
    conter(0,p);
    for(int i=0;i<n;i++)
    {
        if(nodes[i].price==minp&&nodes[i].child.size()==0) sum++;
    }
    printf("%.4f %d",minp,sum); 
    return 0;
}

套路一样,不过还是卡在了细节,在计算最低价的数量时,不是叶节点的应排除。

1079 Total Sales of Supply Chain (25 分)
 

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the total sales from all the retailers.

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (≤), the total number of the members in the supply chain (and hence their ID's are numbered from 0 to N1, and the root supplier's ID is 0); P, the unit price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

Ki​​ ID[1] ID[2] ... ID[Ki​​]

where in the i-th line, Ki​​ is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. Kj​​ being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after Kj​​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 1.

Sample Input:

10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3

Sample Output:

42.4


#include <cstdio>
#include <vector>
using namespace std;
const int MAXN =100010;
int n;
double p,r,sum=0;
struct node
{
    double price;
    int num;
    vector<int> child;
}nodes[MAXN];
void conter(int root,double pr)
{
    nodes[root].price=pr;
    if(nodes[root].child.size()==0)
    {
        sum+=nodes[root].num*nodes[root].price;
    }else
    {
        for(int i=0;i<nodes[root].child.size();i++)
        {
            conter(nodes[root].child[i],pr*(1+r/100));
        }
    }
}
int main()
{
    scanf("%d %lf %lf",&n,&p,&r);
    int m;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&m);
        if(m==0) scanf("%d",&nodes[i].num);
        else
        {
            int temp;
            while(m--)
            {
                scanf("%d",&temp);
                nodes[i].child.push_back(temp);
            }
        }    
    }
    conter(0,p);
    printf("%.1f",sum); 
    return 0;
}

是上一题的改进,大致思路差不多,建树,然后遍历的时候求出需要的数据。

1094 The Largest Generation (25 分)
 

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (<) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a family member, K (>) is the number of his/her children, followed by a sequence of two-digit ID's of his/her children. For the sake of simplicity, let us fix the root ID to be 01. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.

Sample Input:

23 13
21 1 23
01 4 03 02 04 05
03 3 06 07 08
06 2 12 13
13 1 21
08 2 15 16
02 2 09 10
11 2 19 20
17 1 22
05 1 11
07 1 14
09 1 17
10 1 18

Sample Output:

9 4


#include <cstdio>
#include <vector>
using namespace std;
const int MAXN =110;

vector<int> nodes[MAXN];
int gennum[MAXN]={0};
int maxgen=0,ans,maxpop=0;
void counter(int root,int gen)
{
    gennum[gen]++;
    if(gen>maxgen) maxgen=gen;
    for(int i=0;i<nodes[root].size();i++)
    {
        counter(nodes[root][i],gen+1);
    }
}
int main()
{
    int n,m,id,s,temp;
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&id,&s);
        for(int j=0;j<s;j++)
        {
            scanf("%d",&temp);
            nodes[id].push_back(temp);
        }
    }
    counter(1,1);
    for(int i=1;i<=maxgen;i++)
    {
        if(gennum[i]>maxpop) 
        {
            maxpop=gennum[i];
            ans=i;
        }
    }
    printf("%d %d",maxpop,ans);
    
    return 0;
}

同样的套路,不过不小心打错了一个变量,怎么都没弄对,费了好久才发现。。哎,这本该是初学者才易犯的错误。

1004 Counting Leaves (30 分)
 

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0, the number of nodes in a tree, and M (<), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

For each test case, you are supposed to count those family members who have no child for every seniority levelstarting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02

Sample Output:

0 1


#include <cstdio>
#include <vector>

using namespace std;

const int MAXN=110;
vector<int> nodes[MAXN];
int depnum[MAXN]={0};
int n,m,id,k,temp,maxdep=0;
void counter(int root,int dep)
{
    if(dep>maxdep) maxdep=dep;
    if(nodes[root].size()==0) depnum[dep]++;
    else
    {
        for(int i=0;i<nodes[root].size();i++) counter(nodes[root][i],dep+1);
    }
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&id,&k);
        for(int j=0;j<k;j++)
        {
            scanf("%d",&temp);
            nodes[id].push_back(temp);
        }
    }
    counter(1,0);
    for(int i=0;i<=maxdep;i++)
    {
        printf("%d",depnum[i]);
        if(i<maxdep) printf(" ");
    }
    return 0;
}
1053 Path of Equal Weight (30 分)
 

Given a non-empty tree with root R, and with weight Wi​​ assigned to each tree node Ti​​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0, the number of nodes in a tree, M (<), the number of non-leaf nodes, and 0, the given weight number. The next line contains N positive numbers where Wi​​ (<) corresponds to the tree node Ti​​. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 00.

Output Specification:

For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence { is said to be greater than sequence { if there exists 1 such that Ai​​=Bi​​ for ,, and Ak+1​​>Bk+1​​.

Sample Input:

20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19

Sample Output:

10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN=110;
int n,m,s;
struct node
{
    int weight;
    vector<int> child;    
}nodes[MAXN]; 
bool cmp(int a,int b)
{
    return nodes[a].weight>nodes[b].weight;
}
int path[MAXN];

void counter(int root,int weight,int pl)
{
    weight+=nodes[root].weight;
    path[pl]=nodes[root].weight;
    if(nodes[root].child.size()==0&&weight==s)
    {
        for(int i=0;i<=pl;i++)
        {
            printf("%d",path[i]);
            if(i<pl) printf(" ");
        }
        printf("\n");
    }
    else
    {
        for(int i=0;i<nodes[root].child.size();i++) counter(nodes[root].child[i],weight,pl+1);
    }
}

int main()
{
    scanf("%d %d %d",&n,&m,&s);
    for(int i=0;i<n;i++) scanf("%d",&nodes[i].weight);
    int id,k,temp;
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&id,&k);
        for(int j=0;j<k;j++)
        {
            scanf("%d",&temp); 
            nodes[id].child.push_back(temp); 
        }
        sort(nodes[id].child.begin(),nodes[id].child.end(),cmp);
    }
    counter(0,0,0);
    
    return 0;
}

额,大概还是一样的思路,细节上,再考虑权重的排序即可。

猜你喜欢

转载自www.cnblogs.com/fremontxutheultimate/p/11277726.html