http://acm.hdu.edu.cn/showproblem.php?pid=5927

Auxiliary Set

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2991    Accepted Submission(s): 851

Problem Description
Given a rooted tree with n vertices, some of the vertices are important.

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.

 
Input
The first line contains only one integer T (T≤1000), which indicates the number of test cases.

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.

 
Output
For each test case, first output one line "Case #x:", where x is the case number (starting from 1).

Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.

 
Sample Input
1
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4
 
Sample Output
Case #1:
3
6
3

Hint

For the query {1,2, 3}:
•node 4, 5, 6 are important nodes For the query {5}:
•node 1,2, 3, 4, 6 are important nodes
•node 5 is the lea of node 4 and node 3 For the query {3, 1,4}:
• node 2, 5, 6 are important nodes

 
Source

题意:给一棵树,给一堆定义,有q次查询

题解:dfs先跑出各个顶点的深度和父节点,然后给q次询问按照深度排序,最后在q次询问中更新当前顶点对父节点的影响。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<"["<<#x<<"]"<<" "<<x<<endl;
const int maxn=1e5+;
int head[maxn],cnt,fa[maxn],www[maxn],wwww[maxn],dep[maxn];
struct edge{
int to;
int nex;
int fr;
}e[maxn<<];
struct pot{
int depth;
int id;
}p[maxn];
void adde(int u,int v){
e[cnt].fr=u;
e[cnt].to=v;
e[cnt].nex=head[u];
head[u]=cnt++;
}
void dfs(int u,int f){
fa[u]=f;
dep[u]=dep[f]+;
wwww[u]=;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(v==f)continue;
dfs(v,u);
wwww[u]++;
}
}
bool cmp(struct pot aa,struct pot bb){
return aa.depth>bb.depth;
}
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--){
int n,q;
scanf("%d%d",&n,&q);
cnt=;
for(int i=;i<=n;i++){
head[i]=-;
wwww[i]=;
}
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
adde(x,y);
adde(y,x);
}
dfs(,);
printf("Case #%d:\n",ca++);
for(int i=;i<=q;i++){
int w;
scanf("%d",&w);
for(int j=;j<=w;j++){
scanf("%d",&p[j].id);
p[j].depth=dep[p[j].id];
}
int ans=n;
sort(p+,p++w,cmp);
for(int j=;j<=w;j++){
int v=p[j].id;
www[v]++;
if(wwww[v]==www[v]){
www[fa[v]]++;
}
if(wwww[v]-www[v]<){ans--;}
}
for(int j=;j<=w;j++){
int v=p[j].id;
www[v]=;
www[fa[v]]=;
}
printf("%d\n",ans);
}
}
return ;
}

[hdoj5927][dfs]的更多相关文章

  1. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

随机推荐

  1. QT中PRO文件解析(转)

    From csdn blog: QT中PRO文件写法的详细介绍,很有用,很重要! 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下: ...

  2. linux环境下编写shell脚本实现启动停止tomcat服务

    第一步:以管理员的身份进入控制台,在指定目录下新建一个shell脚本,我这里命名为tomcat.sh 第二步:编写shell脚本 #!/bin/bash tomcat_home=/usr/tomcat ...

  3. Springmvc在项目启动时查询数据库并初始化静态变量

    private static List<ResourceEntity> resourceList = null; //初始化的全局静态变量 @Autowired private Resou ...

  4. python基础学习(九)

    19.解包 # 解包 unpacking user1 = ["张三", 21, "1999.1.1"] # tuple 类型 user2 = ("李四 ...

  5. oauth2 + jwt 实现用户中心

    由于公司项目比较杂,以前都是各产品线自行完成注册和登入.随着产品迭代,需要一个用户中心统一用户的管理,写个博客做个记录. 用oauth2实现,jwt作为token生成. oauth2流程图: user ...

  6. php中比较复杂但又常用的字符串函数

    php系统核心库自带的函数中,字符串比数组函数较为简单,但还是有一些较为复杂但又很常用的函数,比如下面的这些函数 explode()函数 用一个字符串来分割另一个字符串,返回结果是一个数组 explo ...

  7. 【HC89S003F4开发板】 6crc校验

    HC89S003F4开发板crc校验 前言 第一次用有带crc的mcu 使用资料自带的demo @实现效果 通过PC向MCU发送5个8位数据,MCU返回CRC校验值 void main() { /** ...

  8. 解决SVN蓝色问号的问题

    桌面或文件夹右键,选择TortoiseSVN->Settings打开设置对话框,选择Icon Overlays->Overlay Handlers->取消钩选Unversioned. ...

  9. 用Python爬取小说《一念永恒》

    我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...

  10. 宽字节 多字节 mbstowcs wcstombs

    函数 size_t wcstombs(char *dest, const wchar_t *src, size_t n); //wide-character to a multibyte n:被写入到 ...