[hdoj5927][dfs]
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
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
3
6
3
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
题意:给一棵树,给一堆定义,有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]的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- P5200 [USACO19JAN]Sleepy Cow Sorting
P5200 [USACO19JAN]Sleepy Cow Sorting 题目描述 Farmer John正在尝试将他的N头奶牛(1≤N≤10^5),方便起见编号为1…N,在她们前往牧草地吃早餐之前排 ...
- 正整数序列 Help the needed for Dexter ,UVa 11384
题目描述 Description 给定正整数n,你的任务是用最少的操作次数把序列1, 2, …, n中的所有数都变成0.每次操作可从序列中选择一个或多个整数,同时减去一个相同的正整数.比如,1,2,3 ...
- php-获取某个文件夹下面的文件数量
/** * 获取文件夹下文件的数量 * @param $url 传入一个url如:/apps/web * @return int 返回文件数量 */ public function getFileNu ...
- 雷达无线电系列(三)经典CFAR算法门限因子alpha计算(matlab)
前言 本文汇集CA.SO.GO.OS.杂波图等恒虚警算法的门限因子求解方法及其函数 1,CA-CFAR [非常简单,可以直接求解] %% 均值恒虚警_门限因子计算公式 %% 版本:v1 %% 时间:2 ...
- 在论坛中出现的比较难的sql问题:27(字符串拆分、字符串合并、非连续数字的间隔范围、随机返回字符串)
原文:在论坛中出现的比较难的sql问题:27(字符串拆分.字符串合并.非连续数字的间隔范围.随机返回字符串) 在论坛中看到一个帖子,帖子中有一些sql方面的面试题,我觉得这些面试题很有代表性. 原帖的 ...
- [转载]Python 包管理工具
[转载]Python 包管理工具 最近由于机缘巧合,使用各种方法安装了一些Python包,所以对Python的包管理开始感兴趣.在网上找到一篇很好的文章:https://blog.zengrong.n ...
- 详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失
在Spring Cloud中我们用Hystrix来实现断路器,Zuul中默认是用信号量(Hystrix默认是线程)来进行隔离的,我们可以通过配置使用线程方式隔离. 在使用线程隔离的时候,有个问题是必须 ...
- iOS音频学习笔记一:常见音频封装格式及编码格式
(1) pcm格式 pcm是经过话筒录音后直接得到的未经压缩的数据流 数据大小=采样频率*采样位数*声道*秒数/8 采样频率一般是22k或者44k,位数一般是8位或者16位,声道一 ...
- echart——关系图graph详解
VueEchart组件见上一篇 <template> <VueEcharts :options="options" auto-resize /> </ ...
- 互联网项目中mysql推荐(读已提交RC)的事务隔离级别
[原创]互联网项目中mysql应该选什么事务隔离级别 Mysql为什么不和Oracle一样使用RC,而用RR 使用RC的原因 这个是有历史原因的,当然要从我们的主从复制开始讲起了!主从复制,是基于什么 ...