HDU5927-Auxiliary Set
来源:互联网 发布:免费棋牌app源码 编辑:程序博客网 时间:2024/05/29 17:39
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1539 Accepted Submission(s): 464
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东北地区大学生程序设计竞赛 - 重现赛
Recommend
wange2014
题意:给一棵有根树,有q次询问,每次给出m个“不重要”的点,但如果一个点是两个重要的点的最近公共祖先,它会变成重要的点,问有多少个重要点
解题思路:先求出所有点在树中的层数,每次对给出的m个不重要点根据它们在树中的层数从大到小排序,然后对每个不重要点看和它相连并且层数大于它的点,若这些点的下面存在重要点,那么这个点标记为1,若存在两个以上这样的点,则答案加一
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <math.h>#include <time.h>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <map>#include <set>using namespace std;const int INF = 0x3f3f3f3f;#define LL long longint s[100009], nt[200009], e[200009],n,m,ans,level[100009];bool visit[100009];struct node{int id, level;}a[100009];bool cmp(node a, node b){return a.level > b.level;}void dfs(int x,int y){for (int i = s[x];~i;i = nt[i]){int ee = e[i];if (level[ee]) continue;level[ee] = y + 1;dfs(ee, y + 1);}}void f(int x){int cnt = 0;for (int i = s[x];~i;i = nt[i]){int ee = e[i];if (level[ee] < level[x]) continue;if (visit[ee]) cnt++;if (cnt >= 2) { ans++; break; }}if (cnt >= 1) visit[x] = 1;}int main(){int t,cas=0,q;scanf("%d", &t);while (t--){printf("Case #%d:\n", ++cas);scanf("%d%d", &n, &q);int cnt = 1;memset(s, -1, sizeof s);memset(level, 0, sizeof level);for (int i = 1;i < n;i++){int u, v;scanf("%d%d", &u, &v);nt[cnt] = s[u], e[cnt] = v, s[u] = cnt++;nt[cnt] = s[v], e[cnt] = u, s[v] = cnt++;}level[1] = 1;dfs(1,1);while (q--){scanf("%d", &m);memset(visit, true, sizeof visit);for (int i = 1;i <= m;i++)scanf("%d", &a[i].id), visit[a[i].id] = 0,a[i].level=level[a[i].id];sort(a + 1, a + 1 + m, cmp);ans = 0;for (int i = 1;i <=m;i++) f(a[i].id);printf("%d\n",ans+(n-m));}}return 0;}
阅读全文
0 0
- HDU5927 Auxiliary Set(dfs)
- HDU5927-Auxiliary Set
- HDU5927 Auxiliary Set ->树形DP
- Auxiliary Set hdu5927(dfs)
- dfs序 树状数组--hdu5927 Auxiliary Set
- Auxiliary Set
- hdu 5927 Auxiliary Set
- hdu 5927 Auxiliary Set
- HDU 5927 Auxiliary Set
- Auxiliary Set HDU
- HDU 5927Auxiliary Set
- HDU5927
- hdu5927 2016 东北赛F Auliary Set
- 【HDU5927 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F】【dfs序 + 线段树 or 树状数组 复杂度计算】Auxiliary Set 一个点如果是好点或是两个好点的LCA就是好
- 5927 Auxiliary Set (CCPC) 无根树转有根树
- hdu 5927 Auxiliary Set dfs
- HDOJ 5927 Auxiliary Set【树】
- Auxiliary Set----DFS思维题
- nmap基本使用方法
- c++的运算符的重载的代码
- NSValue的使用
- CSS选择器总结
- 字符串的全排列和组合算法
- HDU5927-Auxiliary Set
- java的面向对象基础
- hihoCoder 第157周 二进制小数
- Get 请求中文参数乱码解析
- Ubuntu下NFS服务器的搭建与配置
- Python UDP Server and Client
- git
- 让程序轻松支持换肤功能--VB6示例
- 【OpenVswitch源码分析之三】控制面关键接口与调用流程