pat甲级---DFS、BFS、层序遍历合集

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<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.

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 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<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> v[100];
int book[100],maxdepth=-1;
void dfs(int index,int depth){
	if(v[index].size()==0){
		maxdepth=max(maxdepth,depth);
	    book[depth]++;
	}
	for(int i=0;i<v[index].size();i++){
		dfs(v[index][i],depth+1);
	} 
}
int main(){
	int a,b;
	while(cin>>a>>b){
		for(int i=0;i<b;i++){
			int c,d;
			cin>>c>>d;
			for(int j=0;j<d;j++){
				int e;
				cin>>e;
				v[c].push_back(e);
			}
		}
		dfs(1,0);
		for(int i=0;i<=maxdepth;i++){
			if(i!=maxdepth)
			cout<<book[i]<<" ";
			else
			cout<<book[i]<<endl; 
		} 
	}
} 

1018 Public Bike Management (30分)
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S
​3
​​ , we have 2 different shortest paths:

PBMC -> S
​1
​​ -> S
​3
​​ . In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S
​1
​​ and then take 5 bikes to S
​3
​​ , so that both stations will be in perfect conditions.

PBMC -> S
​2
​​ -> S
​3
​​ . This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:
Each input file contains one test case. For each case, the first line contains 4 numbers: C
​max
​​ (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; S
​p
​​ , the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers C
​i
​​ (i=1,⋯,N) where each C
​i
​​ is the current number of bikes at S
​i
​​ respectively. Then M lines follow, each contains 3 numbers: S
​i
​​ , S
​j
​​ , and T
​ij
​​ which describe the time T
​ij
​​ taken to move betwen stations S
​i
​​ and S
​j
​​ . All the numbers in a line are separated by a space.

Output Specification:
For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0−>S
​1
​​ −>⋯−>S
​p
​​ . Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S
​p
​​ is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge’s data guarantee that such a path is unique.

Sample Input:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
Sample Output:
3 0->2->3 0
代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 99999999;
int cmax, n, sp, m;
int minNeed = inf, minBack = inf;
int e[510][510], dis[510], weight[510];
bool visit[510];
vector<int> pre[510], path, temppath;
void dfs(int v) {
    temppath.push_back(v);
    if(v == 0) {
        int need = 0, back = 0;
        for(int i = temppath.size() - 1; i >= 0; i--) {
            int id = temppath[i];
            if(weight[id] > 0) {
                back += weight[id];
            } else {
                if(back > (0 - weight[id])) {
                    back += weight[id];
                } else {
                    need += ((0 - weight[id]) - back);
                    back = 0;
                }
            }
        }
        if(need < minNeed) {
            minNeed = need;
            minBack = back;
            path = temppath;
        } else if(need == minNeed && back < minBack) {
            minBack = back;
            path = temppath;
        }
        temppath.pop_back();
        return ;
    }
    for(int i = 0; i < pre[v].size(); i++)
        dfs(pre[v][i]);
    temppath.pop_back();
}
int main() {
    fill(e[0], e[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    scanf("%d%d%d%d", &cmax, &n, &sp, &m);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &weight[i]);
        weight[i] = weight[i] - cmax / 2;
    }
    for(int i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        scanf("%d", &e[a][b]);
        e[b][a] = e[a][b];
    }
    dis[0] = 0;
    for(int i = 0; i <= n; i++) {
        int u = -1, minn = inf;
        for(int j = 0; j <= n; j++) {
            if(visit[j] == false && dis[j] < minn) {
                u = j;
                minn = dis[j];
            }
        }
        if(u == -1) break;
        visit[u] = true;
        for(int v = 0; v <= n; v++) {
            if(visit[v] == false && e[u][v] != inf) {
                if(dis[v] > dis[u] + e[u][v]) {
                    dis[v] = dis[u] + e[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(dis[v] == dis[u] + e[u][v]) {
                    pre[v].push_back(u);
                }
            }
        }
    }
    dfs(sp);
    printf("%d 0", minNeed);
    for(int i = path.size() - 2; i >= 0; i--)
        printf("->%d", path[i]);
    printf(" %d", minBack);
    return 0;
}

1021 Deepest Root (25分)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤10
​4
​​ ) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes’ numbers.

Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components where K is the number of connected components in the graph.

Sample Input 1:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components
代码:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int father[10010];
bool isRoot[10010];
int maxheight=0;
vector<int> temp,G[10010],ans; 
void init(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
		isRoot[i]=false;
	}
}
int find(int x){
	int a=x;
	while(x!=father[x])
	x=father[x];
	while(a!=father[a]){
		int z=a;
		father[z]=x;
		a=father[a];
	}
	return x;
}
void Union(int a,int b){
	if(find(a)!=find(b)){
		father[find(a)]=find(b);
	}
}
void dfs(int height,int pre,int node){
	if(height>maxheight){
		temp.clear();
		temp.push_back(node);
		maxheight=height;
	} 
	else if(height==maxheight){
		temp.push_back(node);
	}
	for(int i=0;i<G[node].size();i++){
		if(G[node][i]==pre)
		continue;
		dfs(height+1,node,G[node][i]);
	}
}
int main(){
	int n;
	while(cin>>n){
		init(n);
		for(int i=0;i<n-1;i++){
			int c,d;
			cin>>c>>d;
			G[c].push_back(d);
			G[d].push_back(c); 
			Union(c,d);
		}
		for(int i=1;i<=n;i++){
			isRoot[find(i)]=true;
		}
		int cnt=0;
		for(int i=1;i<=n;i++){
			if(isRoot[i]){
				cnt++;
			}
		}
		if(cnt!=1)
		cout<<"Error: "<<cnt<<" components"<<endl;
		else{
			dfs(1,-1,1);
			ans=temp;
			temp.clear();
			dfs(1,-1,ans[0]);
			for(auto it=temp.begin();it!=temp.end();it++){
			ans.push_back(*it);	
			}
			sort(ans.begin(),ans.end());
			for(auto it=ans.begin();it!=ans.end();it++){
				if(*it!=*(it+1)){
					cout<<*it<<endl;
				}
			} 
		}
	}
	return 0;
}

1076 Forwards on Weibo (30分)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:

M[i] user_list[i]
where M[i] (≤100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

Then finally a positive K is given, followed by K UserID’s for query.

Output Specification:
For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.

Sample Input:
7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6
Sample Output:
4
5
代码:

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct Node{
	int id,layer;
};
vector<vector<int>> v;
int l;
int Bfs(Node node){
	bool visit[1010]={false};
	queue<Node> q;
	 q.push(node);
	 visit[node.id]=true;
	 int sum=0;
	 while(!q.empty()){
	 	Node top=q.front();
	 	q.pop();
	 	int topid=top.id;
	 	for(int i=0;i<v[topid].size();i++){
	 		if(visit[v[topid][i]]!=true&&top.layer<l){
	 		Node next={v[topid][i],top.layer+1};
	 		q.push(next);
	 		visit[v[topid][i]]=true;
	 		sum++;
			 }	 		
		 }
	 }
	 return sum;
}
int main(){
	int num,m,fid,k,root;
	scanf("%d%d",&num,&l);
	v.resize(num+1);
	for(int i=1;i<=num;i++){
         scanf("%d",&m);
         for(int j=0;j<m;j++){
         	scanf("%d",&fid);
            v[fid].push_back(i);
		 }
	}
	scanf("%d",&k);
	for(int i=0;i<k;i++){
	  scanf("%d",&root);
	  Node s={root,0};
	  printf("%d\n",Bfs(s));	
	}
}

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 (≤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<iostream>
#include<vector>
using namespace std;
vector<vector<int>> v;
double rate;
int count[100010];
double sum=0;
void dfs(int root,double price){
	if(v[root].size()==0){
		sum+=price*count[root];
		return;
	}
	for(int i=0;i<v[root].size();i++){
		dfs(v[root][i],price*(1+rate/100));
	}
}
int main(){
	int num,n,t;
	double price;
	scanf("%d%lf%lf",&num,&price,&rate);
	v.resize(num);
	 for(int i=0;i<num;i++){
	 	scanf("%d",&n);
	 	if(n==0){
	 	  scanf("%d",&count[i]);	
		 }else{
		 	for(int j=0;j<n;j++){
	 		scanf("%d",&t); 
	 	  v[i].push_back(t);	
		 }
		 }	 	
	 }	
	 dfs(0,price);
	 printf("%.1f",sum); 
	return 0;
}

1087 All Roads Lead to Rome (30分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness – it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->…->ROM.

Sample Input:
6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
代码:

#include<iostream>
#include<map>
#include<vector>
#include<algorithm> 
using namespace std;
#define maxn 202
#define inf 99999999
vector<int> pre[maxn],path,temppath;
map<string, int> m1;
map<int, string> m2;
bool visit[maxn];
int dis[maxn];
int cost[maxn][maxn];
int weight[maxn];
int maxvalue=0,cntpath=0;
double maxavalue=0;
void dfs(int v){
	temppath.push_back(v);
	if(v==1){
		int value=0;
		for(int i=0;i<temppath.size();i++){
			value+=weight[temppath[i]];
		}
		double  avalue=value*1.0/(temppath.size()-1);
		if(maxvalue<value){
			maxvalue=value;
			maxavalue=avalue;
			path=temppath;
		}else if(maxvalue==value&&maxavalue<avalue){
			maxavalue=avalue;
			path=temppath;
		}
		temppath.pop_back();
		cntpath++;
		return;
	}
	for(int i=0;i<pre[v].size();i++){
		dfs(pre[v][i]);
	}
	temppath.pop_back();
}
int main(){
	fill(dis,dis+maxn,inf);
	fill(cost[0],cost[0]+maxn*maxn,inf);
	int n,m;
	string s;
	scanf("%d %d",&n,&m);
	cin>>s;
	m1[s]=1;
	m2[1]=s;
	for(int i=0;i<n-1;i++){
		string t;
		int h;
		cin>>t>>h;
		weight[i+2]=h;
		m1[t]=i+2;
		m2[i+2]=t;
	}
	for(int i=0;i<m;i++){
	  string j,k;
	  int g;
	  cin>>j>>k>>g;
	  cost[m1[k]][m1[j]]=cost[m1[j]][m1[k]]=g;
	}
	dis[1]=0;
	for(int i=0;i<n;i++){
		int u=-1,minn=inf;
		for(int j=1;j<=n;j++){
			if(visit[j]==false&&dis[j]<minn){
				minn=dis[j];
				u=j;
			}
		}
		if(u==-1) break;
		visit[u]=true;
		for(int v=1;v<=n;v++){
			if(visit[v]==false&&cost[u][v]!=inf){
				if(dis[v]>dis[u]+cost[u][v]){
					dis[v]=dis[u]+cost[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}else if(dis[v]==dis[u]+cost[u][v]){
					pre[v].push_back(u);
				} 
			}
		}
	}
	int num=m1["ROM"];
	dfs(num);
	printf("%d %d %d %d\n",cntpath,dis[num],maxvalue,(int)maxavalue);
	for(int i=path.size()-1;i>=1;i--){
		cout<<m2[path[i]]<<"->";
	}
	cout<<"ROM";
}

1091 Acute Stroke (30分)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.

Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).

Then L slices are given. Each slice is represented by an M×N matrix of 0’s and 1’s, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1’s to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.

figstroke.jpg

Figure 1

Output Specification:
For each case, output in a line the total volume of the stroke core.

Sample Input:
3 4 5 2
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
0 0 1 1
1 0 1 1
0 1 0 0
0 0 0 0
1 0 1 1
0 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1
1 0 0 0
Sample Output:
26
代码:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
bool visit[1300][130][60];
int arr[1300][130][60];
struct Node{
    int x,y,z;
};
int X[6]={0,0,1,0,0,-1};
int Y[6]={0,1,0,0,-1,0};
int Z[6]={1,0,0,-1,0,0};
int m,n,t,l;
bool isJudge(int x,int y,int z){
    if(x<0||x>=m||y<0||y>=n||z<0||z>=l)   return false;
    if(visit[x][y][z]==true||arr[x][y][z]==0) return false;
    return true;
}
int bfs(int x,int y,int z){
    Node temp;
    temp.x=x;
    temp.y=y;
    temp.z=z;
    int cnt=0;
    queue<Node> q;
    q.push(temp);
    visit[x][y][z]=true;
    while(!q.empty()){
        Node ans=q.front();
        q.pop();
        cnt++;
        for(int i=0;i<6;i++){
           temp.x=ans.x+X[i];
            temp.y=ans.y+Y[i];
            temp.z=ans.z+Z[i];
            if(isJudge(temp.x,temp.y,temp.z)){
               visit[temp.x][temp.y][temp.z]=true; 
               q.push(temp); 
            }
        }
    }
    if(cnt>=t)   return cnt;
    else      return 0;
    }
int main(){
    scanf("%d%d%d%d",&m,&n,&l,&t);
  for(int k=0;k<l;k++)
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            scanf("%d",&arr[i][j][k]);
    int sum=0;
    for(int k=0;k<l;k++){
          for(int i=0;i<m;i++){
          for(int j=0;j<n;j++){
                if(arr[i][j][k]==1&&visit[i][j][k]==false)
                    sum+=bfs(i,j,k);    
            }
        }
    }
    printf("%d\n",sum);
}

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 (<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<iostream>
#include<vector>
using namespace std;
#define maxn 101
vector<int> v[maxn];
vector<int> t[maxn];
int maxlevel=-1;
void dfs(int root,int level){
    t[level].push_back(root);
    if(level>maxlevel)
        maxlevel=level;
    if(v[root].size()==0)
        return;    
    for(int i=0;i<v[root].size();i++){
        dfs(v[root][i],level+1);
    }
}
int main(){
    int a,b,c,d,k;
    cin>>a>>b;
    for(int i=0;i<b;i++){
        cin>>c>>k;
        for(int j=0;j<k;j++){
            cin>>d;
            v[c].push_back(d);
        }
    }
    dfs(1,1);
    int maxnum=0,resultlevel=0;
    for(int i=1;i<=maxlevel;i++){
        if((t[i].size())>maxnum){
            maxnum=t[i].size();
            resultlevel=i;
        }
    }
    printf("%d %d",maxnum,resultlevel);
}

1103 Integer Factorization (30分)
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.

Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.

Output Specification:
For each case, if the solution exists, output in the format:

N = n[1]^P + … n[K]^P
where n[i] (i = 1, …, K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 12
​2
​​ +4
​2
​​ +2
​2
​​ +2
​2
​​ +1
​2
​​ , or 11
​2
​​ +6
​2
​​ +2
​2
​​ +2
​2
​​ +2
​2
​​ , or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen – sequence { a
​1
​​ ,a
​2
​​ ,⋯,a
​K
​​ } is said to be larger than { b
​1
​​ ,b
​2
​​ ,⋯,b
​K
​​ } if there exists 1≤L≤K such that a
​i
​​ =b
​i
​​ for i<L and a
​L
​​ >b
​L
​​ .

If there is no solution, simple output Impossible.

Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible
代码:

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
vector<int> tmp,curr,data;
int maxnum=0,amount,k,mi;
void init(int n){
    int number=0,index=-1;
    while(number<n){
        index++;
        number=pow(index,mi);
        data.push_back(number);
    }
}
void dfs(int index,int tempk,int tmpans,int facsum){ 
    if(tempk==k){
    	if(tmpans==amount&&facsum>maxnum){
            maxnum=facsum;
            curr=tmp;	
		}
        return;
    }
   while(index>=1){   	
        if(tmpans+data[index]<=amount){
            tmp[tempk]=index;
            dfs(index,tempk+1,tmpans+data[index],facsum+index);
        }
        index--;
    }
}
int main(){
    scanf("%d%d%d",&amount,&k,&mi);
    tmp.resize(k);
    init(amount);
    dfs(data.size()-1,0,0,0);
    if(maxnum==0){
        printf("Impossible");
    }else{
        printf("%d = %d^%d",amount,curr[0],mi);
        for(int i=1;i<curr.size();i++){
            printf(" + %d^%d",curr[i],mi);
        }
    }
}

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 (≤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<iostream>
#include<vector>
#include<cmath>
using namespace std;
int minn=99999999;
double rate;
int sum=1;
vector<int> v[100010];
void dfs(int root,int depth){
	 if(v[root].size()==0){
	 if(minn==depth){
	     	sum++;
		 }else if(minn>depth){
			sum=1;
			minn=depth;
		}
         return;
	 }		
	for(int i=0;i<v[root].size();i++){
		dfs(v[root][i],depth+1);
	}
}
int main(){
	int a,x,y;
	double b;
	scanf("%d %lf %lf",&a,&b,&rate);
	for(int i=0;i<a;i++){
	   	scanf("%d",&x);
		for(int j=0;j<x;j++){
			scanf("%d",&y);
			v[i].push_back(y);
		}
	}
    dfs(0,0);
    printf("%.4f %d",b*pow((1+rate/100),minn),sum);
}

1127 ZigZagging on a Tree (30分)
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in “zigzagging order” – that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

zigzag.jpg

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
Sample Output:
1 11 5 8 17 12 20 15
作者
CHEN, Yue
单位
浙江大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码:

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int> in,post,result[35];
int n,tree[35][2],root;
struct Node{
	int index,depth;
};
void dfs(int &index,int inleft,int inright,int postleft,int postright){
	if(inleft>inright)  return;
	index=postright;
	int i=0;
	while(in[i]!=post[postright]) i++;
	dfs(tree[index][0],inleft,i-1,postleft,postleft+(i-inleft)-1);
	dfs(tree[index][1],i+1,inright,postleft+(i-inleft),postright-1);
} 
void bfs(){
	queue<Node> q;
	q.push(Node{root,0});
	while(!q.empty()){
		Node temp=q.front();
		q.pop();
		result[temp.depth].push_back(post[temp.index]);
		if(tree[temp.index][0]!=0)
		q.push(Node{tree[temp.index][0],temp.depth+1});
		if(tree[temp.index][1]!=0)
		q.push(Node{tree[temp.index][1],temp.depth+1});
	} 
}
int main(){
   cin>>n;
   in.resize(n+1),post.resize(n+1);
   for(int i=1;i<=n;i++) cin>>in[i];
   for(int i=1;i<=n;i++)  cin>>post[i];
  dfs(root,1,n,1,n);
  bfs();
  printf("%d",result[0][0]);
  for(int i=1;i<35;i++){
  	if(i%2==1){
  		for(int j=0;j<result[i].size();j++)
  		printf(" %d",result[i][j]);
  		
	  }else{
	  	for(int j=result[i].size()-1;j>=0;j--)
	  	printf(" %d",result[i][j]);
	  }
  }
  return 0;
}

1130 Infix Expression (25分)
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:

data left_child right_child
where data is a string of no more than 10 characters, left_child and right_child are the indices of this node’s left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.

infix1.JPG infix2.JPG
Figure 1 Figure 2
Output Specification:
For each case, print in a line the infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. There must be no space between any symbols.

Sample Input 1:
8

  • 8 7
    a -1 -1
  • 4 1
  • 2 5
    b -1 -1
    d -1 -1
  • -1 6
    c -1 -1
    Sample Output 1:
    (a+b)(c(-d))
    Sample Input 2:
    8
    2.35 -1 -1
  • 6 1
  • -1 4
    % 7 8
  • 2 3
    a -1 -1
    str -1 -1
    871 -1 -1
    Sample Output 2:
    (a*2.35)+(-(str%871))
    代码:
#include<iostream>
#include<vector>
using namespace std;
struct Node{
	int leftchild,rightchild;
};
vector<Node> v;
vector<int> q;
bool visit[21]={false};
string s[21];
void dfs(int root,int level){
	if(v[root].leftchild==-1&&v[root].rightchild==-1){
		cout<<s[root];
		return;	
	}
	if(level!=0)
	printf("(");			
	if(v[root].leftchild!=-1){	    	
		 dfs(v[root].leftchild,level+1);			 
	}	 	
      cout<<s[root];		
	if(v[root].rightchild!=-1){	
	dfs(v[root].rightchild,level+1);
	}
	if(level!=0)
	printf(")");
}
int main(){
	int num,x,y;
	cin>>num;
	v.resize(num+1);	
	for(int i=1;i<=num;i++){
	cin>>s[i]>>x>>y;
	Node n={x,y};
	v[i]=n;
	}
	for(int i=1;i<=num;i++){
		if(v[i].leftchild!=-1)
		visit[v[i].leftchild]=true;
		if(v[i].rightchild!=-1)
		visit[v[i].rightchild]=true;
	}
	int root=1;
	while(visit[root])   root++;
	dfs(root,0);
	return 0;
}

1131 Subway Map (30分)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest way to his/her destination.

subwaymap.jpg

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 100), the number of subway lines. Then N lines follow, with the i-th (i=1,⋯,N) line describes the i-th subway line in the format:

M S[1] S[2] … S[M]

where M (≤ 100) is the number of stops, and S[i]'s (i=1,⋯,M) are the indices of the stations (the indices are 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations are given in the correct order – that is, the train travels between S[i] and S[i+1] (i=1,⋯,M−1) without any stop.

Note: It is possible to have loops, but not self-loop (no train starts from S and stops at S without passing through another station). Each station interval belongs to a unique subway line. Although the lines may cross each other at some stations (so called “transfer stations”), no station can be the conjunction of more than 5 lines.

After the description of the subway, another positive integer K (≤ 10) is given. Then K lines follow, each gives a query from your user: the two indices as the starting station and the destination, respectively.

The following figure shows the sample map.

samplemap.jpg

Note: It is guaranteed that all the stations are reachable, and all the queries consist of legal station numbers.

Output Specification:
For each query, first print in a line the minimum number of stops. Then you are supposed to show the optimal path in a friendly format as the following:

Take Line#X1 from S1 to S2.
Take Line#X2 from S2 to S3.

where Xi’s are the line numbers and Si’s are the station indices. Note: Besides the starting and ending stations, only the transfer stations shall be printed.

If the quickest path is not unique, output the one with the minimum number of transfers, which is guaranteed to be unique.

Sample Input:
4
7 1001 3212 1003 1204 1005 1306 7797
9 9988 2333 1204 2006 2005 2004 2003 2302 2001
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
3
3011 3013
6666 2001
2004 3001
Sample Output:
2
Take Line#3 from 3011 to 3013.
10
Take Line#4 from 6666 to 1306.
Take Line#3 from 1306 to 2302.
Take Line#2 from 2302 to 2001.
6
Take Line#2 from 2004 to 1204.
Take Line#1 from 1204 to 1306.
Take Line#3 from 1306 to 3001.
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
vector<int> e[10000],line[110],temppath,path;
unordered_map<int,int> route; 
int minnum=99999,mintransfer=99999;
bool visit[10000];
int transfercnt(vector<int> a){
	int pre=0,t=0;
	for(int i=1;i<a.size();i++){
		if(route[a[i-1]*10000+a[i]]!=pre){
		   t++;
		   pre=route[a[i-1]*10000+a[i]];	
		}
	}
	return t;
}
void dfs(int start,int end,int cnt){
	if(start==end){
		temppath.push_back(start);
		if(minnum>cnt||(minnum==cnt&&mintransfer>transfercnt(temppath))){
		minnum=cnt;
		mintransfer=transfercnt(temppath);
		path=temppath;
	}
		return;	
	}
	temppath.push_back(start);
	visit[start]=true;
	for(int i=0;i<e[start].size();i++){
		if(visit[e[start][i]]==false){
		dfs(e[start][i],end,cnt+1);	
		visit[e[start][i]]=false;
		temppath.pop_back();
		}				
	}	
}
int main(){
	int num1,num2,num3,start,end,x;
	cin>>num1;
	for(int i=1;i<=num1;i++){
		cin>>num2;
		for(int j=0;j<num2;j++){
			cin>>x;
			line[i].push_back(x);
		}
	}
	for(int i=1;i<=num1;i++){
		for(int j=0;j<line[i].size()-1;j++){
			e[line[i][j]].push_back(line[i][j+1]);
			e[line[i][j+1]].push_back(line[i][j]);
			route[line[i][j]*10000+line[i][j+1]]=route[line[i][j]+line[i][j+1]*10000]=i;
		}
	}
	cin>>num3;
	for(int i=0;i<num3;i++){
		cin>>start>>end;
		minnum=99999,mintransfer=99999;
		fill(visit,visit+10000,false);
		temppath.clear();
		dfs(start,end,0);
		printf("%d\n",minnum);	
		int pre=0,startransfer=start;
		for(int i=1;i<path.size();i++){
			if(route[path[i-1]*10000+path[i]]!=pre){
				if(pre!=0) printf("Take Line#%d from %04d to %04d.\n",pre,startransfer,path[i-1]);
				pre=route[path[i-1]*10000+path[i]];				
			    startransfer=path[i-1];
			}
		} 
		printf("Take Line#%d from %04d to %04d.\n",pre,startransfer,end);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值