Cod sursa(job #330481)

Utilizator freak93Adrian Budau freak93 Data 10 iulie 2009 08:34:04
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include<fstream>
#include<algorithm>
#define maxn 16007

using namespace std;

ifstream f("asmax.in");
ofstream g("asmax.out");

struct muchie
{
    int v;
    muchie *next;
} *a[maxn],*use;

struct nod
{
    nod *next;
    nod *son;
    int v;
    int smax;
} *root,*nil;

int n,i,j,x,y,m,val[maxn],passed[maxn],tmax=-0x3f3f3f3f;

void make(nod *d,int x)
{
   passed[x]=1;
   d->smax=0;
   for(muchie *i=a[x];i;i=i->next)
   {
       int r=i->v;
       if(passed[r]==0)
        {
            nod *p=new nod;
            p->next=d->son;
            p->son=0;
            d->son=p;
            make(p,r);
            d->smax+=max(p->smax,0);
        }
   }
   d->smax+=val[x];
   if(d->smax>tmax)
    tmax=d->smax;
}



int main()
{
    f>>n;
    for(i=1;i<=n;++i)
        f>>val[i];
    for(i=1;i<n;++i)
    {
        f>>x>>y;
        use=new muchie;
        use->v=y;
        use->next=a[x];
        a[x]=use;
        use=new muchie;
        use->v=x;
        use->next=a[y];
        a[y]=use;
    }

    nil=new nod;
    nil->smax=nil->v=0;
    nil->son=0;
    nil->next=0;

    root=new nod;
    root=nil;

    make(root,1);
    g<<tmax<<"\n";

    f.close();
    g.close();

    return 0;
}