HDU5905(树dp)

(一)ANSYS WORKBENCH实践入门 ANSYS-workbench 有限元分析应用基础教程 (张晔)学习笔记 阅读详情

Black White Tree

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 80 Accepted Submission(s): 40

Problem Description
Alex has a tree T with n vertices conveniently labeled with 1,2,…,n. Each vertex of the tree is colored black or white. For every integer pair (a,b), Alex wants to know whether there is a subtree of T with exact a white vertices and b black vertices.

A subtree of T is a subgraph of T that is also a tree.

Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤100) indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤2000). The next line contains a binary string s length n where si denoting the color of the i-th vertex (‘0’ means white, while ‘1’ means black). Each of the following n−1 lines contains two integers ai,bi, denoting an edge between vertices ai and bi (1≤ai,bi≤n).

Output
For each test case, output an integer W=∑na=0∑nb=0(a+1)(b+1)S(a,b). S(a,b)=1 if there is a subtree with exact a white vertices and b black vertices, or 0 otherwise.

Sample Input
3
4
1010
1 4
2 4
3 4
3
101
1 2
2 3
10
1010111001
1 2
1 3
1 8
2 4
2 6
2 7
3 9
4 5
7 10

Sample Output
33
15
365

题意:这是bc题,有中文题,自己去找。

解题思路:有一个结论,就是大小为i的子树中,肯定有一个里面的黑点数最大,我们设为Max,肯定有一个里面的黑点数最小,我们设为Min,那么这个结论就是这个树一定存在大小为i,黑点数介于Max与Min之间的子树,具体怎么证明,本弱鸡也不会,跪请大佬解释下怎么证明,那么知道了这个结论后就好办了,我们只需要求出大小为i的子图中Max与Min就行。具体做发看代码注释。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3 + 10;
const ll inf = 1e9;
ll Max[maxn];//Max[i]表示大小为i的连通图黑点数的最大值
ll Min[maxn];//Min[i]表示大小为i的连通图黑点数的最小值
ll dpMax[maxn][maxn];//dpMax[i][j]表示以i为根的子树大小为j的最大黑点数
ll dpMin[maxn][maxn];//dpMin[i][j]表示以i为根的子树大小为j的最小黑点数
ll dp[maxn];//dp[i]表示以i为根的子树的大小
bool visit[maxn];
int n;
char s[maxn];
vector<int> g[maxn];
void init()
{
    memset(visit,false,sizeof(visit));
    memset(dp,0,sizeof(dp));
    for(int i = 1; i <= n; i++)
    {
        g[i].clear();
        Max[i] = -inf;
        Min[i] = inf;
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            dpMax[i][j] = -inf;
            dpMin[i][j] = inf;
        }
    }

}
void dfs(int root)
{
    if(visit[root]) return;
    visit[root] = true;
    dp[root] = 1;
    for(int i = 0; i < g[root].size(); i++)
    {
        int v = g[root][i];
        if(!visit[v])
        {
            dfs(v);
            dp[root] += dp[v];
        }
    }
}
void solve(int root)
{
    if(visit[root]) return;
    dpMax[root][0] = dpMin[root][0] = 0;
    if(s[root] == '1')
    {
        dpMax[root][1] = dpMin[root][1] = 1;
    }
    else
    {
        dpMax[root][1] = dpMin[root][1] = 0;
    }
    visit[root] = true;
    int ans = 1;
    for(int i = 0; i < g[root].size(); i++)
    {
        int v = g[root][i];
        if(!visit[v])
        {
            solve(v);
            for(int j = ans; j > 0; j--)
            {
                for(int k = 1; k <= dp[v]; k++)
                {
                    dpMax[root][j + k] = max(dpMax[root][j + k],dpMax[root][j] + dpMax[v][k]);
                    dpMin[root][j + k] = min(dpMin[root][j + k],dpMin[root][j] + dpMin[v][k]);
                }
            }
            ans += dp[v];
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        scanf("%s",s + 1);
        int u,v;
        for(int i = 1; i < n; i++)
        {
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs(1);//假设以一为根
        memset(visit,false,sizeof(visit));
        solve(1);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                Max[j] = max(Max[j],dpMax[i][j]);
                Min[j] = min(Min[j],dpMin[i][j]);
            }
        }
        ll sum = 1;//a = 0,b = 0的特殊情况
        for(int i = 1; i <= n; i++)
        {
            for(int j = Max[i]; j >= Min[i]; j--)
            {
                sum += (j + 1)*(i - j + 1);
            }
        }
        printf("%lld\n",sum);

    }
    return 0;
}
WSOLA原理及matlab仿真 WSOLA原理 对于 OLA 算法来说,常常无法保持原信号中的周期性结构,在帧裁剪的过程中,不能保证每一个帧都能覆盖完整周期并保证其相位对齐,会产生相位跳跃失真。 为保证**基频不变**,就要使用 WSOLA 算法。 WSOLA 并不直接在 Ha 处找一帧进行直接拼接,而是在一个区间内寻找与理想帧最接近的一帧进行叠加。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/8a234f4fde104fc69a9f3ddb7bcf3014.png?x-oss-process=ima 阅读详情

