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

Why my program still WA?Please help me!Thank you!
Posted by kingfisher 24 Mar 2003 19:17
#include<iostream.h>
#include<string.h>

#define max 101

int n,q,note[max][max],tree[max][2],used[max];
long total;

void init()
{
  int a,b,s;
  memset(note,0,sizeof(note));
  cin>>n>>q;
  while(cin>>a>>b>>s)
    {note[a][b]=note[b][a]=s;}
}

void make(int num)
{
  int i,p;
  p=0;
  for(i=2;i<=n;i++)
    if((note[num][i])&&(!tree[i][0]))
      {
    tree[num][p]=i;make(i);
    if(p)return;else p++;
      }
}

void find(int num)
{
  int a,b,i,j,k,sa,sb,ss,sum;
  a=tree[num][0];b=tree[num][1];
  if(a)find(a);else for(i=0;i<=q;i++)note[a][i]=0;
  if(b)find(b);else for(i=0;i<=q;i++)note[b][i]=0;
  sa=note[num][a];sb=note[num][b];
  for(i=0;i<=q;i++)note[num][i]=0;
  if(sa>sb)note[num][1]=sa;else note[num][1]=sb;
  for(i=1;i<=q;i++)
    {
      k=q-i;ss=sa+note[a][i];
      if(note[num][i+1]<ss)note[num][i+1]=ss;ss+=sb;
      for(j=1;j<=k;j++)
    {
      sum=ss+note[b][j];
      if(sum>note[num][i+j+2])note[num][i+j+2]=sum;
    }
    }
}

void work()
{
  memset(tree,0,sizeof(tree));
  memset(used,0,sizeof(used));
  tree[1][0]=0;
  make(1);
  find(1);
  total=note[1][q];
}

void out()
{
  cout<<total<<endl;
}

void main()
{
  init();
  work();
  out();
}
Please give me some tests!!Thank you!!
Posted by yellowgreen(*Jane*)^_^ 24 Mar 2003 20:03
> #include<iostream.h>
> #include<string.h>
>
> #define max 101
>
> int n,q,note[max][max],tree[max][2],used[max];
> long total;
>
> void init()
> {
>   int a,b,s;
>   memset(note,0,sizeof(note));
>   cin>>n>>q;
>   while(cin>>a>>b>>s)
>     {note[a][b]=note[b][a]=s;}
> }
>
> void make(int num)
> {
>   int i,p;
>   p=0;
>   for(i=2;i<=n;i++)
>     if((note[num][i])&&(!tree[i][0]))
>       {
>     tree[num][p]=i;make(i);
>     if(p)return;else p++;
>       }
> }
>
> void find(int num)
> {
>   int a,b,i,j,k,sa,sb,ss,sum;
>   a=tree[num][0];b=tree[num][1];
>   if(a)find(a);else for(i=0;i<=q;i++)note[a][i]=0;
>   if(b)find(b);else for(i=0;i<=q;i++)note[b][i]=0;
>   sa=note[num][a];sb=note[num][b];
>   for(i=0;i<=q;i++)note[num][i]=0;
>   if(sa>sb)note[num][1]=sa;else note[num][1]=sb;
>   for(i=1;i<=q;i++)
>     {
>       k=q-i;ss=sa+note[a][i];
>       if(note[num][i+1]<ss)note[num][i+1]=ss;ss+=sb;
>       for(j=1;j<=k;j++)
>     {
>       sum=ss+note[b][j];
>       if(sum>note[num][i+j+2])note[num][i+j+2]=sum;
>     }
>     }
> }
>
> void work()
> {
>   memset(tree,0,sizeof(tree));
>   memset(used,0,sizeof(used));
>   tree[1][0]=0;
>   make(1);
>   find(1);
>   total=note[1][q];
> }
>
> void out()
> {
>   cout<<total<<endl;
> }
>
> void main()
> {
>   init();
>   work();
>   out();
> }
>