[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 ...
随机推荐
- [CF9D]How Many Trees?_动态规划_树形dp_ntt
How many trees? 题目链接:https://www.codeforces.com/contest/9/problem/D 数据范围:略. 题解: 水题. $f_{i,j}$表示$i$个节 ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- [转帖]运维必读:Linux 的内存分页管理
运维必读:Linux 的内存分页管理 https://cloud.tencent.com/developer/article/1356431 内存是计算机的主存储器.内存为进程开辟出进程空间,让进程在 ...
- (4)Spring Boot Web开发---静态资源
文章目录 对静态资源的映射规则 模板引擎 Thymeleaf 使用 & 语法 使用之前将的快速创建项目的方法,勾选我们需要的场景,这里我需要 web --> web.sql --> ...
- C 语言字符串的比较
C 语言字符串的比较 #include <stdio.h> #include <Windows.h> #include <string.h> int main(vo ...
- oracle sqlplus命令
show和set命令是两条用于维护SQL*Plus系统变量的命令 SQL> show all --查看所有68个系统变量值 SQL> show user --显示当前连接用户 SQL> ...
- truncate删除一个分区,测试全局索引是否失效
目的,有一个清理数据的需求,需要删除历史的一个分区所有记录信息,但是存在主键global索引,如何更好的维护. 如下测试流程一 提前创建好一个已时间created 字段作为分区键的范围分区表 SQL& ...
- Powershell学习笔记:(二)、基础知识
从Window7以后,WIndows系统都自带了Windows PowerShell. 自带版本如下 WIndow7 2.0 WIndow8 3.0 Window8.1 4.0 Win ...
- [JZOJ5281]钦点题解--瞎搞+链表
[JZOJ5281]钦点题解--瞎搞+链表 题目链接 于 暴 力 过
- 关于http的小知识
http客户端发起请求,创建端口 http服务器在端口监听客户端请求 http服务器向客户端返回状态和内容 浏览器: 1.Chrome搜索自身的DNS缓存 2.搜索操作系统自身的DNS缓存(浏览器没有 ...