uva10859 树形dp

这道题太让我恶心了,用别人ac的代码和自己的答案进行对拍,没错,但就是ac不了,希望大神可以帮我一下(按道理这题提交不成功是因为没有用记忆花搜索,应该是tle,但却是wa,懒得改为记忆花搜索了,毕竟答案都是错的,那就没必要去纠结超时的问题了但我好像知道wa在哪里了)

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
#include <cstring>
#define M 2000
#define inf 0x3f3f3f3f
int n,m,dp[1010][2],head[1001];//dp[x][0]表示这个点不取,dp[x][1]表示取这个点
vector<int> eage[1010];

int dfs(int son,int father,int ope)
{
    int i,j,k,choose,choose1;
    if(ope==0)
        choose=2001;
    else
    {
        choose=2000;
        choose1=1;
    }
    if(eage[son].size()==1&&ope==0)
    {
        return 2000+1;
    }
    if(eage[son].size()==1&&ope==1)
    {
       return 1;
    }
    for(i=0;i<eage[son].size();i++)
    {
        j=eage[son][i];
        if(j!=father)
        {
           if(ope==0)
           {
              choose+=dfs(j,son,1);
           }
           else
           {
               choose+=dfs(j,son,1);
               choose1+=dfs(j,son,0);
           }
        }
    }
    if(ope==1)
    choose=min(choose,choose1);
    return choose;
}

int main()
{
    //freopen("d:\\in.txt","r",stdin);
    int T,ans1=2000,ans2=0;
    scanf("%d",&T);
    while(T)
    {
        for(int i=0;i<=1001;i++)
            eage[i].clear();
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        memset(dp,inf,sizeof(dp));
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            eage[a].push_back(b);
            eage[b].push_back(a);
        }
        ans1=2000,ans2=0;
        for(int i=0;i<eage[0].size();i++)
        {
           int j;
           j=eage[0][i];
           ans1+=dfs(j,0,1);
           ans2+=dfs(j,0,0);
        }
        int ans=min(ans1,ans2);
        printf("%d %d %d\n",ans/2000,m-ans%2000,ans%2000);
        T--;
    }
    return 0;
}//写的又臭又长

你可能感兴趣的:(uva10859 树形dp)