Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29105 Accepted Submission(s): 9932
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
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
Recommend
linle
/*
*@Author: GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ull unsigned long long
#define scf(n) scanf("%d",&n)
#define scfl(n) scanf("%lld",&n)
#define prf(n) printf("%d",n)
#define prfl(n) printf("%lld",n)
#define scfd(n) scanf("%lf",&n)
#define prfd(n) printf("%.lf",n)
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
inline double max(double a,double b){
return a>b?a:b;
}
inline double min(double a,double b){
return a<b?a:b;
}
inline int read() {
int x=0,f=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//fastPow
int fastPow(int x,int n){
if(n==1) return x;
int tmp=fastPow(x,n/2); //fenzi
if(n%2==1) return tmp*tmp*x;
else return tmp*tmp;
}
ll gcd(ll m, ll n){return n == 0 ? m : gcd(n, m%n);}
ll lcm(ll m, ll n){return m*n / gcd(m, n);}
ll pows(ll base, ll power,ll mod){ll result=1;while(power>0){if(power&1){result=result*base%mod;}power>>=1;base=(base*base)%mod;}return result;}
ll poww(ll base, ll power){ll result=1;while(power>0){if(power&1){result=result*base;}power>>=1;base=(base*base);}return result;}
//dijsktra
//start
//const int MAX= 10020;
//struct node {
// int x,to,next,w;
// bool operator <(const node &a) const{
// return this->w>a.w;
// }
//}G[MAX<<1];
//int cnt;
//int dis[MAX];
//int vis[MAX];
//int head[MAX];
//
//
//int m,n;
//int b[10020];
//
//
//void add(int u,int v,int w){
// G[++cnt].to=v;
// G[cnt].w=w;
// G[cnt].next=head[u];
// head[u]=cnt;
//}
//int dijkstra(int s){
// mms(dis,INF);
// dis[s]=0;
// priority_queue<node> p;
// node t;
// t.x=s;
// t.w=0;
// p.push(t);
// while (!p.empty()) {
// node u=p.top();
// p.pop();
// int v=u.x;
// if(dis[v]!=u.w) continue;
// for(int i=head[v];i;i=G[i].next){
// int to=G[i].to;
// if(dis[to]>dis[v]+G[i].w){
// dis[to]=dis[v]+G[i].w;
// t.x=to;
// t.w=dis[to];
// p.push(t);
// }
//
// }
// }
// int m1=-1;
// int m2=-1;
// dis[s]=-3;
// for(int i=1;i<=n;i++){
// if(dis[i]==INF) continue;
// if(dis[i]>m1){
// m2=m1;
// m1=dis[i];
// }
// else if(dis[i]>m2){
// m2=dis[i];
// }
// }
// if(m1!=-1&&m2!=-1) return m1+m2;
// return -1;
//
//}
//
//
//int main(){
// int t;
// cin>>t;
// while (t--) {
// cin>>n>>m;
// int u,v,w;
//
// for(int i=1;i<=m;i++){
// cin>>u>>v>>w;
// add(u,v,w);
// add(v,u,w);
// }
// int ans=0;
// int cnt=0;
// for(int i=1;i<=n;i++){
// ans=max(ans,dijkstra(i));
// }
// cout<<ans<<endl;
// }
//}
//end
//e_cheak
//int vis[1000010];
//int prime[100010];
//int e_cheak(int n){ //k为2-n素数的个数
// for(int i=0;i<=n;i++){
// vis[i]=0;
// }
// for(int i=2;i*i<=n;i++){
// if(!vis[i]){
// for(int j=i*i;j<=n;j+=i){
// vis[j]=1;
// }
// }
// }
// int k=0;
// for(int i=2;i<=n;i++){
// if(!vis[i])
// prime[k++]=i; //统计素数
// }
// return k;
//}
//
进制转化 x转化为y进制
//string work(int x,int y){
// string str="";
// while(x){
// if(x%y>=10) str+=x%y+'A'-10;
// else str+=x%y+'0';
// x/=y;
// }
// reverse(str.begin(),str.end());
// return str;
//}
//
//
//const int MAX=1000005;
//char str[MAX];
//char pattern[MAX];
//int cnt;
//int Next[MAX];
//void getnext(string p,int plen){
// Next[0]=0;
// Next[1]=0;
// for(int i=1;i<plen;i++){
// int j=Next[i];
// while (j&&p[i]!=p[j]) {
// j=Next[j];
// }
// Next[i+1]=(p[i]==p[j])?j+1:0;
// }
//}
//int kmp(string s,string p){
// int last=-1;
// int slen=s.length();
// int plen=p.length();
// getnext(p,plen);
// int j=0;
// for(int i=0;i<slen;i++){
// while (j&&s[i]!=p[j]) {
// j=Next[j];
// }
// if(s[i]==p[j]) j++;
// if(j==plen){
// //start
// return 1;
// //end
// }
// }
// return 0;
//}
//int main(){
// int n;
// cin>>n;
// string t;
// string s="";
// for(int i=2;i<=16;i++){
// s="";
// s+=work(n,i);
// if(kmp(s,t))
// {
// cout<<"yes";
// return 0;
// }
// }
// cout<<"no"<<endl;
// return 0;
//}
//string s1,s2;
//int main(){
// cin>>s1;
// s2="cocacola";
// int ans;
// for(int i=0;i<s1.length();i++){
// if(s1[i]!=s2[i])
// ans++;
// }
// cout<<ceil(ans/2);
//}
int n;
int value[10010];
int father[1010];
vector<int>tree[1010];
int dp[1010][2];
void dfs(int s){
dp[s][0]=0;
dp[s][1]=value[s];
for(int i=0;i<tree[s].size();i++){
int son=tree[s][i];
dfs(son);
dp[s][1]+=dp[son][0];
dp[s][0]+=max(dp[son][0],dp[son][1]);
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) {
cin>>value[i];
father[i]=-1;
}
int a,b;
while (cin>>a>>b&&(a!=0)&&(b!=0)) {
father[a]=b;//父子关系
tree[b].push_back(a); //用邻接表来建树
}
int t=1;
while (father[t]!=-1) {
t=father[t]; //找根节点
}
dfs(t);
cout<<max(dp[t][0],dp[t][1]); //从根开始搜索
}