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. 网络爬虫基本概念与Scrapy工具包使用

    Scrapy网络爬虫 Scrapy结构图: Scrapy流动图 图 2-1 1.在D:\Workspace下新建ScrapyTest文件夹,即D:\Workspace\ScrapyTest 2.cd ...

  2. Java连接Jira,创建、修改、删除工单信息

    还不了解Jira是什么的同学可以看一下这篇文章:https://www.cnblogs.com/wgblog-code/p/11750767.html 本篇文章主要介绍如何使用Java操作Jira,包 ...

  3. Linux上安装pstree命令(-bash: pstree: command not found)

    一.pstree命令的安装 1.在 Mac OS上 brew install pstree 2.在 Fedora/Red Hat/CentOS yum -y install psmisc 3.在 Ub ...

  4. Luogu5307 [COCI2019] Mobitel 【数论分块】【递推】

    题目分析: 对于向上取整我们总有,$\lceil \frac{\lceil \frac{n}{a} \rceil}{b} \rceil = \lceil \frac{n}{a*b} \rceil$这个 ...

  5. Consul 注册中心介绍

    在 Spring Cloud 体系中,几乎每个角色都会有两个以上的产品提供选择,比如在注册中心有:Eureka.Consul.zookeeper.etcd 等:网关的产品有 Zuul.Spring C ...

  6. App客户端性能测试点总结

    一.内存 测试范围1. 空闲状态下的应用内存消耗情况2. 中等规格状态下的应用内存消耗情况3. 满规格状态下的应用内存消耗情况4. 应用内存峰值情况5. 应用内存泄露情况6. 应用是否常驻内存7. 压 ...

  7. windowsAPI创建句柄失败的返回值

    创建句柄的api返回值 INVALID_HANDLE_VALUE CreateFile CreateNamedPipe CreateToolhelp32Snapshot FilterConnectCo ...

  8. View Controller Programming Guid for iOS 笔记

    1.View Controller 基础 1.1 View Controller 分类 ViewController分为container view controller 和content view ...

  9. 开始Swift学习之路

    Swift出来好几个月了,除了同事分享点知识外,对swift还真没有去关心过.GitHub上整理的学习Swift资料还是很不错的,目前也推出了电子书和PDF格式. Swift的语法和我们平常开发的语言 ...

  10. 【亲测有效】vs2017无法断点

    解决方案: 一:设置为Debug模式 二:[工具]--[选项]--[调试]--[常规],取消打钩[要求源文件与原始版本完全匹配]