7.28

1053 Path of Equal Weight (30)(30 分)提问

Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. 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 < 2^30^, the given weight number. The next line contains N positive numbers where W~i~ (&lt1000) corresponds to the tree node T~i~. 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 {A~1~, A~2~, ..., A~n~} is said to be greater than sequence {B~1~, B~2~, ..., B~m~} if there exists 1 <= k < min{n, m} such that A~i~ = B~i~ for i=1, ... k, and A~k+1~ > B~k+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;
struct node{
	int weight;
	vector<int> child;
	
}Node[maxn];
bool cmp(int a,int b)
{
	return Node[a].weight>Node[b].weight;
} 
int n, m,s;//结点数 边数 给定的和
int path[maxn];//记录路径

//当前访问结点为index numnode为当前路径path上的结点个数
//sum为当前的结点点权和
void DFS(int index,int numnode,int sum)
{
	if(sum>s) return ;//如果和超过s直接返回
	if(sum==s)
	{
		if(Node[index].child.size()!=0) return ;//还没到达叶子结点
		//到达叶子结点 此时存放了一条完整路径 输出
		for(int i=0;i<numnode;i++)
		{
			printf("%d",Node[path[i]].weight);
			if(i<numnode-1) printf(" ");
			else printf("\n");
		} 
		return ;
	} //否则继续 
	for(int i=0;i<Node[index].child.size();i++)//枚举所有子节点
	{
		int child=Node[index].child[i];//结点index的第i的子结点编号
		path[numnode]=child;//将结点child加到路径path末尾
		DFS(child,numnode+1,sum+Node[child].weight); 
	} 
}
int main()
{
	scanf("%d%d%d",&n,&m,&s);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&Node[i].weight);
	}
	int k,id,child;
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&id,&k);//结点编号,孩子个数 
		for(int j=0;j<k;j++)
		{
			scanf("%d",&child);
			Node[id].child.push_back(child);
		}
		sort(Node[id].child.begin(),Node[id].child.end(),cmp);
	}
	path[0]=0;//路径的第一个结点设置为0号结点 
	DFS(0,1,Node[0].weight);//DFS求解 
	return 0;
}

1079 Total Sales of Supply Chain (25)(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 (<=10^5^), 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:

K~i~ ID[1] ID[2] ... ID[K~i~]

where in the i-th line, K~i~ 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. K~j~ being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after K~j~. 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 10^10^.

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 <cmath>
#include <vector>
using namespace std;
const int maxn=100010;
struct node{
	double data;
	vector <int>child;//指针域 
}Node[maxn];//存放树 
int n;
double p,r,ans=0;//ans为价格之和
void DFS(int index,int depth)
{
	if(Node[index].child.size()==0)
	{
		ans+=Node[index].data*pow(1+r,depth);
		return ;
	}
	for(int i=0;i<Node[index].child.size();i++)
	{
		DFS(Node[index].child[i],depth+1);
	}
} 
int main()
{
	int k,child;
	scanf("%d%lf%lf",&n,&p,&r);
	r/=100;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&k);
		if(k==0)
		{
			scanf("%lf",&Node[i].data);
		}
		else{
			for(int j=0;j<k;j++)
			{
				scanf("%d",&child);
				Node[i].child.push_back(child);
		    }
		}	
	}
   	DFS(0,0);//DFS入口 
	printf("%.1f\n",p*ans);
	return 0;
}

又一次把j打成i

1090 Highest Price in Supply Chain (25)(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 (<=10^5^), 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 S~i~ is the index of the supplier for the i-th member. S~root~ 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 10^10^.

Sample Input:

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

Sample Output:

1.85 2
#include <cstdio> 
#include <cmath>
#include <vector>
using namespace std;
const int maxn=100010;
vector<int> child[maxn];//存放树 
double p,r;

int n,maxdepth=0,num=0;//最大深度 num为最大深度的结点个数 

void DFS(int index,int depth)
{
	if(child[index].size()==0)//到达叶节点 
	{
		if(depth>maxdepth)
		{
			maxdepth=depth;
			num=1;
		}
		else if(depth==maxdepth)
		     num++;
		return ;
	}
	for(int i=0;i<child[index].size();i++)
	DFS(child[index][i],depth+1);//递归访问结点index的子节点 	
} 
int main()
{
	int father,root;
	scanf("%d%lf%lf",&n,&p,&r); 
	r/=100;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&father);
		if(father!=-1)
		{
			child[father].push_back(i);//i是father的耳机 
		}
		else root=i;//根节点为root 
	}
	DFS(root,0);//入口 
	printf("%.2f %d\n",p*pow(1+r,maxdepth),num);
	return 0;
}

