№1039
http://acm.timus.ru/problem.aspx?space=1&num=1039
ЖМИ ДАЛЕЕ>>>
РЕШЕНИЕ:
PASCAL
Var
T,F: Array[0..6000] of Integer;
O: Array[0..6000] of LongInt;
I,J,N,R,L,K: LongInt;
Function MaxF(K:LongInt):LongInt;
Var F1,F2,I,J: LongInt;
Begin
If O[K]<>0 then begin
MaxF:=O[K];
Exit;
End;
F1:=0; F2:=0;
For I:=1 to N do
If T[I]=K then begin
For J:=1 to N do
If T[J]=I then F2:=F2+MaxF(J);
F1:=F1+MaxF(I);
End;
F2:=F2+F[K];
If F2>F1 then O[K]:=F2
else O[K]:=F1;
MaxF:=O[K];
End;
Begin
R:=0;
FillChar(T,SizeOf(T),0);
FillChar(F,SizeOf(F),0);
FillChar(O,SizeOf(O),0);
Read(N);
For I:=1 to N do
Read(F[I]);
Repeat
Read(L,K);
T[L]:=K;
Until (K=0);
For I:=1 to N do
If T[I]=0 then R:=R+MaxF(I);
Writeln(R);
End.
1039. Anniversary Party
Ограничение времени: 1.0 секунды
Ограничение памяти: 16 МБ
Ограничение памяти: 16 МБ
Вступление
The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.
Задача
Your task is to make up a guest list with the maximal conviviality rating of the guests.
Исходные данные
The first line of the input contains a number N. 1 ≤ N ≤ 6000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from –128 to 127. After that the supervisor relation tree goes. Each line of the tree specification has the form
<L> <K>
which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line
0 0
Результат
The output should contain the maximal total rating of the guests.
Пример
исходные данные | результат |
---|---|
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0 | 5 |
Автор задачи: Марат Бакиров
Источник задачи: Пятый командный чемпионат УрГУ по программированию (Октябрь 2000 г.)
Источник задачи: Пятый командный чемпионат УрГУ по программированию (Октябрь 2000 г.)