zjnu1709 UZASTOPNI (bitset,树形dp)
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)的更多相关文章
- HDU-4661 Message Passing 树形DP,排列组合
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4661 题意:有n个人呈树状结构,每个人知道一个独特的消息.每次可以让一个人将他所知的所有消息告诉和他相 ...
- BNUOJ-26482 Juice 树形DP
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26482 题意:给一颗树,根节点为送电站,可以无穷送电,其它节点为house,电量达到pi时 ...
- HDU-4679 Terrorist’s destroy 树形DP,维护
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给一颗树,每个边有一个权值,要你去掉一条边权值w剩下的两颗子树中分别的最长链a,b,使得w ...
- HDU-4616 Game 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4616 比较典型的树形DP题目,f[u][j][k]表示以点u为子树,经过 j 个陷阱的最大值,其中k= ...
- BZOJ5341[Ctsc2018]暴力写挂——边分治+虚树+树形DP
题目链接: CSTC2018暴力写挂 题目大意:给出n个点结构不同的两棵树,边有边权(有负权边及0边),要求找到一个点对(a,b)满足dep(a)+dep(b)-dep(lca)-dep'(lca)最 ...
- [WC2018]通道——边分治+虚树+树形DP
题目链接: [WC2018]通道 题目大意:给出三棵n个节点结构不同的树,边有边权,要求找出一个点对(a,b)使三棵树上这两点的路径权值和最大,一条路径权值为路径上所有边的边权和. 我们按照部分分逐个 ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
随机推荐
- 如何在 Vite 中使用 Element UI + Vue 3
在上篇文章<2021新年 Vue3.0 + Element UI 尝鲜小记>里,我们尝试使用了 Vue CLI 创建 Vue 3 + Element UI 的项目,而 Vue CLI 实际 ...
- SonarQube学习(六)- SonarQube之扫描报告解析
登录http://192.16.1.105:9000,加载项目扫描情况 点击项目名称,查看报告总览 开发人员主要关注为[问题]标签页. 类型 主要关注为bug和漏洞. 其中bug是必须要修复的,漏洞是 ...
- grep和egrep
grep nobody /etc/passwd 显示/etc/passwd中带有nobody字样的行,区分大小写 grep -i nobody /etc/passwd 现实/etc/passwd中 ...
- me21n增强BADI:ME_PROCESS_PO_CUST之process_account
当实施ME_PROCESS_PO_CUST这个badi来增强ME21N的时候,用了到方法process_account,既对ME21N的行项目的科目分配做增强.主要用到如下类: IF_PURCHASE ...
- 使用Intelij 运行Android 程序导致的无法安装
前几天的时候更换了开发工具开发Android ,终于不用忍受Android studio 的各种卡顿了.我决定使用一段时间Intelij 开发Android. 之前的程序代码在运行的时候也出现了异常, ...
- SQLSERVER 修改数据实例的排序规则
SQL Server服务器修改排序规则的方法 操作及验证步骤: 1 登录数据库后,查看当前安装数据库默认排序规则的两种方式 方式一.使用SQL Server 2014 Management Studi ...
- Scrapy———反爬蟲的一些基本應對方法
1. IP地址驗證 背景:有些網站會使用IP地址驗證進行反爬蟲處理,檢查客戶端的IP地址,若同一個IP地址頻繁訪問,則會判斷該客戶端是爬蟲程序. 解決方案: 1. 讓Scrapy不斷隨機更換代理服務器 ...
- 关闭(隐藏)VS2019控制台上文件路径的显示
昨天有个朋友问我,怎么关闭在运行程序后,控制台上显示的文件路径啊?啥??我突然不知道他说的说什么,然后我就自己随便打了几行运行了一下,才知道原来他说的是这个: 一开始我也没在意,我就告诉他,这个无所谓 ...
- 反射型XSS
反射型XSS漏洞详解 http://www.ttlsa.com/safe/xss-description/ 一.原理 如果一个应用程序使用动态页面向用户显示错误消息,就会造成一种常见的XSS漏洞.通常 ...
- HMS Core华为分析丨受众细分,多场景促进精益运营
用户的偏好不同,对产品的需求也不一样,要想更好地培养用户粘性,就需要因人施策,精细化运营,而受众细分是精细化运营的重要方法之一.受众细分是根据用户属性和行为数据,将具有相同或类似特征的用户归为一个群组 ...