Untitled

 avatar
unknown
plain_text
5 months ago
1.4 kB
3
Indexable
#include<bits/stdc++.h>
using namespace std;

struct node{
    int left;
    int right;
    int weight;
};

vector<node>v;
vector<int>freight;

void weight_search(int index){
    if(v[index].left == 0 && v[index].right == 0){
        return;
    }
    weight_search(v[index].left);
    weight_search(v[index].right);
    v[index].weight = v[v[index].left].weight + v[v[index].right].weight;
}

int assign(int temp_weight , int index){
    v[index].weight += temp_weight;
    if(v[index].left == 0 && v[index].right == 0){
        return index;
    }
    if(v[v[index].left].weight <= v[v[index].right].weight){
        return assign(temp_weight , v[index].left);
    }
    else{
        return assign(temp_weight , v[index].right);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n , m;
    cin>>n>>m;
    v.clear();
    freight.clear();
    freight.resize(m+5);
    v.resize(2*n+5);
    for(int i = n ; i< 2*n ; i++){
        cin>>v[i].weight;
        v[i].left = 0;
        v[i].right = 0;
    }
    for(int i = 0 ; i<m ; i++){
        cin>>freight[i];
    }
    for(int i = 1 ; i<n ; i++){
        int trash;
        cin>>trash>>v[i].left>>v[i].right;
        v[i].weight = 0;
    }
    weight_search(1);
    for(int i = 0 ; i<m ; i++){
        cout<<assign(freight[i] , 1)<<' ';
    }
}
Editor is loading...
Leave a Comment