大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4310 Accepted Submission(s): 1976
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
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int maxn=6005;
vector<int> mq[maxn];
int dp[maxn][2];
void dfs(int cur,int p)
{
int i,nex;
for(i=0;i<mq[cur].size();i++)
{
nex=mq[cur][i];
if(nex==p) continue;
dfs(nex,cur); //先找从cur这个点能够扩展的dp
dp[cur][0]+=max(dp[nex][0],dp[nex][1]);
dp[cur][1]+=dp[nex][0];
}
}
int main()
{
int n,i;
int a,b;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
mq[i].clear();
scanf("%d",&dp[i][1]);
}
while(scanf("%d%d",&a,&b)&&(a+b))
{
mq[a].push_back(b);
mq[b].push_back(a);
}
dfs(1,0);
int ans=max(dp[1][0],dp[1][1]);
printf("%d\n",ans);
}
return 0;
}
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118610.html原文链接:https://javaforall.net
