ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1018. Binary Apple Tree

Can you find the mistake , ...please?
Posted by IOANNIS 27 Nov 2006 03:20
#include <iostream>

bool check[101];
int con[101][101];
int saveap[101][101];
int dp[101][101];
int down[101];
using namespace std;
int main()
{
    int N,i,j,v,v1,v2,ap,Q,all=0;
    cin>>N>>Q;

     for (i=1;i<=N;i++)
     dp[i][1]=-1;
    for (i=1;i<=N-1;i++)
    {

     cin>>v1>>v2>>ap;
     all+=ap;
     con[v1][0]++;
     con[v1][con[v1][0]]=v2;
     con[v2][0]++;
     con[v2][con[v2][0]]=v1;
     saveap[v1][v2]=ap;
     saveap[v2][v1]=ap;
    }
    if (N==Q)
    cout<<all<<"\n";
    else
    {

    int queue[101];
    int f=0;
    int l=1;
    int x,y,t,cv;

    for (i=1;i<=N;i++)
    {
        if (con[i][0]==1)
        {
         f++;
         queue[f]=i;
         check[i]=1;
        }
    }
    while (f>=l)
    {
          x=0;
          y=0;
          v=queue[l];

      for (i=1;i<=con[v][0];i++)
      {
          cv=con[v][i];
        if (dp[cv][1]==-1)
        { dp[v][1]=saveap[v][cv];
          down[cv]+=down[v]+1;
          if (check[cv]==0)
          { f++;

          check[cv]=1;
          queue[f]=cv;}
        }
        else
        {
        if (x==0)
        x=cv;
        else
        y=cv;
        }
      }

         for (j=2;j<=down[v]+1;j++)
          for (t=0;t<j;t++)
             if (j-1-t<=down[y]+1 && t<=down[x]+1)
             {
             if (dp[v][j]<dp[v][1]+dp[x][t]+dp[y][j-1-t])
             dp[v][j]=dp[v][1]+dp[x][t]+dp[y][j-1-t];
             }
      l++;
    }

     cout<<dp[1][1+Q]+1<<"\n";
    }
    return 0;
}