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处理。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 600000
#define maxn 10050
#define l first
#define r second
int v[maxn];
vector<int>e[maxn]; //存储边
bitset<105>flag[maxn][105]; //flag[i][j]表示第i个节点左端点为j,右端点的方案数
vector<int>p[105]; //p[i]表示该节点子树中左端点为i的右端点们
vector<pair<int,int> >s[maxn]; //s[i]表示节点为i的符合条件的区间们 void dfs(int u)
{
int i,j,k,lo,re;
for(i=0;i<e[u].size();i++){
dfs(e[u][i]);
} for(i=1;i<=100;i++)p[i].clear(); for(i=0;i<e[u].size();i++){
int v=e[u][i];
for(j=0;j<s[v].size();j++){
p[s[v][j].l ].push_back(s[v][j].r);
}
}
for(lo=100;lo>=1;lo--){
if(lo==v[u]){
flag[u][lo]|=flag[u][lo+1];
flag[u][lo].set(lo);
}
else{
for(i=0;i<p[lo].size();i++){
re=p[lo][i];
if(lo>v[u] || re<v[u]){
flag[u][lo]|=flag[u][re+1];
flag[u][lo].set(re);
}
}
}
for(re=lo;re<=100;re++){
if(flag[u][lo].test(re)==1 && lo<=v[u] && re>=v[u]){
s[u].push_back(make_pair(lo,re) );
}
}
}
}
int main()
{
int n,m,i,j,c,d;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d",&v[i]);
}
for(i=1;i<=n-1;i++){
scanf("%d%d",&c,&d);
e[c].push_back(d);
}
dfs(1);
printf("%d\n",s[1].size());
}
return 0;
}

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. C++题目东华

    1. 定义一个点类Point,其有两个double型的私有数据成员x和y.此外还包含以下公有成员函数: (1)构造函数,给点初始化: (2)setPoint函数,设置点坐标值: (3)distance ...

  2. 【C++】《Effective C++》第二章

    第二章 构造/析构/赋值运算 条款05:了解C++默默编写并调用哪些函数 默认函数 一般情况下,编译器会为类默认合成以下函数:default构造函数.copy构造函数.non-virtual析构函数. ...

  3. 剑指 Offer 16. 数值的整数次方

    实现函数double Power(double base, int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题. 来源:力扣(LeetCode) 链接 ...

  4. python学习笔记 | selenium各浏览器驱动下载地址

    Chrome http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的chromedriver.exe 版本也不一样, ...

  5. 【Spring】XML方式实现(无参构造 有参构造)和注解方式实现 IoC

    文章目录 Spring IoC的实现方式 XML方式实现 通过无参构造方法来创建 1.编写一个User实体类 2.编写我们的spring文件 3.测试类 UserTest.java 4.测试结果 通过 ...

  6. 使用memory_profiler异常

    在使用memory_profiler模块0.55.0版本执行命令诊断程序内存用量时,遇到下面错误: C:\Users\Chen\Desktop\python_doc\第四模块课件>python ...

  7. Vue MVVM模型原理

    最近反思了下自己,觉得自己很急躁,学技术总是觉得能用就行了,其实这样很不好,总是这样,就永远只能当用轮子的人.好了,废话不多说,转入正题: 要理解MVVM的原理,首先要理解它是什么,怎么运作起来的: ...

  8. 同步alv的前端显示和输出内表中的数据

    在使用CL_GUI_ALV_GRID显示报表的时候,当我们使用了checkbox的时候,或者是有可编辑的字段,当我们 在前段修改了单元格内容的时候,后台的内表并不会自动的更新,此时需要我们调用一个方法 ...

  9. 入门OJ:售货员的难题

    题目描述 某乡有n个村庄(1<n<15),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且A村到B村与B村到A村的路大多不同.为了提高效率 ...

  10. Http中的options请求

    引自:https://www.jianshu.com/p/5cf82f092201.https://www.cnblogs.com/mamimi/p/10602722.html 一.options是什 ...