You are given a tree with n nodes. The weight of the i-th node is wi. Given a positive integer m, now you need to judge that for every integer i in [1,m] whether there exists a connected subgraph which the sum of the weights of all nodes is equal to i.

Input:

The first line contains an integer T (1 ≤ T ≤ 15) representing the number of test cases. For each test case, the first line contains two integers n (1 ≤ n ≤ 3000) and m (1 ≤ m ≤ 100000), which are mentioned above. The following n−1 lines each contains two integers ui and vi (1 ≤ ui,vi ≤ n). It describes an edge between node ui and node vi. The following n lines each contains an integer wi (0 ≤ wi ≤ 100000) represents the weight of the i-th node. It is guaranteed that the input graph is a tree.

Output :

For each test case, print a string only contains 0 and 1, and the length of the string is equal to m. If there is a connected subgraph which the sum of the weights of its nodes is equal to i, the i-th letter of string is 1 otherwise 0.
Example
standard input

2

4 10

1 2

2 3

3 4

3 2 7 5

6 10

1 2

1 3

2 5

3 4

3 6

1 3 5 7 9 11

standard output

0110101010

1011111010

题意:给你一棵树 询问现在小于等于m的权值出现情况 权值是任意联通子树的点权和

思路:对于第i个节点 我们把问题的规模分成 包含i点的子树 不包含i点的子树 对于第二种情况 可以递归求解

在计算经过i点的图的权值的时候我们可以用bitset来标记 很巧妙 具体操作可以看代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int head[3007],vis[3007];
int d[3007],val[3007];
struct node{
int to,next;
};
node edge[6007];
int cnt,n,m;
void init(){
cnt=0;
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
}
void add(int from,int to){
edge[++cnt].to=to;
edge[cnt].next=head[from];
head[from]=cnt;
}
int son[3007];
int now_size,sz,root;
void find_root(int u,int fa){
son[u]=1; int res=-inf;
for(int i=head[u];i;i=edge[i].next){
if(vis[edge[i].to]||edge[i].to==fa) continue;
int to=edge[i].to;
find_root(to,u);
son[u]+=son[to];
res=max(res,son[to]);
}
res=max(res,sz-son[u]);
if(res<now_size) now_size=res,root=u;
}
bitset<100007>bits[3007],ans;
void solve(int u,int fa){
bits[u]<<=val[u]; //把之前出现过的权值都加上val[u]
for(int i=head[u];i;i=edge[i].next){
if(vis[edge[i].to]||edge[i].to==fa) continue;
int to=edge[i].to;
bits[to]=bits[u]; //向下传递
solve(to,u);
bits[u]|=bits[to]; //收集信息
}
}
void dfs(int u){ //分治
vis[u]=1;
bits[u].reset();
bits[u].set(0); //把0位置的置1
solve(u,0);
ans|=bits[u];
int totsz=sz;
for(int i=head[u];i;i=edge[i].next){
if(vis[edge[i].to]) continue;
int to=edge[i].to;
now_size=inf; root=0;
sz=son[to]>son[u]?totsz-son[u]:son[to];
find_root(to,0);
dfs(root);
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
init();
ans.reset();
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int from,to;
scanf("%d%d",&from,&to);
add(from,to); add(to,from);
}
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
now_size=inf,sz=n,root=0;
find_root(1,0);
dfs(root);
for(int i=1;i<=m;i++)
printf("%d",(int)ans[i]);
printf("\n");
}
return 0;
}