1094 The Largest Generation (25)(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 (&lt100) 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 (&ltN) 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 (&gt0) 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> Node[maxn];//树的静态写法 Node[i]里存放结点i的孩子结点编号
int hashtable[maxn]={0};//记录每层的结点个数
void DFS(int index,int level)
{
	hashtable[level]++;//第level层结点数+1 
	for(int j=0;j<Node[index].size();j++)
	{
		DFS(Node[index][j],level+1);//遍历所有孩子结点 进行递归 
	} 
} 
int main()
{
	int n,m,parent,k,child;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&parent,&k);
		for(int j=0;j<k;j++)
		{
			scanf("%d",&child);
			Node[parent].push_back(child);
		}
	}
	DFS(1,1);//结点为1号 层号为1
	int maxlevel=-1,maxvalue=0;
	for(int i=1;i<maxn;i++)
	{
		if(hashtable[i]>maxvalue)
		{
			maxvalue=hashtable[i];
			maxlevel=i; 
		}
	} 
	printf("%d %d\n",maxvalue,maxlevel);
	return 0;
} 

1106 Lowest Price in Supply Chain (25)(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 (<=10^5^), 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:

K~i~ ID[1] ID[2] ... ID[K~i~]

where in the i-th line, K~i~ 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. K~j~ 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 10^10^.

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 <cmath>
#include <vector>
using namespace std;
const int maxn=100010;
const double inf=1e12;
vector<int> Node[maxn];//NODE【i】存放所有孩子结点的编号
int n,num=0;
double p,r,ans=inf;
void DFS(int index,int depth)
{
	if(Node[index].size()==0)
{	double price=p*pow(1+r,depth);//当前价格
	  if(price<ans)
	  {
	  	ans=price;
	  	num=1;
	  } 
	  else if(price==ans)
	  num++;
	//return ;//全部返回 
		} 
	for(int i=0;i<Node[index].size();i++)
	 DFS(Node[index][i],depth+1);
} 
int main()
{
	int k,child;
	scanf("%d%lf%lf",&n,&p,&r);
	r/=100;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&k);
		if(k!=0)
	    {
	    	for(int j=0;j<k;j++)
	    	{
	    		scanf("%d",&child);
	    		Node[i].push_back(child);
	    	}
	    }
	}
	DFS(0,0);
	printf("%.4f %d\n",ans,num);
	return 0;
}






 事实证明 去掉return也可以

1004 Counting Leaves (30)(30 分)提问

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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N=110;

vector<int> G[N];//存放树
int leaf[N]={0};//存放每层的叶子结点的个数
int maxh=1;//树的深度 

void DFS(int index,int h)
{
	maxh=max(maxh,h);
	if(G[index].size()==0)
	{
		leaf[h]++;
		return;
	}
	for(int i=0;i<G[index].size();i++)
	{
		DFS(G[index][i],h+1);
	}
}
int main()
{
	int n,m,parent,child,k;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++) 
	{
		scanf("%d%d",&parent,&k);//父编号及子节点个数 
		for(int j=0;j<k;j++)
		{
			scanf("%d",&child);
			G[parent].push_back(child);
		}	
	}
	DFS(1,1);
	printf("%d",leaf[1]);
	for(int i=2;i<=maxh;i++)
	  printf(" %d",leaf[i]);
	return 0;
}
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int N=105;
vector<int >G[N];//树
int h[N]={0};//各节点所处的层数 
int leaf[N]={0};//每层的叶子结点个数 
int maxh=0;

void BFS()
{
	queue<int >Q;
	Q.push(1); 
	while(!Q.empty())
	{
		int id=Q.front();
		Q.pop();
		maxh=max(maxh,h[id]);
		if(G[id].size()==0)
		{
			leaf[h[id]]++;
		}
		for(int i=0;i<G[id].size();i++)//枚举所有子节点 
		{
			h[G[id][i]]=h[id]+1;
			Q.push(G[id][i]);
		}
		
	}
} 
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int parent,k,child;
		scanf("%d%d",&parent,&k);
		for(int j=0;j<k;j++)
		{
			scanf("%d",&child);
			G[parent].push_back(child);
		}
		
	}
	h[1]=1;//初始化根节点 
	BFS();
	for(int i=1;i<=maxh;i++)
	{
		if(i==1) printf("%d",leaf[i]);
		else printf(" %d",leaf[i]);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Coding18/article/details/81265587