POJ 2342 树状dp

Anniversary party

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4606   Accepted: 2615

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 N – 1 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

POJ 2342

题目意思:

求一棵树的最小点权覆盖;

解题思路:

树状dp,状态转移方程为:

dp[i][0]=max(dp[son1][0],dp[son1][1])+max(dp[son2][0],dp[son2][1])+...max(dp[sonm][0],dp[sonm][1]);

dp[i][1]=sum(dp[son][0]);

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdio>
 6 using namespace std;
 7 const int maxn=6007;
 8 bool visit[maxn];
 9 int value[maxn];
10 int head[maxn];
11 int dp[maxn][2];
12 int sum=0;
13 int n;
14 struct node
15 {
16     int to,next;
17 };
18 node tree[maxn];
19 void init()
20 {
21     memset(head,-1,sizeof(head));
22     memset(visit,0,sizeof(visit));
23     memset(dp,0,sizeof(dp));
24     memset(value,0,sizeof(value));
25     sum=0;
26 }
27 void AddEdge(int u,int v)
28 {
29         tree[sum].to=v;
30         tree[sum].next=head[u];head[u]=sum;
31         sum++;
32 }
33 void dfs_tree(int point)
34 {
35     int res0=0,res1=0;
36     if(visit[point]) return;
37     visit[point]=1;
38     for(int i=head[point];i!=-1;i=tree[i].next)
39     {
40         if(!visit[tree[i].to]) dfs_tree(tree[i].to);
41         res1+=dp[tree[i].to][0];
42         res0+=max(dp[tree[i].to][0],dp[tree[i].to][1]);
43     }
44     dp[point][0]=res0;
45     dp[point][1]=res1+value[point];
46 }
47 int main()
48 {
49     // freopen("in.txt","r",stdin);
50     while(~scanf("%d",&n)){
51         init();
52         for(int i=1;i<=n;i++)
53         {
54             scanf("%d",&value[i]);
55         }
56         int a,b;
57         while(1)
58         {
59             scanf("%d %d",&a,&b);
60             if(a==0&&b==0) break;
61             AddEdge(b,a);
62             visit[a]=1;
63         }
64         int root;
65         for(int i=1;i<=n;i++)
66         {
67             if(visit[i]==0) root=i;
68         }
69         memset(visit,0,sizeof(visit));
70         dfs_tree(root);
71         int ans=max(dp[root][0],dp[root][1]);
72         printf("%d\n",ans);
73     }
74     return 0;
75 }
 
时间: 2024-09-30 18:44:22

POJ 2342 树状dp的相关文章

POJ 1155 树状dp

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3856   Accepted: 2054 Description A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tre

POJ 1463 树状dp

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6629   Accepted: 3058 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

POJ 2378 树状dp

Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3849   Accepted: 2304 Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible

树状DP (poj 2342)

题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[i][0] += max(F[k][1], F[k][0]);(k为i的儿子) 第二种情况:第i个员工参与,F[i][1] += F[k][0]; F[i][j]表示第i个员工是否参与: 边界:F[i][0] = 0:F[i][1] = 其快乐指数: #include <iostream> #in

洛谷P1122 最大子树和 (树状dp)

题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明就向老师提出了这个问题: 一株奇怪的花卉,上面共连有N 朵花,共有N-1条枝干将花儿连在一起,并且未修剪时每朵花都不是孤立的.每朵花都有一个“美丽指数”,该数越大说明这朵花越漂亮,也有“美丽指数”为负 数的,说明这朵花看着都让人恶心.所谓“修剪”,意为:去掉其中的一条枝条,这样一株花就成了两株,扔掉

POJ 2342 (树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 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

poj1947--Rebuilding Roads(树状dp)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9496   Accepted: 4316 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The

洛谷P2015 二叉苹果树(树状dp)

题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.下面是一颗有4个树枝的树 2 5 \ / 3 4 \ / 1 现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果. 输入输出格式 输入格式: 第1行2个数,N和Q(1<=Q<= N,1<N<=100). N表示树

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include