大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Anniversaryparty树形dp-创新互联

简单的树形dpAnniversaryparty
树形dp

dp[i] 表示以i为根的包括i 的大欢乐度

专业从事成都网站建设、做网站,高端网站制作设计,微信小程序,网站推广的成都做网站的公司。优秀技术团队竭力真诚服务,采用H5页面制作+CSS3前端渲染技术,响应式网站设计,让网站在手机、平板、PC、微信下都能呈现。建站过程建立专项小组,与您实时在线互动,随时提供解决方案,畅聊想法和感受。

f[i] 表示以i为根不包括i 的大欢乐度

叶节点则为 f[i] = 0, dp[i] = w[i]  只要初始化 dp f 都全为0 就可以了 dfs会自动复制

说实话我觉得这题很不严谨 他说欢乐度可以为负值  按理来说如果小于0 就应该将欢乐度设为0 表示那个人不参加

最后是 无论设不设为0 都AC 证明根本没有欢乐度为负值的

要注意的是

1. 不是只有一个case  我一开始以为只有一个case  一直错 要处理到没有输入为止。。。 题目又没说明

2. 有可能有多棵树 所以要多次dfs()

View Code
Anniversary party

Time Limit :2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) :5   Accepted Submission(s) : 2
Font: Times New Roman| Verdana | Georgia
Font Size: ← →
Problem Description
Thereis 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 numberedfrom 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
711111111 32 36 47 44 53 50 0
Sample Output
5
View Code
#include 
#include
#include

const int MAXN = 6005;
struct node
{
int index ;
    node*next ;
}adj[MAXN];
bool vis[MAXN], isRoot[MAXN];
int n, m, f[MAXN], dp[MAXN], w[MAXN]; 
void addEdge(int x, int y)
{
    node*p = new node;
    p->index = y;
    p->next = adj[x].next;
    adj[x].next= p;
    isRoot[y]= 0;
}
int max(int a, int b)
{return a>=b ?a :b  ;  }
void dfs(int root)
{
    vis[root]= 1;
    node*p = adj[root].next;
while( p != NULL )
    {
int u = p->index;
if(!vis[u])
        {
            dfs(u);
//    dp[root] += f[u]<0 ?0 :f[u];
//    int tmp = max(dp[u], f[u]);
//    f[root] += tmp < 0 ? 0 : tmp ;            dp[root] += f[u];
            f[root]+= max(dp[u], f[u]);
        }
        p= p->next;
    }
    dp[root]+= w[root];
}
int main()
{
int i, a, b, root, ans = 0;
while(scanf("%d", &n)!=EOF)
    {
        ans= 0;
for(i=1; i<=n; i++)
        {
            scanf("%d", &w[i]);
            isRoot[i]= 1; dp[i] = f[i] = 0;
            adj[i].next= NULL, vis[i] = 0;
        }
while(scanf("%d %d", &a, &b), a|b)
            addEdge(b, a);
for(i=1; i<=n; i++)
if(isRoot[i])
            {
                dfs(i);
                ans+= max(dp[i], f[i]);
            }
            printf("%d
", ans);
    }
return 0;
}

文章名称:Anniversaryparty树形dp-创新互联
当前URL:http://dzwzjz.com/article/djjgco.html
在线咨询
服务热线
服务热线:028-86922220
TOP