HDU 5927 Auxiliary Set (树形)
来源:互联网 发布:mud游戏编程 编辑:程序博客网 时间:2024/05/29 18:21
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2035 Accepted Submission(s): 602
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东北地区大学生程序设计竞赛 - 重现赛
题意:
求出每次询问中重要的点的个数。给你不重要的点,这些点如果是两个重要点的LCA,那这个不重要的就变成重要的了。
POINT:
给你的x个点之外的所有点都是重要的。那么只要看不重要的点里有几个重要的。
遍历数,存下fa是谁和son的个数,和深度。
这x个点按深度排序。那么从最深的开始。
如果son>=2,那么就是重要的点,(即肯定有两个重要的点的LCA是他——仔细想想)。
son==1,这个点肯定不是,但是这个点的重要的son可以通过这个点来使其他点变重要。所以这个分支还需要,不能son--.
son=0,那肯定不是,而且要把这个点的fa的son--。因为不需要这个分支了。
#include <stdio.h>#include<iostream>#include <math.h>#include<algorithm>#include <string.h>using namespace std;const int maxn = 2e5+10;int head[maxn],nxt[maxn],sz;int to[maxn];int fa[maxn],son[maxn],dep[maxn];int d;void dfs(int u,int pre){ fa[u]=pre; dep[u]=d++; for(int i=head[u];i!=-1;i=nxt[i]){ int v=to[i]; if(v==pre) continue; dfs(v,u); son[u]++; }}bool cmd(int a,int b){ return dep[a]>dep[b];}int main(){ int T; scanf("%d",&T); int cas=1; while(T--){ sz=0; d=1; int n,q; scanf("%d %d",&n,&q); fa[n]=son[n]=0; for(int i=1;i<=n;i++) head[i]=-1; for(int i=1;i<=n-1;i++){ fa[i]=son[i]=0; int u,v; scanf("%d %d",&u,&v); to[sz]=v; nxt[sz]=head[u];head[u]=sz++; to[sz]=u; nxt[sz]=head[v];head[v]=sz++; } dfs(1,0); int temp[maxn]; int num[maxn]; printf("Case #%d:\n",cas++); while(q--){ int x;scanf("%d",&x); int ans=n-x; for(int i=1;i<=x;i++) scanf("%d",&num[i]); sort(num+1,num+1+x,cmd); for(int i=1;i<=x;i++) temp[num[i]]=son[num[i]]; for(int i=1;i<=x;i++){ if(temp[num[i]]>1){ ans++; }else if(temp[num[i]]==0){ temp[fa[num[i]]]--; } } printf("%d\n",ans); } } return 0;}
阅读全文
0 0
- HDU 5927 Auxiliary Set (树形)
- HDU 5927 Auxiliary Set (树形DP+思维)
- HDU - 5927 Auxiliary Set (树形DP)
- hdu 5927 Auxiliary Set
- hdu 5927 Auxiliary Set
- HDU 5927 Auxiliary Set
- HDU 5927Auxiliary Set
- hdu 5927 Auxiliary Set (分析*)
- HDU 5927 Auxiliary Set(DFS)
- hdu 5927 Auxiliary Set(思路)
- hdu 5927 Auxiliary Set dfs
- F - Auxiliary Set HDU - 5927
- hdu 5927 Auxiliary Set (dfs)
- [HDU 5927] Auxiliary Set (DFS+树上LCA)
- HDU 5927 Auxiliary Set(dfs+逆向思维)
- HDU 5927 Auxiliary Set(树上搞事)
- HDU 5927 Auxiliary Set dfs(思维)
- HDU 5927 Auxiliary Set By Assassin
- 导入excel 读取单元格报错
- position中的absolute和relative
- Maven新建webapp项目index.jsp报错
- 批处理改ip
- Træfɪk
- HDU 5927 Auxiliary Set (树形)
- FTP服务基本原理与配置
- Linux之redhat中的磁盘配额与加密
- 6.6 利息存款问题
- C++拷贝构造函数详解
- 巩固与修炼(二)
- bzoj1697 [Usaco2007 Feb]Cow Sorting牛排序
- 日用品 跟帖补充一个单词奖励1Euro,请勿恶意灌水,以保持帖子的连贯.
- Gradle project sync failed Error:Connection reset