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. python安装whl包时出现的问题解决:is not a supported wheel on this platform

    @ 目录 一.问题 二.查找问题 三.问题解决 一.问题 1.下载一个twisted包 安装Twisted,进入https://www.lfd.uci.edu/~gohlke/pythonlibs 下 ...

  2. Ossec 安装并配置邮件通知

    Ossec 安装并配置邮件通知 目录 Ossec 安装并配置邮件通知 1. 介绍 2. 软硬件环境 3. 安装步骤 3.1 Server 3.2 Agent 3.3 配置邮件通知 4. 参考资料 1. ...

  3. python学习笔记 | PyCharm出现卡顿解决方法

    问题:使用pycharm时常出现 the IDE is running low on memory 的问题 表示pycharm这款IDE使用内存不足 需要在系统内存充足的情况下扩充IDE memory ...

  4. 【ORACLE错误】SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled

    执行set autotrace traceonly的时候,报错 SQL> set autotrace traceonly SP2-0618: Cannot find the Session Id ...

  5. MCU的心脏-晶振

    晶振是石英晶体谐振器(quartzcrystal oscillator)的简称,它被称为电路系统的心脏,它为整个系统提供"心跳".中央处理器(CPU)一切指令的执行都是建立在这个& ...

  6. oracle新增ID主键列,如何补全旧数据的ID值

    1.创建SEQUENCE CREATE SEQUENCE MONKEY.TEST_ADD_IDCOL_ID CACHE 100; 2.新增表栏位 ALTER TABLE MONKEY.TEST_ADD ...

  7. ABAP 多表联合查询

    inner join(等值连接) 只返回两个表中联结字段相等的行left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) 返回包括右表中的所有记录 ...

  8. 入门OJ:Coin

    题目描述 你有n个硬币,第i硬币面值为ai,现在总队长想知道如果丢掉了某个硬币,剩下的硬币能组成多少种价值?(0价值不算) 输入格式 第一行一个整数n 第二行n个整数.,a1,a2-an. 1< ...

  9. H3C、Huawei、Cisco网络设备AAA TACACS认证配置

    TACACS技术白皮书 摘要:TACACS是实现AAA功能的一种安全协议,主要是通过TACACS客户端与TACACS服务器通信来实现多种用户的AAA功能. HWTACACS采用TCP协议承载报文,TC ...

  10. 抓包一张tcpdump小抄就够了

    作者简介 李先生(Lemon),高级运维工程师(自称),SRE专家(目标),梦想在35岁买一辆保时捷.喜欢钻研底层技术,认为底层基础才是王道.一切新技术都离不开操作系统(CPU.内存.磁盘).网络等. ...