洛谷 P1352 没有上司的舞会【树形DP/邻接链表+链式前向星】
题目描述
某大学有N个职员,编号为1~N。他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但是呢,如果某个职员的上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了。所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数。
输入输出格式
输入格式:
第一行一个整数N。(1<=N<=6000)
接下来N行,第i+1行表示i号职员的快乐指数Ri。(-128<=Ri<=127)
接下来N-1行,每行输入一对整数L,K。表示K是L的直接上司。
最后一行输入0 0
输出格式:
输出最大的快乐指数。
【分析】:
那么考虑树形DP的状态设计,一般与子树有关,即求出子树的答案然后合并上去
考虑在合并的时候,会影响答案合并的状态,就是子树的根节点选或者不选了,选了的话当前节点就不能选,两者的答案不一样当然要分开统计
于是得出状态设计 : f(i,0/1)
表示第i个子树内,i这个点选或者不选的最大值
f(i,0) = sigma max(f(v,0),f(v,1)) + ai,这是由于只要根不选,子树选不选都可以的缘故
f(i,1) = sigma f(v,0) ,这是由于根选了,所有子树的根都不能选的缘故
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int mod = 10056;
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int h[maxn];
int v[maxn];
int n;
vector<int> son[maxn];
int f[maxn][2];
void dfs(int x)
{
    f[x][0]=0;
    f[x][1]=h[x];
    for(int i=0;i<son[x].size();i++)
    {
        int y=son[x][i];
        dfs(y);
        f[x][0]+=max(f[y][0],f[y][1]);
        f[x][1]+=f[y][0];
    }
}
int main()
{
   cin>>n;
   rep(i,1,n) cin>>h[i];
   rep(i,1,n-1)
   {
       int x,y;
       cin>>x>>y;
       son[y].push_back(x);
       v[x]=1;
   }
   int root;
   rep(i,1,n)
   {
       if(!v[i])
       {
           root=i;
           break;
       }
   }
   dfs(root);
   cout<<max(f[root][0],f[root][1])<<endl;
}
HDU 1520 【链式前向星版本】
链接
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 100010 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int mod = 10056;
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int h[maxn];
int v[maxn];
int n;
int f[maxn][2];
int head[maxn];
struct node
{
    int to,nxt;
}e[maxn*2];
int tot=0;
void add(int u,int v)
{
    tot++;
    e[tot].to=v;
    e[tot].nxt=head[u];
    head[u]= tot;
}
void init()
{
    tot=0;
    ms(head,-1);
    ms(f,0);
    ms(v,0);
}
void dfs(int u,int fa)
{
    //f[u][0]=0;
    f[u][1]=h[u];
    for(int i=head[u]; i!=-1; i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa) continue;
        dfs(v,u);
        f[u][0] += max(f[v][0],f[v][1]);
        f[u][1] += f[v][0];
    }
}
int main()
{
   while(cin>>n)
   {
       init();
       rep(i,1,n) cin>>h[i];
       rep(i,1,n-1)
       {
           int x,y;
           cin>>x>>y;
           add(x,y), add(y,x);
           v[x]++;
       }
       scanf("\n0 0");
       int root;
       rep(i,1,n)
           if(!v[i])
           {
               root=i;
               break;
           }
       dfs(root,root);
       cout<<max(f[root][0],f[root][1])<<endl;
   }
}
/*
Sample Input
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
5
*/
												
											洛谷 P1352 没有上司的舞会【树形DP/邻接链表+链式前向星】的更多相关文章
- 洛谷P1352 没有上司的舞会——树形DP
		
第一次自己写树形DP的题,发个博客纪念`- 题目来源:P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结 ...
 - 洛谷 P1352 没有上司的舞会  树形DP板子
		
luogu传送门 题目描述: 某大学有n个职员,编号为1~n. 他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司. 现在有个周年庆宴会,宴会每邀请来一个职员都会 ...
 - 洛谷 P1352 没有上司的舞会(树形 DP)
		
题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...
 - 洛谷P1352没有上司的舞会+树形二维DP
		
传送门 题意:上司和直接下属,不能同时去一个聚会,问可邀请到的人的快乐值最大是多少: 参考:https://www.luogu.org/blog/mak2333/solution-p1352 思路: ...
 - 洛谷 p1352 没有上司的舞会 题解
		
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员 ...
 - 洛谷P1352 没有上司的舞会 [2017年5月计划 清北学堂51精英班Day3]
		
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子 结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职 ...
 - P1352 没有上司的舞会——树形DP入门
		
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员 ...
 - 洛谷 P1352 没有上司的舞会【树形DP】(经典)
		
<题目链接> <转载于>>> > 题目描述: 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的 ...
 - 洛谷P1352 没有上司的舞会
		
题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...
 
随机推荐
- 利用枚举算法实现todoList:把对应项添加的内容列表
			
功能: 点击城市列表项,如果内容列表不存在,则插入点击项: 如果内容列表中已存在,则不插入,然后把内容列表中的对应项放到第一位. HTML代码: <!DOCTYPE html> <h ...
 - IntentServicce;Looper;long-running task
			
7. If you want to carry on a long-running task, what do you need to do? IntentService:Service Servic ...
 - POJ 2431    Expedition      (优先队列+贪心)
			
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
 - MongoDB安装配置及使用
			
1.安装配置:https://www.cnblogs.com/ymwangel/p/5859453.html 2.使用 from pymongo import MongoClient #连接 conn ...
 - bzoj 2809 左偏树\平衡树启发式合并
			
首先我们对于一颗树,要选取最多的节点使得代价和不超过m,那么我们可以对于每一个节点维护一个平衡树,平衡树维护代价以及代价的和,那么我们可以在logn的时间内求出这个子树最多选取的节点数,然后对于一个节 ...
 - 静态资源(JS/CSS)存储在localStorage
			
一.简单了解SEO SEO由英文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”.SEO是指从自然搜索结果获得网站流量的技术和过程. 搜索引擎不优化的网站分为 ...
 - 使用Sysmon分析宏病毒(Macros Downloader)
			
样本为一个Word文件,Virustotal地址: https://www.virustotal.com/#/file/f8aede78ad92bd28f5f699b677d7d5fd362c8be8 ...
 - /proc/diskstats文件注解
			
/proc/diskstats 注解 今儿在准备利用shell监控磁盘读写次数等信息时,看到该文件,但是又不清楚每段的具体含义,这里备注下. 文件内容 [root@namenode proc]# ca ...
 - Timer类
			
构造方法:public Timer() 创建一个新计时器.相关的线程不 作为守护程序运行. public Timer(boolean isDaemon) 创建一个新计时器,可以指定其相关的线程作为守护 ...
 - python多进程处理数据
			
当我们处理大规模数据如ImageNet的时候,单进程显得很吃力耗时,且不能充分利用多核CPU计算机的资源.因此需要使用多进程对数据进行并行处理,然后将结果合并即可.以下给出的是多进程处理的demo代码 ...