相关推荐

HCIA重点知识

华为认证ICT工程师,是华为认证体系中用于标识个人能力在某一技术领域达到工程师级别的证明,表示通过认证的人员具备安装、配置、运行 ICT 设备,并进行基础故障排除的能力。抽象语言 -----> 编码 -----> 二进制 -----> 电信号 -----> 处理电信号信号的转换标准:协议,是一种允许电子设备相互交流的一系列标准的规则。即开放系统参考模型OSI/RM,简称“OSI”。OSI参考模型将计算机网络分为七个层次,自上而下依次为:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层。1、应用层:

Awen0201的博客 2547

hdu 5905 Black White Tree 树dp

我们可以思考对一个子树如果他节点数固定那么黑点数一定是连续变化的,那我们可以很容易想到找到每个子树大小的黑点最大值和最小值也就是dp[i]表示子树大小为i的黑点最大值,dp1[i]为最小值,剩下的就是dp。 #include #include #include #include #include using namespace std; const int maxn=2004; int dp[m

huantwofat 743

【R语言-生存分析之观察生存率计算】

全人群肿瘤登记资料常用的统计分析指标包括发病率、死亡率、现患率以及生存率等,其中肿瘤生存率是评估肿瘤治疗效果和肿瘤负担的必要指标,其计算涉及肿瘤患者的发病、 死亡和随访三个方面的资料, 数据整理和计算过程均较为复杂, 如何及时、 准确地计算肿瘤生存率, 并使其可以在不同地区、 不同人群、不同时期间被客观公正地比较和评价,仍然是统计学家们一直在研究的主题。

weixin_46579654的博客 5446

【HDU】5905 Black White Tree【树dp,枚举子树,对于一个a,其对应的可行b一定是一个连续区间】

题目链接:Black White Tree枚举子树,对于一个a,其对应的可行b一定是一个连续区间。#include <bits/stdc++.h> using namespace std ;typedef long long LL ; typedef pair < int , int > pii ;#define clr( a , x ) memset ( a , x , sizeof a )con

poursoul 773

别再只会用官网例子了!Vxe-Table过滤功能深度自定义:从下拉框到服务端筛选的完整配置流程

本文深入解析Vxe-Table表格组件的过滤功能,从基础UI定制到服务端筛选的完整实现流程。通过实战案例演示如何超越官方示例,实现下拉框、日期范围等复杂过滤交互,并详细讲解混合过滤策略与性能优化技巧,帮助开发者应对大数据量场景下的表格筛选需求。

weixin_42526353的博客 208

STM32——LCD—FSMC原理简介

STM32——LCD—FSMC原理简介

weixin_62584795的博客 2991

