目录
7.Highest Price in Supply Chain
1.结构体的定义
struct node{
typename data;
int child[maxn];
}nodes[maxn];
struct node{
typename data;
vector child;
}nodes[maxn];
2.新建节点
int index=0;
int newnode(int v){
nodes[index].data=v;
nodes[index].child.clear();
return index++;
}
3.树的先根遍历
void preorder(int root){
printf("%d ",nodes[root].data);
for(int i=0;i<nodes[root].child.size();i++){
preorder(nodes[root].child[i]);
}
}
4.层序遍历
void layerorder(int root){
queue<int> q;
q.push(root);
nodes[root].layer=0;
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=0;i<nodes[front].child.size();i++){
int child=nodes[front].child[i];
nodes[child].layer=nodes[front].layer+1;
q.push(child);
}
}
}
5.Path of Equal Weight
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=110;
struct node{
int weight;
vector<int> child;
}nodes[maxn];
bool cmp(int a,int b){
return nodes[a].weight>nodes[b].weight;
}
int n,m,s;
int path[maxn];
void dfs(int index,int numnode,int sums){
if(sums>s) return;
if(sums==s){
if(nodes[index].child.size()!=0) return;
for(int i=0;i<numnode;i++){
printf("%d",nodes[path[i]].weight);
if(i<numnode-1) printf(" ");
else printf("\n");
}
return;
}
for(int i=0;i<nodes[index].child.size();i++){
int child=nodes[index].child[i];
path[numnode] = child;
dfs(child,numnode+1,sums+nodes[child].weight);
}
}
int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=0;i<n;i++){
scanf("%d",&nodes[i].weight);
}
int id,k,child;
for(int i=0;i<m;i++){
scanf("%d%d",&id,&k);
for(int j=0;j<k;j++){
scanf("%d",&child);
nodes[id].child.push_back(child);
}
sort(nodes[id].child.begin(),nodes[id].child.end(),cmp);
}
path[0] = 0;
dfs(0,1,nodes[0].weight);
return 0;
}
6.Total Sales of Supply Chain
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct node{
double data;
vector<int> child;
}nodes[maxn];
int n;
double p,r,ans=0;
void dfs(int index,int depth){
if(nodes[index].child.size()==0){
ans+=nodes[index].data*pow(1+r,depth);
return;
}
for(int i=0;i<nodes[index].child.size();i++){
dfs(nodes[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",&nodes[i].data);
}else{
for(int j=0;j<k;j++){
scanf("%d",&child);
nodes[i].child.push_back(child);
}
}
}
dfs(0,0);
printf("%.1f\n",p*ans);
return 0;
}
7.Highest Price in Supply Chain
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100010;
vector<int> child[maxn];
double p,r;
int n,maxdepth=0,num=0;
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);
}
}
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);
}else{
root=i;
}
}
dfs(root,0);
printf("%.2f %d\n",p*pow(1+r,maxdepth),num);
return 0;
}