hdu 6268 Master of Subgraph(点分治+bitset)的更多相关文章

  1. HDU - 6268: Master of Subgraph (分治+bitset优化背包)

    题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包 ...

  2. HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)

    题目链接  2017 CCPC Hangzhou  Problem E 题意  给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...

  3. 算法学习分析-点分治 HDU 6269 Master of Subgraph

    首先给出定义 点分治是一种处理树上路径的工具 挂出一道题目来:Master of Subgraph 这道题目让你求所有联通子图加和所能产生数字,问你1到m之间,那些数字可以被产生 这道题目,假如我们利 ...

  4. Master of Subgraph

    Problem E. Master of SubgraphYou are given a tree with n nodes. The weight of the i-th node is wi. G ...

  5. [HDU6268]Master of Subgraph

    [HDU6268]Master of Subgraph 题目大意: 一棵\(n(n\le3000)\)个结点的树,每个结点的权值为\(w_i\).给定\(m(m\le10^5)\),对于任意\(i\i ...

  6. CCPC 2016 杭州 E. Master of Subgraph(点分治+bitset优化DP)

    题目链接:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 题意:给定一棵有 n 个结点的树和一个数 m,对于 i ∈ ...

  7. Hdu 6268 点分治 树上背包 bitset 优化

    给你一颗大小为n(3000)的树,树上每个点有点权(100000),再给你一个数m(100000) i为1~m,问树中是否存在一个子图,使得权值为i. 每次solve到一个节点 用一个bitset维护 ...

  8. HDU 5016 Mart Master II (树上点分治)

    题目地址:pid=5016">HDU 5016 先两遍DFS预处理出每一个点距近期的基站的距离与基站的编号. 然后找重心.求出每一个点距重心的距离.然后依据dis[x]+dis[y] ...

  9. HDU 5324 Boring Class【cdq分治】

    这就是一个三维排序的问题,一维递减,两维递增,这样的问题用裸的CDQ分治恰好能够解决. 如同HDU 4742(三维排序,一个三维都是递增的) 由于最小字典序比較麻烦,所以要从后面往前面做分治.每一个点 ...

随机推荐

  1. web元素定位和appium-app元素定位

    一.web页面元素定位工具介绍 1.打开google浏览器,按F12进入开发者模式,如下图: 2.用鼠标点击下图红色框中的箭头--然后鼠标移动到web页面的元素上(此处为百度框),会自动定位到对应的h ...

  2. git的基础知识

    git 分布式版本控制工具 具备的功能 协同开发 多人并行不悖修改服务器端的同一个文件 数据备份 不仅保持目录和文件当前状态,还能保存每一个提交的历史版本 版本管理 保存每一个版本的文件信息的时候做到 ...

  3. laravel5.4 接入qq第三方登录

    第一步:先composer安装需要用到的依赖,命令行如下 composer require socialiteproviders/qq 第二步:在config/app.php 中的 providers ...

  4. 改进你的c#代码的5个技巧(三)

    本文完全独立于前两篇文章.如果你喜欢它们,我希望你也会喜欢这个.在上一篇文章中,我展示了哪种方法更快,并比较了代码的执行速度.在本文中,我将展示不同代码片段的内存消耗情况.为了显示内存映射和分配图,我 ...

  5. Flutter 布局类组件:弹性布局(Flex)

    前言 弹性布局允许子组件按照一定比例来分配父容器空间,Flutter中的弹性布局主要通过Flex和Expanded来配合实现. Flex Flex组件可以沿着水平或垂直方向排列子组件,如果你知道主轴方 ...

  6. 【Linux】以001格式循环到100保证位数是3位

    这里有一个前提,要保证数位是相同的 确实数字是1-100  但是数位是不同的,需要统一一下位数必须是3位的 这个问题在很多论坛上用的都是printf这个命令,确实可以达到这个效果,但是没有我下面介绍的 ...

  7. 【Oracle】10g 11g下载路径

    现在直接点击不能下载了 要经过oracle许可才可以下载 如果嫌麻烦可以用迅雷直接下载密码是这个 一般不会动了 大家也不用帮我找回密码了 每次都改 也很麻烦的用迅雷下就不用密码了 下载也不会卡到最后 ...

  8. 【ORA】ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O

    操作系统是CentOS 5.11 数据库 10.2.0.5.0 晚上查看数据库,发现数据库报错查看相关的trace文件内容如下: *** SERVICE NAME:(SYS$BACKGROUND) 2 ...

  9. C语言------三目运算符(条件运算符)

    今天在看C语言的时候看到了下面的代码(废话少说,直接上代码): #include <stdio.h> int main() {int max(); extern int A,B,C; // ...

  10. 爬虫学习(二)requests模块的使用

    一.requests的概述 requests模块是用于发送网络请求,返回响应数据.底层实现是urllib,而且简单易用,在python2.python3中通用,能够自动帮助我们解压(gzip压缩的等) ...