1053 Path of Equal Weight (30 分)sort错误➕二维数组初始化错误➕dfs中push,pop错误

不会处理这种记录多条路径的,下边代码只有push没有pop,不知道在什么时候开始pop

void dfs(int root,int weigh){
    
    
    v[num].push_back(n[root].w);
    if(n[root].child.size()==0)
    {
    
    
        if(weigh==need){
    
    
            for(int i=0;i<v[num].size();i++)
                ans[num1][i]=v[num][i];
            num++;num1++;
        }
    }
    else{
    
    
        for(int i=0;i<n[root].child.size();i++){
    
    
            dfs(n[root].child[i],weigh+n[n[root].child[i]].w);
        }
    }
}

错误1⃣️后来想起了dfs的背包问题,push然后递归结束后pop,开始想不通pop和push应该放在那里,把它放在lfor循环里,这会导致多次push入相同结点

void dfs(int root,int weigh){
    
    
    v.push_back(n[root].w);
    if(n[root].child.size()==0)
    {
    
    
        if(weigh==need){
    
    
            for(int i=0;i<v.size();i++)
                ans[num1][i]=v[i];
            num1++;
        }
    }
    else{
    
    
        for(int i=0;i<n[root].child.size();i++){
    
    
            dfs(n[root].child[i],weigh+n[n[root].child[i]].w);
        }
    }
    v.pop_back();
}

错误2⃣️ 它总是显示加上num1++会段错误,所以一直在找dfs的错,其实是num1++会影响main中输出错误,二维数组不能这么初始化

ans[101][101]={
    
    -1};
可以写出ans[101][101]={
    
    {
    
    0,0,0,}....}
或者for循环

错误3⃣️:当查找结束后排序困难时,可以看看那棵树和查找的时候有什么规律,比如都是从左到右查找,那么直接讲孩子结点权重由小到大排序就可以,出现这种错误:忘了child是个int不是node,cmp对应的参数类型不对

In file included from a.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/iostream:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ostream:38:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ios:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/char_traits.h:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_algobase.h:71:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/predefined_ops.h:143:31: error: no viable conversion from 'int' to 'node'
        {
    
     return bool(_M_comp(*__it1, *__it2)); }

ac代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    
    
    int w;
    vector<int> child;
}n[101];
int N,m,need,num=0,ans[101][101],num1=0;
vector<int> v;
void dfs(int root,int weigh){
    
    
    v.push_back(n[root].w);
    if(n[root].child.size()==0)
    {
    
    
        if(weigh==need){
    
    
            for(int i=0;i<v.size();i++)
                ans[num1][i]=v[i];
            num1++;
        }
    }
    else{
    
    
        for(int i=0;i<n[root].child.size();i++){
    
    
            dfs(n[root].child[i],weigh+n[n[root].child[i]].w);
        }
    }
    v.pop_back();
}
bool cmp(int a,int b)
{
    
    
    return n[a].w>n[b].w;
}
int main(){
    
    
    cin>>N>>m>>need;
    for(int i=0;i<N;i++)
        cin>>n[i].w;
    for(int i=0;i<m;i++){
    
    
        int temp,num,t;
        cin>>temp>>num;
        for(int j=0;j<num;j++)
        {
    
    
            cin>>t;
            n[temp].child.push_back(t);
        }
        sort(n[temp].child.begin(),n[temp].child.end(),cmp);
    }
    dfs(0,n[0].w);
    for(int i=0;i<num1;i++){
    
    
        cout<<ans[i][0];
        for(int j=1;ans[i][j]!=0;j++)
        {
    
    
            cout<<" "<<ans[i][j];
        }
        cout<<endl;
    }
}


猜你喜欢

转载自blog.csdn.net/qq_42835526/article/details/113720647