Auxiliary Set----DFS思维题
来源:互联网 发布:任天堂vb用电池的吗 编辑:程序博客网 时间:2024/06/05 11:02
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1083 Accepted Submission(s): 340
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东北地区大学生程序设计竞赛 - 重现赛
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927
好一个思维DFS,这个题竟然能用dfs做我都没有想到,今天的比赛,我都没有看到这个题,放上大牛的链接,我从代码里讲吧。
http://blog.csdn.net/discreeter/article/details/52749361
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N=100010;struct node{ int to,next;}g[N*2];int cnt,head[N];int son[N],par[N],d[N],deep[N],tmp[N];bool vis[N];int n,m;void connect(int v,int u){//前向星存图 g[cnt].to=u; g[cnt].next=head[v]; head[v]=cnt++;}void dfs(int v,int fa,int d){//一遍dfs求出父节点,儿子节点和深度 vis[v]=true; par[v]=fa; deep[v]=d; for(int i=head[v];~i;i=g[i].next){ int u=g[i].to; if(!vis[u]) dfs(u,v,d+1),son[v]++; }}bool cmp(int a,int b){//按深度排序 return deep[a]>deep[b];}int main(){ int t; scanf("%d",&t); int ans=0; while(t--){ ans++; cnt=0; int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){//初始化 vis[i]=false; head[i]=-1; son[i]=0; } for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&v,&u); connect(u,v); connect(v,u); } dfs(1,-1,1); printf("Case #%d:\n",ans); while(m--){ int k; scanf("%d",&k); int sum=n-k;//最开始有这么多重要节点 for(int i=1;i<=k;i++){ scanf("%d",&d[i]); tmp[d[i]]=son[d[i]];//复制 } sort(d+1,d+1+k,cmp);//按深度排序 for(int i=1;i<=k;i++){ if(tmp[d[i]]>=2)//大牛讲的很清楚这里 sum++; else if(tmp[d[i]]==0)//因为是从最深开始的,所有最深的那个不重要节点下面肯定全都是重要的节点 tmp[par[d[i]]]--; } cout<<sum<<endl; } } return 0;}
0 0
- Auxiliary Set----DFS思维题
- HDU 5927 Auxiliary Set dfs(思维)
- hdu 5927 Auxiliary Set dfs+逆向思维
- HDU 5927 Auxiliary Set(dfs+逆向思维)
- HDU 5927 Auxiliary Set DFS好题
- HDU5927 Auxiliary Set(dfs)
- hdu 5927 Auxiliary Set dfs
- Auxiliary Set hdu5927(dfs)
- hdu 5927 Auxiliary Set (dfs)
- HDU 5927 Auxiliary Set(DFS)
- dfs序 树状数组--hdu5927 Auxiliary Set
- HDU 5927 Auxiliary Set (DFS+模拟)
- (HDU 5927)Auxiliary Set 思维题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5927 Auxiliary Set【DFS】(2016CCPC东北赛F题)
- Auxiliary Set
- HDU 5927 Auxiliary Set (树形DP+思维)
- [HDU 5927] Auxiliary Set (DFS+树上LCA)
- Auxiliary Set(dfs求每个节点的儿子个数)
- BAE数据库链接方法整理版
- PHP 引用赋值和传递赋值
- 把表A的某列值赋给表B对应的列SQL
- MFC 对文件以及文件夹操作
- 生化危机6终章中英双字
- Auxiliary Set----DFS思维题
- Git(1)/Git简介
- Ubuntu16.04下启动eclips
- LSM内核源代码分析与测试(一)
- BZOJ 1024, 生日快乐
- Android studio使用JNI实例
- Glide-图片的压缩
- 用gdb调试游戏服务端
- AngularJs服务-$location(暴露当前地址栏的URL)