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

AC coad .wa many times only because the input do not say which node is father
Posted by mcdoing_ron 10 Sep 2010 20:27
#include<iostream>
using namespace std;
const int maxn=105;
int count[maxn]; int g[maxn][maxn]; int n;int q; int cost[maxn][maxn]; int f[maxn][maxn]; int visit[maxn];
void init( )
{
            cin>>n>>q;
            for(int i=1;i<n;i++)
            {
                        int a,b,c;
                        scanf("%d %d %d",&a,&b,&c);
                        g[a][++count[a]]=b;
                        cost[a][b]=c;
                        g[b][++count[b]]=a;
                        cost[b][a]=c;
            }
            //memset(f,-10000,sizeof(f));
}
void dfs(int x)
{
            visit[x]=1;
            if(count[x]==0)
            {

                        return ;
            }
            for(int i=1;i<=count[x];i++)
            {
                        int child=g[x][i];
                        if(visit[child]==0){
                                    dfs(child);
                        for(int j=q;j>=0;j--)
                                    for(int k=q-j-1;k>=0;k--)
                                    {
                                                f[x][k+j+1]=max(f[child][k]+f[x][j]+cost[x][child],f[x][k+j+1]);

                                    }
                        }
            }
}
void print( )
{
            cout<<f[1][q]<<endl;
}
int main( )
{
            freopen("in.txt","r",stdin);
            init( );
            dfs(1);
            print( );
            return 0;
}
Re: AC coad .wa many times only because the input do not say which node is father
Posted by dauren.ktl 25 Nov 2010 17:16
Please explain your solution. Thanks