Cod sursa(job #945461)

Utilizator mitrutstrutMitrea Andrei Ionut mitrutstrut Data 1 mai 2013 22:36:02
Problema Asmax Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <stdio.h>
#include <iostream.h>
#define NMAX 16384
#define INF "asmax.in"
#define OUF "asmax.out"
int s[NMAX];
char bit[NMAX]={0};
struct nod
{
    int x;
    nod *next;
};
struct lst
{
    nod *p,*q;
}list[NMAX];
void deeps(int vf)
{
    if(list[vf].p!=NULL)
    {
        nod *op;
        op=list[vf].p;
        while(op!=NULL)
        {
            if(!bit[op->x])
            {
            bit[op->x]=1;
            deeps(op->x);
            if(s[op->x]>0) s[vf]+=s[op->x];
            bit[op->x]=0;
            }
            op=op->next;
        }
    }
}
 
int main()
{
    int n,i,a,b,max;
    nod *op;
    FILE *in,*out;
    in=fopen(INF,"r");
    out=fopen(OUF,"w");
    fscanf(in,"%d",&n);
    for(i=1;i<=n;i++)
    {
        fscanf(in,"%d",s+i);
        list[i].p=list[i].q=NULL;
    }
    max=-16777216;
    for(i=1;i<n;i++)
    {
           fscanf(in,"%d %d",&a,&b);
       op=new nod;
       op->x=b;op->next=NULL;
       if(list[a].p==NULL) list[a].p=list[a].q=op;
       else
       {
           list[a].q->next=op;
           list[a].q=op;
       }
       op=new nod;
       op->x=a;op->next=NULL;
       if(list[b].p==NULL) list[b].p=list[b].q=op;
       else
       {
           list[b].q->next=op;
           list[b].q=op;
       }
    }
    bit[1]=1;
    deeps(1);
    for(i=1;i<=n;i++) if(s[i]>max) max=s[i];
    fprintf(out,"%d",max);
    fclose(in);fclose(out);
    return 0;
}