【C】树的遍历

1004. Counting Leaves (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), 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.

Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting 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

//每层有多少个叶子节点
//root为第0层
#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;
struct node{
	vector<int> child;
}Node[110];
int leaves[110]={0};//记录每层叶子节点数
int n,m;
int level=0;
void bfs(int index){
	int i;
	queue<int> q;
	q.push(index);
	int front=1,begin=0,rear=1;
	while(!q.empty()){
		int p=q.front();
		q.pop();begin++;
		for(i=0;i<Node[p].child.size();i++){
			q.push(Node[p].child[i]);
			rear++;
		}
		if(Node[p].child.size()==0) leaves[level]++;
		if(front==begin){
			level++;
			front=rear;
		}
	}
}
int main(){
	int i,j;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		for(j=0;j<b;j++){
			int c;
			scanf("%d",&c);
			Node[a].child.push_back(c);
		}
	}
	bfs(1);
	for(i=0;i<level-1;i++){
		printf("%d ",leaves[i]);
	}
	printf("%d",leaves[level-1]);
	return 0;
}//使用1094题的测试用例


1053. Path of Equal Weight (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Figure 1: 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 Figure 1.


Figure 1

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0 < N <= 100, the number of nodes in a tree, M (< N), the number of non-leaf nodes, and 0 < S < 230, the given weight number. The next line contains N positive numbers where Wi (<1000) 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 {A1, A2, ..., An} is said to be greater than sequence {B1, B2, ..., Bm} if there exists 1 <= k < min{n, m} such that Ai = Bi for i=1, ... k, 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<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,s;
int path[110];
struct node{
	int data;
	vector<int> child;
}Node[110];
bool cmp(int a,int b){
	return Node[a].data>Node[b].data;
}
void dfs(int index,int numNode,int sum){
	int i;
	if(sum>s) return;
	if(sum==s){
		if(Node[index].child.size()!=0) return;//不是叶子
		for(i=0;i<numNode;i++){
			printf("%d",Node[path[i]].data);
			if(i<numNode-1) printf(" ");
			else printf("\n");
		}
		return;
	}
	for(i=0;i<Node[index].child.size();i++){
		int child=Node[index].child[i];
		path[numNode]=child;
		dfs(child,numNode+1,sum+Node[child].data);
	}
}
int main(){
	int j,i;
	scanf("%d %d %d",&n,&m,&s);
	for(i=0;i<n;i++){
		scanf("%d",&Node[i].data);
	}
	for(i=0;i<m;i++){
		int a,num;
		scanf("%d %d",&a,&num);
		for(j=0;j<num;j++){
			int c;
			scanf("%d",&c);
			Node[a].child.push_back(c);
		}
		sort(Node[a].child.begin(),Node[a].child.end(),cmp);
	}
	path[0]=0;
	dfs(0,1,Node[0].data);
	return 0;
}


1079. Total Sales of Supply Chain (25)

时间限制
250 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
struct node{
	int data;
	vector<int> child;
	int total;
}Node[100010];
int n,leafnum=0,num=0;
double p,r;
double sum=0;
int childd[100010]={0};
void dfs(int index,int len){
	int i;
	if(Node[index].child.size()==0){
		//printf("%d %.2f\n",index,sum);
		sum=sum+p*pow(r*0.01+1.0,len)*Node[index].total;
		num++;//已经处理的叶子数
		return;
	}
	else{
		for(i=0;i<Node[index].child.size();i++){
			dfs(Node[index].child[i],len+1);
		}
	}
}
int main(){
	int i,j;
	scanf("%d %lf %lf",&n,&p,&r);
	for(i=0;i<n;i++){
		int cn;
		scanf("%d",&cn);
		for(j=0;j<cn;j++){
			int child;
			scanf("%d",&child);
			childd[child]=1;
			Node[i].child.push_back(child);//注意插入方式,不能是&Node[i].child[j]
		}
		if(cn==0){
			leafnum++;
			scanf("%d",&Node[i].total);
		}
	}
	/*for(i=0;i<n;i++){
		if(leafnum==num) break;
		dfs(i,0);
	}*///这种方法是计算叶子数,当叶子数达到时,就不再遍历 
	for(i=0;i<n;i++){
		if(childd[i]==0) break;
	}
	dfs(i,0); //这种方法寻找父亲,即遍历哪个节点没有当过孩子就是父亲,从父亲开始dfs 
	printf("%.1f",sum);
	return 0;
}

1090. Highest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); 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 -1. 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 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

