By the Pit ... HDU are all read into multiple sets of data.
----------------------------------------------------------------------------------------------
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace Std;
#define REP (i,n) for (int i=1;i<=n;i++)
#define CLR (x,c) memset (x,c,sizeof (x))
int read () {
int x=0;
Char C=getchar ();
int f=1;
while (!isdigit (c)) {
if (c== '-') f=-1;
C=getchar ();
}
while (IsDigit (c)) {
x=x*10+c-' 0 ';
C=getchar ();
}
return x*f;
}
vector<int>e[6005];
int w[6005],v[6005],f[6005][2];
void dp (int x) {
for (int i=0;i<e[x].size (); i++) {
DP (E[x][i]);
F[X][1]+=F[E[X][I]][0];
F[x][0]+=max (f[e[x][i]][0],f[e[x][i]][1]);
}
F[X][1]+=W[X];
return;
}
int main () {
int n;
while (scanf ("%d", &n) ==1) {
Rep (I,n) e[i].clear ();
CLR (v,0); CLR (f,0);
Rep (i,n) {
W[i]=read ();
}
int s,t;
while (scanf ("%d%d", &s,&t) ==2&&s&&t) {
E[t].push_back (s);
V[s]=1;
}
Rep (i,n) if (v[i]==0) s=i;
/*rep (i,n) {
printf ("%d\n", I);
for (int j=0;j<e[i].size (); j + +)
printf ("%d", e[i][j]);
printf ("\ n");
}
printf ("%d\n", s); */
DP (s);
printf ("%d\n", Max (f[s][0],f[s][1]));}
return 0;
}
----------------------------------------------------------------------------------------------
Anniversary party
time limit:2000/1000 MS (java/others) memory limit:65536/32768 K (Java/Others)
Total Submission (s): 7559 accepted submission (s): 3315
Problem Descriptionthere is going to be a party to celebrate the 80-th anniversary of the Ural state University. The University has a hierarchical structure of employees. It means the supervisor relation forms a tree rooted at the Rector v. E. Tretyakov. In order to make the party funny for every one, the rector does not want both a employee and his or her immediate supervi Sor to is present. The personnel office has evaluated conviviality of all employee, so everyone had some number (rating) attached to him or Her. Your task is to make a list of guests with the maximal possible sum of guests ' conviviality ratings.
Inputemployees is numbered from 1 to N. A first line of input contains a number n. 1 <= n <= 6 000. 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 the go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the k-th employee was an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Outputoutput should contain the maximal sum of guests ' ratings.
Sample Input7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample OUTPUT5
Sourceural State University Internal Contest October ' Students Session
Recommendlinle | We have carefully selected several similar problems for you:1561 1011 2196 1494 2242
hdu1520: Tree-shaped DP