terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

wudabin edited 1 年,1 月前

报错terminate called after throwing an instance of ‘std::bad_alloc’
what(): std::bad_alloc,但是我在vscode上跑的通,不知道为什么

#include<iostream>
#include<vector>
#include<memory>
//make tree
struct tree {
    int data;
    tree* father;
    std::vector<tree*>* son;
};

int main(){
    int num;
    std::cin>>num;
    
    std::vector<tree*> trees;
    for(int i=0;i!=num;++i) {
        tree* tmp {new tree};
        std::vector<tree*> tmpv {};
        tmp->son = &tmpv;
        trees.push_back(tmp);
        std::cin>>tmp->data;
    }
    
    int first {};
    int second {};
    for(int i=0;i!=num - 1;++i) {
        std::cin>>first;
        std::cin>>second;
        trees[first-1]->son->push_back(trees[second-1]);
        trees[second-1]->father = trees[first-1];
    }
    
    int result = 0;
    for(auto& x:trees){
        if(x->father&&(x->son)->size()!=0){
            for(auto& y:*(x->son)){
                if(x->father->data+y->data == x->data) ++result;
            }
        }
        std::vector<tree*>().swap(*(x->son));
    }
    
    std::cout<<result;
}

Comments