#include<stdio.h>
#include<vector>
#include<math.h>
using  namespace std;
struct node{
	int data;
	vector<int> child;
}Node[100010];
int n,maxlen=0,num=0;
double p,r,sum;
void dfs(int index,int len){
	int i;
	if(Node[index].child.size()==0){
		if(len>maxlen){
			//printf("len=%d\n",len);
			maxlen=len;
			num=1;
		}
		else if(len==maxlen) num++;
		return;
	}
	else{
		for(i=0;i<Node[index].child.size();i++){
			dfs(Node[index].child[i],len+1);
		}
	}
}
int main(){
	scanf("%d %lf %lf",&n,&p,&r);
	int i,root;
	for(i=0;i<n;i++){
		int parent;
		scanf("%d",&parent);
		if(parent!=-1) Node[parent].child.push_back(i);
		else root=i;
	}
	dfs(root,0);
	sum=p*pow(r*0.01+1.0,maxlen);
	printf("%.2f %d",sum,num);
	return 0;
}


1094. The Largest Generation (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<N) 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 (>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<stdio.h>
#include<queue>
using namespace std;
int n,m;
int maxlevel=1,maxlen=1;//测试点2:如果M=1,N=0,所以初值均设为1
//int childd[110]={0};
struct node{
	vector<int> child;
}Node[110];
void bfs(int index){
	int i;
	int front=1,rear=1,begin=0,level=1;
	queue<int> q;
	q.push(index);
	while(!q.empty()){
		int p=q.front();
		q.pop();begin++;
		for(i=0;i<Node[p].child.size();i++){
			q.push(Node[p].child[i]);
			rear++;
		}
		if(begin==front){
			level++;
			int len=rear-front;
			if(len>maxlen){
				maxlen=len;
				maxlevel=level;
			}
			front=rear;
		}
	}
}
int main(){
	scanf("%d %d",&n,&m);
	int i,j;
	for(i=0;i<m;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		for(j=0;j<b;j++){
			int c;
			scanf("%d",&c);
			Node[a].child.push_back(c);
			//childd[c]=1;
		}
	}
	/*for(i=1;i<=n;i++){//这里注意i是从1-n
		if(childd[i]==0) break;
	}*/
	bfs(1);//由于定义了祖先是1
	printf("%d %d",maxlen,maxlevel);
	return 0;
}

1106. Lowest Price in Supply Chain (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=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 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 1010.

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<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
struct node{
	vector<int> child;
}Node[100010];
int n,minlen=100010,num=0;
double p,r;
void dfs(int index,int len){
	int i=0;
	if(Node[index].child.size()==0){
		if(len<minlen){
			num=1;
			minlen=len;
		}
		else if(len==minlen) num++;
		return;
	}
	for(i=0;i<Node[index].child.size();i++){
		dfs(Node[index].child[i],len+1);
	}
}
int main(){
	int j,i;
	scanf("%d %lf %lf",&n,&p,&r);
	for(i=0;i<n;i++){
		int a;
		scanf("%d",&a);
		for(j=0;j<a;j++){
			int b;
			scanf("%d",&b);
			Node[i].child.push_back(b);
		}
	}
	dfs(0,0);
	double sum=p*pow(r*0.01+1.0,minlen);
	printf("%.4f %d",sum,num);
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值