被坑。。。hdu都是多组数据读入的
----------------------------------------------------------------------------------------------
#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 Description
There 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 that 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 an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has 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.
Input
Employees are 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 that 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 is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Output
Output should contain the maximal sum of guests‘ ratings.
Sample Input
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample Output
5
Source
Ural State University Internal Contest October‘2000 Students Session
Recommend
linle | We have carefully selected several similar problems for you: 1561 1011 2196 1494 2242