HDU5927 Auxiliary Set(dfs)
来源:互联网 发布:js面向对象编程案例 编辑:程序博客网 时间:2024/05/16 08:00
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 937 Accepted Submission(s): 289
Total Submission(s): 937 Accepted Submission(s): 289
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.
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
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 integersui,vi(1≤ui,vi≤n) indicating there is an edge between ui i and vi in the tree.
In the next q lines, the i-th line first comes with an integermi(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 whichn≥1000 or∑qi=1mi≥1000 is no more than 10.
For each test case, the first line contains two integers n (
In the following n -1 lines, the i-th line contains two integers
In the next q lines, the i-th line first comes with an integer
It is guaranteed that
It is also guaranteed that the number of test cases in which
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.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Sample Input
16 36 42 55 41 55 33 1 2 31 53 3 1 4
Sample Output
Case #1:363HintFor 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
2016CCPC东北地区大学生程序设计竞赛 - 重现赛
题意:有一颗有根数树(根为1),n个节点给出n-1条边,q次询问,每次询问给出一个不重要点集合,有mi个,然后求一种集合的元素个数,
这个集合内包括:重要的点,两个不同的重要的点的最近公共祖先。
思路:首先,结果集合里有n-mi个重要的点,剩下的就在mi个不重要的点找是否有符合"两个不同的重要的点的最近公共祖先"这个要求的点。
先做一遍dfs,算出每个结点的深度,父结点,和孩子个数。然后每次询问把mi个不重要点按照深度从大到小排序。遍历不重要结点,如果当前结点的
儿子数目大于或等于2,那么它就符合要求,否则如果他的儿子数是0那就让它的父结点的儿子数减一(这样做可以使得如果某个结点有儿子的结点,
那么他的儿子结点里一定有重要的点或者有通过不重要结点与它的连通,注意每个结点只算跟它直接相连的儿子个数,所以如果儿子数目大于或等于2,
就肯定有2个以上的重要结点而且不在同一条边上,那当前遍历的点就是它们的最近公共祖先)。
思路:首先,结果集合里有n-mi个重要的点,剩下的就在mi个不重要的点找是否有符合"两个不同的重要的点的最近公共祖先"这个要求的点。
先做一遍dfs,算出每个结点的深度,父结点,和孩子个数。然后每次询问把mi个不重要点按照深度从大到小排序。遍历不重要结点,如果当前结点的
儿子数目大于或等于2,那么它就符合要求,否则如果他的儿子数是0那就让它的父结点的儿子数减一(这样做可以使得如果某个结点有儿子的结点,
那么他的儿子结点里一定有重要的点或者有通过不重要结点与它的连通,注意每个结点只算跟它直接相连的儿子个数,所以如果儿子数目大于或等于2,
就肯定有2个以上的重要结点而且不在同一条边上,那当前遍历的点就是它们的最近公共祖先)。
#include <bits/stdc++.h>using namespace std;#define maxn 100010int head[maxn<<1], tot;int son[maxn], fa[maxn], cop[maxn];int dep[maxn], d[maxn];struct node{ int to, next;}G[maxn<<1];void add(int u, int v){ G[tot].to = v; G[tot].next = head[u]; head[u] = tot++;}void dfs(int now, int pre){ fa[now] = pre, son[now] = 0, dep[now] = dep[pre]+1; for(int i = head[now];i != -1;i = G[i].next){ int to = G[i].to; if(to==pre) continue; dfs(to, now); son[now]++; }}bool cmp(int a, int b){ return dep[a] > dep[b];}int main(){ int T, n, q, u, v, k = 1, m, i, j, ans; scanf("%d", &T); while(T--){ scanf("%d %d", &n, &q); tot = 1; memset(head, -1, sizeof head); for(i = 0;i < n-1;i++){ scanf("%d %d", &u, &v); add(u, v); add(v, u); } printf("Case #%d:\n", k++); dfs(1, 0); for(i = 0;i < q;i++){ scanf("%d", &m); for(j = 0;j < m;j++) scanf("%d", &d[j]); sort(d, d+m, cmp); for(j = 0;j < m;j++) cop[d[j]] = son[d[j]]; ans = n-m; for(j = 0;j < m;j++){ if(cop[d[j]]>=2) ans++; else if(cop[d[j]]==0) cop[fa[d[j]]]--; } printf("%d\n", ans); } }}
1 0
- HDU5927 Auxiliary Set(dfs)
- Auxiliary Set hdu5927(dfs)
- dfs序 树状数组--hdu5927 Auxiliary Set
- HDU5927-Auxiliary Set
- HDU5927 Auxiliary Set ->树形DP
- hdu 5927 Auxiliary Set dfs
- Auxiliary Set----DFS思维题
- hdu 5927 Auxiliary Set (dfs)
- hdu5927 dfs
- HDU 5927 Auxiliary Set dfs(思维)
- HDU 5927 Auxiliary Set DFS好题
- HDU 5927 Auxiliary Set(DFS)
- HDU 5927 Auxiliary Set (DFS+模拟)
- hdu 5927 Auxiliary Set dfs+逆向思维
- 【HDU5927 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F】【dfs序 + 线段树 or 树状数组 复杂度计算】Auxiliary Set 一个点如果是好点或是两个好点的LCA就是好
- Auxiliary Set
- [HDU 5927] Auxiliary Set (DFS+树上LCA)
- Auxiliary Set(dfs求每个节点的儿子个数)
- spring-学习,IOC容器的构建
- Jquery.EasyUI中树形插件的使用
- UMeng统计分析Android集成文档
- Java 位运算(与、或、非、异或)
- Android MediaPlayer的生命周期
- HDU5927 Auxiliary Set(dfs)
- 极客班
- linux网络编程之进程间通信基础(一):进程间通信概述
- 面向对象基础
- 线程间通信(生产者消费者问题)
- 【HDU 1085】【母函数】Holding Bin-Laden Captive!【给你a1个一元硬币,a2个两元硬币,a3个五元硬币,问不能凑出来的第一个面额是多少】
- python中的string相关用法(1)
- MyEclipse使用总结——设置MyEclipse开发项目时使用的JDK
- 2017今年CSDN博客排名