Description

Petar is throwing a birthday party and he decided to invite some of the employees of his company where he is the CEO. Each employee, including Petar, has a unique label from 1 to N, and an accompanying type of jokes they tell Vi . Also, each employee of the
company except Petar has exactly one supervisor. Since Petar is the CEO of the company, he has the label 1 and is directly or indirectly superordinate to all the employees. At the birthday party, there are certain rules that all people present (including Petar)
must follow.

• At the party, there shouldn’t be two people that tell the same type of jokes.

• Person X cannot be invited if their direct supervisor is not invited.

• Person X cannot be invited if the set of jokes the invitees that person X is superior to (directly or indirectly) tell and person X don’t form a set of consecutive numbers.

The numbers in the set are consecutive if the difference between adjacent elements is exactly 1 when the set is sorted ascendingly. For example, (3, 1, 2) and (5, 1, 2, 4, 3). Petar wants to know how many different sets of jokes he can see at his party with
the listed constraints.

Input

The first line of input contains the integer N, (1 ≤ N ≤ 10 000). The second line of input contains N integers, the types of jokes person i tells, Vi, (1 ≤ Vi ≤ 100). Each of the following N-1 lines contains two integers A and B, (1 ≤ A, B ≤ N), denoting that
person A is directly superior to person B.

Output

The first and only line of output must contain the number of different sets of jokes that comply to the previously listed constraints.

Sample Input

4

2 1 3 4

1 2

1 3

3 4

4

3 4 5 6

1 2

1 3

2 4

6

5 3 6 4 2 1

1 2

1 3

1 4

2 5

5 6 

Sample Output

6

3

10

题意:n个点形成一棵树,每一个点有自己的价值,让你在满足这3个条件的前提下找到总的方案数:1.一个节点的父亲节点没有选择时,该节点不能选择.2.选择的点构成的集合中不能存在相同价值的两个点.3.选择的任何一棵子树(包含本身)价值所构成的集合一定是连续的.

思路:可以递归求解,我们假设一个节点的子树都处理完了,那么先把子树中所有得到的可行的区间都记录下来,然后枚举所有左端点(1~100),然后找出可行的右端点,可以用bitset处理。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<vector>
  6. #include<map>
  7. #include<set>
  8. #include<queue>
  9. #include<stack>
  10. #include<string>
  11. #include<bitset>
  12. #include<algorithm>
  13. using namespace std;
  14. typedef long long ll;
  15. #define inf 600000
  16. #define maxn 10050
  17. #define l first
  18. #define r second
  19. int v[maxn];
  20. vector<int>e[maxn]; //存储边
  21. bitset<105>flag[maxn][105]; //flag[i][j]表示第i个节点左端点为j,右端点的方案数
  22. vector<int>p[105]; //p[i]表示该节点子树中左端点为i的右端点们
  23. vector<pair<int,int> >s[maxn]; //s[i]表示节点为i的符合条件的区间们
  24. void dfs(int u)
  25. {
  26. int i,j,k,lo,re;
  27. for(i=0;i<e[u].size();i++){
  28. dfs(e[u][i]);
  29. }
  30. for(i=1;i<=100;i++)p[i].clear();
  31. for(i=0;i<e[u].size();i++){
  32. int v=e[u][i];
  33. for(j=0;j<s[v].size();j++){
  34. p[s[v][j].l ].push_back(s[v][j].r);
  35. }
  36. }
  37. for(lo=100;lo>=1;lo--){
  38. if(lo==v[u]){
  39. flag[u][lo]|=flag[u][lo+1];
  40. flag[u][lo].set(lo);
  41. }
  42. else{
  43. for(i=0;i<p[lo].size();i++){
  44. re=p[lo][i];
  45. if(lo>v[u] || re<v[u]){
  46. flag[u][lo]|=flag[u][re+1];
  47. flag[u][lo].set(re);
  48. }
  49. }
  50. }
  51. for(re=lo;re<=100;re++){
  52. if(flag[u][lo].test(re)==1 && lo<=v[u] && re>=v[u]){
  53. s[u].push_back(make_pair(lo,re) );
  54. }
  55. }
  56. }
  57. }
  58. int main()
  59. {
  60. int n,m,i,j,c,d;
  61. while(scanf("%d",&n)!=EOF)
  62. {
  63. for(i=1;i<=n;i++){
  64. scanf("%d",&v[i]);
  65. }
  66. for(i=1;i<=n-1;i++){
  67. scanf("%d%d",&c,&d);
  68. e[c].push_back(d);
  69. }
  70. dfs(1);
  71. printf("%d\n",s[1].size());
  72. }
  73. return 0;
  74. }

