Cod sursa(job #928407)

Utilizator LuffyBanu Lavinia Luffy Data 26 martie 2013 13:58:10
Problema Asmax Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<stdio.h>
#include <iostream>
#define dim 16005
using namespace std;

int i,n,p,x,j,y,rad,cost[dim],mat[dim][dim],viz[dim],nod[dim],best[dim];
long long Max=-99999999;

int sum = 0;

void DF(int x)
{int i;

  viz[x] = 1;

  for(i=1; i<=mat[x][0]; i++)
   if(! viz[mat[x][i]] )
    {
     DF(mat[x][i]);
     if(best[x] + best[mat[x][i]] > best[x])
       {best[x] = best[x] + best[mat[x][i]];
        if(best[x] > Max) Max = best[x];
       }
    }




}




int main()
{
 FILE *f=fopen("asmax.in","r"), *g=fopen("asmax.out","w");

 fscanf(f,"%d",&n);
  for(i=1; i<=n; i++)
 {fscanf(f,"%d",&best[i]); if(best[i] > Max) Max = best[i];}


  for(i=1; i<n; i++)
 {fscanf(f,"%d%d",&x,&y);
  mat[x][0]++; mat[x][mat[x][0]]=y;
  mat[y][0]++; mat[y][mat[y][0]]=x;
 }

 DF(1);

 fprintf(g,"%lld\n",Max);

fclose(f);
fclose(g);
return 0;
}