hdu-5927 Auxiliary Set(树形dp)
题目链接:
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 521 Accepted Submission(s): 148
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
∙It is an important vertex
∙It is the least common ancestor of two different important vertices.
You are given a tree with n vertices (1 is the root) and q queries.
Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.
For each test case, the first line contains two integers n (1≤n≤100000), q (0≤q≤100000).
In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n) indicating there is an edge between uii and vi in the tree.
In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000) indicating the number of vertices in the query set.Then comes with mi different integers, indicating the nodes in the query set.
It is guaranteed that ∑qi=1mi≤100000.
It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e4+120;
const int maxn=1e5+20;
const double eps=1e-12;
int n,q,m,son[maxn],fa[maxn],dep[maxn],SON[maxn],temp[maxn];
vector<int>ve[maxn];
void dfs(int cur,int father,int deep)
{
int len=ve[cur].size();
dep[cur]=deep;
fa[cur]=father;
if(cur!=1)son[cur]=len-1;
else son[cur]=len;
for(int i=0;i<len;i++)
{
int x=ve[cur][i];
if(x==father)continue;
dfs(x,cur,deep+1);
}
}
int cmp(int x,int y){return dep[x]>dep[y];}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
printf("Case #%d:\n",++Case);
for(int i=0;i<=n;i++)ve[i].clear();
read(n);read(q);
int u,v,x;
for(int i=1;i<n;i++)
{
read(u);read(v);
ve[u].push_back(v);
ve[v].push_back(u);
}
dfs(1,0,0);
while(q--)
{
read(m);
int ans=n-m;
for(int i=1;i<=m;i++)read(temp[i]),SON[temp[i]]=son[temp[i]];
sort(temp+1,temp+m+1,cmp);
for(int i=1;i<=m;i++)
{
if(SON[temp[i]]>=2)ans++;
else if(SON[temp[i]]==0)
{
int father=fa[temp[i]];
SON[father]--;
}
}
printf("%d\n",ans);
}
}
return 0;
}
hdu-5927 Auxiliary Set(树形dp)的更多相关文章
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- hdu 5452 Minimum Cut 树形dp
Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu 1520Anniversary party(简单树形dp)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Install Air Conditioning HDU - 4756(最小生成树+树形dp)
Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...
- HDU 3586 二分答案+树形DP判定
HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
- HDU 1011 Starship Troopers 树形DP 有坑点
本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...
随机推荐
- php面试题之一——PHP核心技术(高级部分)
一.PHP核心技术 1.写出一个能创建多级目录的PHP函数(新浪网技术部) <?php /** * 创建多级目录 * @param $path string 要创建的目录 * @param $m ...
- sublimeCodeIntel 的配置
在项目的根目录目录下建立.codeintel/config 但是在windows 需要进入dos 环境下建立.以点开头的文件夹和文件.资源管理器不允许创建点开头的文件或文件夹,但在命令提示符下是可以的 ...
- 设置让ASP.NET管道接收所有类型的请求
在web.config文件添加如下一段配置: <configuration> <system.webServer> <modules runAllManagedModul ...
- C#6.0语法糖剖析(一)
1.自动属性默认初始化 使用代码 "; 编译器生成的代码: public class Customer { [CompilerGenerated] private string kBacki ...
- 自定义View_2_关于自定义组合View
自定义View(2) Android当中给我们提供了丰富的UI控件,当然也许满足不了我们的需求,我们就必须学会自定义自己的View,我们怎么算是自定义自己的view呢! 我们会根据原来有的View对V ...
- ASP.NET HttpRuntime.Cache缓存类使用总结
1.高性能文件缓存key-value存储—Redis 2.高性能文件缓存key-value存储—Memcached 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...
- XML的介绍
XML是指可扩展标记语言(eXtensible Markup Language),它是一种标记语言,类似于HTML.它被设计的宗旨是传输数据,而非显示数据. XML标签没有被预定义,需要开发者自定 ...
- IOS 计步器
这篇博客介绍的是当前比较流行的“计步器”-只是简单的知识点 计步器的实现在IOS8开始进行了改变. 但是我会对之前之后的都进行简单介绍. IOS 8 - // // ViewController.m ...
- IOS 杂笔-4(属性与成员变量的区别)
属性可以用点语法,比如self.xxx,在外部调用也同样可以someClass.xxx. 属性实际上是对一组set和get方法的简单封装(oc的get方法没有get前缀),同样会自动生成一个私有的成员 ...
- Extjs 控件属性(部分)
Ext.form.TimeField: 配置项: maxValue:列表中允许的最大时间 maxText:当时间大于最大值时的错误提示信息 minValue:列表中允许的最小时间 minText:当时 ...