题目
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 (<=105), the total number of the members in the supply chain (and hence their ID’s are numbered from 0 to N-1, 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 1010.
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
分析题目
题目大意:给一颗树,有N个节点,根节点的权重为P,非叶子节点之间有边权(1+r),叶子节点有权重Kj,输出最后的总权值
分析题目:用结构体数组完成数据结构,dfs遍历
代码
#include
#include
#include
#include
using namespace std;
struct node{
bool isRetailer;
int productAmount;
int childNum;
vector child;
};
vector tree;
double result = 0.0;
double P, r;
void dfs(int root, int depth)
{
if(tree[root].isRetailer)
{
result += tree[root].productAmount*(pow(1+r/100,depth));
return;
}
for(int i = 0; i> N >> P >> r;
tree.resize(N);
for(int i = 0; i> Ki;
if(!Ki)
{
tree[i].isRetailer = true;
cin >> tree[i].productAmount;
}
else
{
tree[i].isRetailer = false;
tree[i].childNum = Ki;
tree[i].child.resize(Ki);
for(int j = 0; j> tree[i].child[j];
}
}
dfs(0,0);
printf("%.1f", result*P);
return 0;
}
总结
dfs题目做多了之后,这道题思路相对比较清晰了
在调试的时候命名起了冲突,题目的r和dfs函数常用的根节点变量r冲突了,以后命名根节点的时候用root,同时看题的时候要注意题目的变量是否和常用的变量冲突。
PAT 1094. The Largest Generation (25)-(bfs,dfs,树的遍历)
题目
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 (<100) 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 ( ID K ID[1] ID[2] … ID[K] where ID is a two-digit number representing a family member, K (>0) 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
总结
错误代码:#include
在对level求解的时候,容易犯两个错误,
for(int i = 0; i<100;i++)
这里是i从0开始遍历了,但0层是没东西的,也无伤大雅,调试能过
for(int i = 1; i
for(int i = 1; i<100; i++)
{
if(maxPopulation < level[i])
{
maxPopulation = level[i];
generation = i;
}
}