HDU 5113 Black And White (DFS+剪枝)*

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 #include&lt;bits/stdc++.h&gt; using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)&lt;(z);(x)++) #define ll lon...

满月照亮的路 328

OpenCASCADE完整教程专栏完整目录

OpenCASCADE完整教程专栏完整目录

希望我的博客,能帮上你解决学习中工作中所遇到的问题 9732

hdu 5905tree

tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 8 Accepted Submission(s) : 6 Problem Description There is a tree(the tree is a connect

wo的只属于我 353

Android 13 Settings蓝牙列表卡顿问题排查及优化过程

此时我们的卡顿优化基本完成了,我们应该要考虑内存是否会泄漏,如果大家不知道哪些需要回收可以用leakcanary和android profiler进行内存检测,看代码有没有内存泄漏。

龚礼鹏的博客 2556

【Leetcode】269.火星词典(Hard)

应用拓扑排序

明朗晨光的专栏 783

POJ3364 HDU1802 UVA11231 Black and white painting【数学】

Black and white painting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2842 Accepted: 1938 Description You are visiting the Centre Pompidou which contains a lot of modern paintings. In ...

海岛Blog 760

linux mmap系统调用

brk/mmap linux 提供了两个比较重要的系统调用brk 和mmap,用于向内核申请相应用户空间,内核会根据系统运行状态判定是否申请新的VMA来管理新申请的用户空间,brk和mmap在整个系统中都占有非常重要的地位。 brk()系统调用被gblic进行了进一步封装成malloc接口,用户层程序一般都是通过调用malloc,由glibc间接调用brk来向内核申请用户空间。brk申请的用户空间属于堆空间 mmap系统调用也可以向内核申请用户空间,不过与brk不同的师,mmap申请的空间属于mma.

weixin_42730667的博客 1282

hdu5905 Black White Tree

hdu5905 Black White Tree

哔哔~哔哔哔哔~ 670

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【HDU 4418】【概率DP 高斯消元】 Time travel 给出一个数轴,有一个起点和终点,某人可以走1-m步,每一种有一个概率,初始有一个方向,走到头则返回,问到达终点期望

传送门:HDU 4418 描述: Time travel Problem Description Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to finish a mission by travel

魔笛手的博客 747

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【HDU 5904 LCIS + dp】

LCIS Problem Description Alex has two sequences a1,a2,…,an and b1,b2,…,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order.Input There are multiple

楚江枫 785

【hdu5890】【bitset+dp】Eighty seven【每次从序列中删掉第i,j,k个数后,是否存在一种选10个数和为87的方案】

传送门:HDU 5890 Eighty seven 描述: Eighty seven Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 819    Accepted Submission(s): 277

魔笛手的博客 729

hdu 2069Coin Change(dp)

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17261    Accepted Submission(s): 5905 Problem Description Suppose the

GreyWings的博客 378

hdu5904LCIS+dp+最长公共子串

LCIS Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 501 Accepted Submission(s): 231Problem Description Alex has two sequences a1,a2,…,an

xtulollipop 422

hdu 4472 Count【思维+dp】好题

Count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2330    Accepted Submission(s): 1554 Problem Description Prof. Tigris is the head

mengxiang000000 850

HDU 5904 LCIS (DP)

LCIS Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 400    Accepted Submission(s): 182 Problem Description Alex has two sequences 

Calm微笑 401

hdu5904 BestCoder Round #87 LCIS -dp

LCIS Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order. There are multiple test cases. The first line of input contains an integer T, indicating the numb

ly59782的博客 496

HDU 2955 Robberies

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16116    Accepted Submission(s): 5905 Problem Description The aspiring Roy th

马少爷~ 245

HDU 5583 Kingdom of Black and White 暴力

Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1890Accepted Submission(s): 588 Problem Description In the Kin...

weixin_33915554的博客 114

刷题记录

不忘初心

QWsin的博客 741
上一篇: HDU2896,ac自动机模板题
下一篇: HDU6024(dp)
creatorx
博客等级 码龄10年 127粉丝 145原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值