Cod sursa(job #330476)

Utilizator freak93Adrian Budau freak93 Data 10 iulie 2009 08:03:54
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include<fstream>
#include<vector>
#include<algorithm>
#define maxn 16007

using namespace std;

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

vector<int>a[maxn];

struct nod
{
    vector<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;
   int r;
   d.smax=0;
   d.v=val[x];
   while(a[x].size()>0)
   {
       r=a[x].back();
       a[x].pop_back();
       if(!passed[r])
        {

            d.son.push_back(nil);
            make(d.son.back(),r);
            d.smax+=max(d.son.back().smax,0);
        }
   }
   d.smax+=d.v;
   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;
        a[x].push_back(y);
        a[y].push_back(x);
    }

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

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

    return 0;
}