zjnu1709 UZASTOPNI (bitset,树形dp)的更多相关文章

  1. HDU-4661 Message Passing 树形DP,排列组合

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4661 题意:有n个人呈树状结构,每个人知道一个独特的消息.每次可以让一个人将他所知的所有消息告诉和他相 ...

  2. BNUOJ-26482 Juice 树形DP

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26482 题意:给一颗树,根节点为送电站,可以无穷送电,其它节点为house,电量达到pi时 ...

  3. HDU-4679 Terrorist’s destroy 树形DP,维护

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给一颗树,每个边有一个权值,要你去掉一条边权值w剩下的两颗子树中分别的最长链a,b,使得w ...

  4. HDU-4616 Game 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4616 比较典型的树形DP题目,f[u][j][k]表示以点u为子树,经过 j 个陷阱的最大值,其中k= ...

  5. BZOJ5341[Ctsc2018]暴力写挂——边分治+虚树+树形DP

    题目链接: CSTC2018暴力写挂 题目大意:给出n个点结构不同的两棵树,边有边权(有负权边及0边),要求找到一个点对(a,b)满足dep(a)+dep(b)-dep(lca)-dep'(lca)最 ...

  6. [WC2018]通道——边分治+虚树+树形DP

    题目链接: [WC2018]通道 题目大意:给出三棵n个节点结构不同的树,边有边权,要求找出一个点对(a,b)使三棵树上这两点的路径权值和最大,一条路径权值为路径上所有边的边权和. 我们按照部分分逐个 ...

  7. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  8. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价

    Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/ ...

  9. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

随机推荐

  1. (十)Python装饰器

    装饰器:本质就是函数,功能是为其他函数添加附加功能. 两个原则: 1.不修改被修饰函数的源代码 2.不修改被修饰函数的调用方式 一个栗子 def test(): res = 0 for i in ra ...

  2. ORA-00245 control file backup operation failed 分析和解决

    一.问题说明 操作系统: RedHat 5.8 数据库: 11.2.0.3 2节点RAC. 使用RMAN 备份的时候,报如下错误: ORA-00245: control file backup fai ...

  3. 02_Python基础

    2.1 第一条编程语句 print("Hello, Python!") print("To be, or not to be, it's a question." ...

  4. Spring Security 实战干货:分布式对象SharedObject

    1. 前言 在上一篇我们对AuthenticationManager的初始化的细节进行了分析,其中里面有一段代码引起了不少同学的注意: ApplicationContext context = htt ...

  5. zabbix客户端监控脚本shell

    zabbix客户端监控脚本shell #!/bin/sh sleep 3 zabbixdir=`pwd` zabbix_version=4.2.5 ###指定版本,最好和server端吻合版本,可以自 ...

  6. EasyExcel基本使用

    EasyExcel基本使用 一.应用场景 1.数据导入:减轻录入工作量 2.数据导出:统计信息归档 3.数据传输:异构系统之间数据传输 二.简介 Java领域解析.生成Excel比较有名的框架有Apa ...

  7. Java并发组件二之CyclicBarriar

    使用场景: 多个线程相互等待,直到都满足条件之后,才能执行后续的操作.CyclicBarrier描述的是各个线程之间相互等待的关系. 使用步骤: 正常实例化:CyclicBarrier sCyclic ...

  8. 1.kafka基础架构

    kafka基础架构 ## 什么是kafka? Kafka是一个分布式的基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域. 1.什么是消息队列? 2.使用消息队列的好处 1)解耦 允许你独立的 ...

  9. (Oracle)看懂Oracle执行计划(转载)

    最近一直在跟Oracle打交道,从最初的一脸懵逼到现在的略有所知,也来总结一下自己最近所学,不定时更新ing- 一:什么是Oracle执行计划? 执行计划是一条查询语句在Oracle中的执行过程或访问 ...

  10. Linux 技巧:让进程在后台运行更可靠的几种方法

    Linux 技巧:让进程在后台运行更可靠的几种方法 https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/index.html 我们经常会碰到这 ...