hdu 5927 Auxiliary Set(思路)
来源:互联网 发布:大淘客cms二级域名 编辑:程序博客网 时间:2024/06/04 18:58
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 813 Accepted Submission(s): 256
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
http://www.cnblogs.com/hahatianx/p/5943435.html
受益匪浅,还能从这个角度去做题...
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 100005struct Edge{ int next,v;} edge[N*4];struct Node{ int v,d;} node[N];int cnt,head[N];int son_num[N],dep[N],father[N],temp_son_num[N];void init(){ cnt=0; memset(head,-1,sizeof(head)); memset(son_num,0,sizeof(son_num));}void addedge(int u,int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}int dfs(int u,int k,int d){ dep[u]=d; father[u]=k; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(v==k) continue; son_num[u]++; dfs(v,u,d+1); }}bool cmp(Node a,Node b){ return a.d>b.d;}int main(){ int T,n,m; int Tcase=1; int u,v,t,l; scanf("%d",&T); while(T--) { init(); scanf("%d %d",&n,&m); for(int i=1; i<n; i++) { scanf("%d %d",&u,&v); addedge(u,v); addedge(v,u); } dfs(1,-1,1); printf("Case #%d:\n",Tcase++); while(m--) { scanf("%d",&t); int ans=n-t,c=0; for(int i=1; i<=t; i++) { scanf("%d",&l); temp_son_num[l]=son_num[l]; node[c].v=l,node[c++].d=dep[l]; } sort(node,node+c,cmp); for(int i=0; i<c; i++) { int pos=node[i].v; if(temp_son_num[pos]>=2) ans++; else if(temp_son_num[pos]==0) temp_son_num[father[pos]]--; } printf("%d\n",ans); } } return 0;}
0 0
- hdu 5927 Auxiliary Set(思路)
- 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 (树形DP+思维)
- [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
- HDU - 5927 Auxiliary Set (树形DP)
- VS2008项目中各文件的作用
- MVC,MVP 和 MVVM 的图示
- 安卓1109_四大组件-Service 电话监听 bindservice 接口调用 线程间的通信 对话框
- Jetson TX1使用记录
- MAC XAMPP不能启动Apache
- hdu 5927 Auxiliary Set(思路)
- c 语言 命令行参数
- 数据结构(三)——栈(C语言实现)
- Keil编译错误汇总
- 如何在eclipse中安装svn插件
- FaceNet--Google的人脸识别
- Intent Flag启动模式
- C语言代码4(求最大公约数)
- Redis 主从 Replication 的配置